I am trying to create a warning dialog with an EditText object. I need to install the EditText source code programmatically. Here is what I have.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // ...Irrelevant code for customizing the buttons and title AlertDialog alertDialog = dialogBuilder.create(); LayoutInflater inflater = this.getLayoutInflater(); alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null)); EditText editText = (EditText) findViewById(R.id.label_field); editText.setText("test label"); alertDialog.show();
What do I need to change to have a valid EditText object?
[edit]
Thus, it was pointed out by user370305 and others that I should use alertDialog.findViewById (R.id.label_field);
Unfortunately, there is one more problem. Apparently, setting the presentation of content in AlertDialog causes the program to crash at runtime. You must install it on the builder.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // ...Irrelevant code for customizing the buttons and title dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null)); AlertDialog alertDialog = dialogBuilder.create(); LayoutInflater inflater = this.getLayoutInflater(); EditText editText = (EditText) alertDialog.findViewById(R.id.label_field); editText.setText("test label"); alertDialog.show();
Unfortunately, when you do this, alertDialog.findViewById (R.id.label_field); now returns null.
[/ edit]
android
Don Subert Mar 26 '14 at 8:48 2014-03-26 08:48
source share