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.
If you are using Builder, it's doesn't have the getButton() function. You can try this one
Then you can access the button by below code
Yes, you can do it by getting instance of
POSITIVE
BUTTON and then callperformClick()
on it. try it as:Try this:-
I haven't tested this code but it should work:
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:
and implement
onPositiveButtonClicked()
in your Activity. Instead of programatically clicking the OK button you can callonPositiveButtonClicked()
anddismissDialog(id)
. If you need to handle multiple dialogs, haveonPositiveButtonClicked
take anid
parameter.