Arci
Arci

Reputation: 6819

How to click 'OK' on an AlertDialog via code?

I use showDialog and dismissDialog in activity to display and destroy my dialog. Is there also a way to issue a click command on the currently displayed dialog without keeping a variable referencing the dialog?

For example, I want to press the 'Ok' / positive button of the dialog via code.

Upvotes: 13

Views: 14749

Answers (4)

byteC0de
byteC0de

Reputation: 5273

If you are using Builder, it's doesn't have the getButton() function. You can try this one

AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
alBuilder.setMessage("Test Message")
         .setPositiveButton("Yes", null)
         .setNegativeButton("No",null)
alBuilder.setCancelable(false);
AlertDialog alertDialog = alBuilder.show();

Then you can access the button by below code

alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();

Upvotes: 1

Matt Giles
Matt Giles

Reputation: 751

I haven't tested this code but it should work:

AlertDialog dialog = ...
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();

Alternatively, if you don't want to keep a reference to the dialog but are in control of its setup, you could extract the on click code into another method:

AlertDialog.Builder builder = ...
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {
    onPositiveButtonClicked(); 
  }
});

and implement onPositiveButtonClicked() in your Activity. Instead of programatically clicking the OK button you can call onPositiveButtonClicked() and dismissDialog(id). If you need to handle multiple dialogs, have onPositiveButtonClicked take an id parameter.

Upvotes: 33

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

I want to press the 'Ok' / positive button of the dialog via code

Yes, you can do it by getting instance of POSITIVE BUTTON and then call performClick() on it. try it as:

Button okButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
okButton.performClick(); //<<< click Button using code

Upvotes: 5

bakriOnFire
bakriOnFire

Reputation: 2681

Try this:-

AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
        alBuilder
                .setMessage("Do you wamt to exit?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                                // Write your code here for Yes
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                                // Write your code here for No
                            }
                        });
        alBuilder.setCancelable(false);
        alBuilder.show();

Upvotes: -1

Related Questions