AlertDialog.Builder with custom layout and EditText; no view available

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]

+47
android
Mar 26 '14 at 8:48
source share
6 answers

editText is part of the alertDialog layout, so just use editText with the alertDialog link

 EditText editText = (EditText) alertDialog.findViewById(R.id.label_field); 

Update:

Because in the line of code dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflater Zero .

update your code as below and try to understand each line of code

 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // ...Irrelevant code for customizing the buttons and title LayoutInflater inflater = this.getLayoutInflater(); View dialogView = inflater.inflate(R.layout.alert_label_editor, null); dialogBuilder.setView(dialogView); EditText editText = (EditText) dialogView.findViewById(R.id.label_field); editText.setText("test label"); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); 
+135
Mar 26 '14 at 8:49
source share

Use this

  AlertDialog.Builder builder = new AlertDialog.Builder(activity); // Get the layout inflater LayoutInflater inflater = (activity).getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the // dialog layout builder.setTitle(title); builder.setCancelable(false); builder.setIcon(R.drawable.galleryalart); builder.setView(inflater.inflate(R.layout.dialogue, null)) // Add action buttons .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } } }); builder.create(); builder.show(); 
+17
Mar 26 '14 at 8:56
source share

You can write:

 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // ...Irrelevant code for customizing the buttons and title LayoutInflater inflater = this.getLayoutInflater(); View dialogView= inflater.inflate(R.layout.alert_label_editor, null); dialogBuilder.setView(dialogView); Button button = (Button)dialogView.findViewById(R.id.btnName); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Commond here...... } }); EditText editText = (EditText) dialogView.findViewById(R.id.label_field); editText.setText("test label"); dialogBuilder.create().show(); 
+2
Apr 22 '16 at 12:08
source share

Change this:

 EditText editText = (EditText) findViewById(R.id.label_field); 

:

 EditText editText = (EditText) v.findViewById(R.id.label_field); 
+1
Mar 26 '14 at 8:50
source share
 View v=inflater.inflate(R.layout.alert_label_editor, null); alertDialog.setContentView(v); EditText editText = (EditText)v.findViewById(R.id.label_field); editText.setText("test label"); alertDialog.show(); 
0
Mar 26 '14 at 8:50
source share
 /** * Shows confirmation dialog about signing in. */ private void startAuthDialog() { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); alertDialog.getWindow().setLayout(800, 1400); LayoutInflater inflater = this.getLayoutInflater(); View dialogView = inflater.inflate(R.layout.auth_dialog, null); alertDialog.getWindow().setContentView(dialogView); EditText editText = (EditText) dialogView.findViewById(R.id.label_field); editText.setText("test label"); } 
0
Nov 21 '17 at 7:26
source share



All Articles