Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can can I add custom buttons into an AlertDialog's layout?

I have AlertDialog with Positive and Negative buttons. In AlertDialog layout I have EditText and two Buttons (btnAdd1, btnAdd2). I want when user click at the Button btnAdd1 or btnAdd2 add same text to EditText in AlertDialog (but no close AlertDialog). Is this possible do in AlertDialog or I have to use only Dialog?

This is layout (R.layout.prompt) of AlertDialog:

<LinearLayout> <EditText     android:id="@+id/userInput"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:inputType="text" >      <requestFocus /> </EditText>  <Button     android:id="@+id/btnAdd1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="bla" />  <Button     android:id="@+id/btnAdd2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="bla" />    </LinearLayout> 

And this is source code:

    LayoutInflater layoutInflater = LayoutInflater.from(this);         View promptView = layoutInflater.inflate(R.layout.prompt, null);      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);     alertDialogBuilder.setView(promptView);     alertDialogBuilder             .setCancelable(false)             .setPositiveButton("OK", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int id) {                               //...                  }             })             .setNegativeButton("Cancel",                     new DialogInterface.OnClickListener() {                         public void onClick(DialogInterface dialog, int id) {                             dialog.cancel();                         }                     });      AlertDialog alertD = alertDialogBuilder.create();     alertD.show(); 

I want get acces to the btnAdd1 and btnAdd2 from the layout. Set the OnClickListener() to these two buttons.

like image 282
Pepa Zapletal Avatar asked Aug 21 '13 08:08

Pepa Zapletal


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.


2 Answers

The following code will inflate a view from R.layout.prompt and set it to the AlertDialog. The positive and negative buttons will not be used. You can set the onClick behaviors for btnAdd1 and btnAdd2:

LayoutInflater layoutInflater = LayoutInflater.from(this); View promptView = layoutInflater.inflate(R.layout.prompt, null);  final AlertDialog alertD = new AlertDialog.Builder(this).create();  EditText userInput = (EditText) promptView.findViewById(R.id.userInput);  Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);  Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);  btnAdd1.setOnClickListener(new OnClickListener() {     public void onClick(View v) {          // btnAdd1 has been clicked      } });  btnAdd2.setOnClickListener(new OnClickListener() {     public void onClick(View v) {          // btnAdd2 has been clicked      } });  alertD.setView(promptView);  alertD.show(); 
like image 137
Vikram Avatar answered Sep 20 '22 08:09

Vikram


what you want to do is;

alertD.show(); Button button = (Button)promptView.findViewById(R.id.buttonId); button.setOnClickListener(....) 

using the view to call findViewById, rather than the activity, which will look for the id in the layout that is being displayed.

like image 23
jcw Avatar answered Sep 17 '22 08:09

jcw