12

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.

Arci
  • 6,647
  • 20
  • 70
  • 98
  • Are you using a builder to set up your dialog? – TronicZomB May 07 '13 at 02:54
  • @TronicZomB: Yes, I'm using `Builder` and I'm overriding `onCreateDialog` in the `Activity` to set up my dialog. – Arci May 07 '13 at 03:05
  • 1
    Ok, bakriOnFire has a rather good example of what I was actually going to post right before I noticed his answer. That is pretty much what you will want to model your code like. – TronicZomB May 07 '13 at 03:07
  • @TronicZomB: I think he misunderstood my question. I know how setup a dialog and how to show and dismiss it. My problem is how to trigger a click event on it via code considering that I don't have a reference to the dialog variable. – Arci May 07 '13 at 03:15
  • Hmm.. ok I think I might be following you... Good luck! – TronicZomB May 07 '13 at 03:17

4 Answers4

32

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.

Matt Giles
  • 721
  • 5
  • 8
  • Thanks, but I'm using Builder and it does not have a `getButton` method. Is there a way to click the 'OK' button without keeping a reference of the actual dialog? – Arci May 07 '13 at 03:11
  • 4
    If you're able to keep a reference to the dialog, the AlertDialog instance is returned by AlertDialog.Builder#show(). Otherwise, is it possible to move the on-click logic to a separate method? Instead of simulating a click you could just execute the on-click code and dismiss the dialog. – Matt Giles May 07 '13 at 03:16
  • Yes, it returns an `AlertDialog` but I don't like to keep a variable referencing the `AlertDialog`. Is there a way to access the dialog using the `Activity` given that I have the ID of the dialog? – Arci May 07 '13 at 03:20
  • I understand your question, but I don't know how to do that. The API doesn't seem to make it easy; keep a reference to the dialog might be the cleanest way to go if factoring out the on-click logic and dismissing the dialog is not an option. If you find a nice way to do it please post. I'd be interested to know :) – Matt Giles May 07 '13 at 03:41
  • I wasn't able to find an easy way to access the currently shown dialog on the Activity. I ended up using your solution to save a reference of the `AlertDialog` and triggering the positive button by using `dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick()`. Thanks! – Arci May 07 '13 at 07:11
5

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
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks! But what if I don't have reference to the ``variable? Is there a way to access the `POSITIVE` button using the `Activity` only (similar to `dismissDialog`)? – Arci May 07 '13 at 03:09
1

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();
byteC0de
  • 5,153
  • 5
  • 33
  • 66
-1

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();
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
  • Thanks! But I think you misunderstood my question. – Arci May 07 '13 at 03:12
  • I'm looking for something like `activity.getActiveDialog(DIALOG_ID).getButton(...).performClick()`. Just like `activity.dismissDialog(DIALOG_ID)` wherein you can dismiss the dialog by just using the DIALOG_ID. – Arci May 07 '13 at 07:17