Reputation: 1819
I want to be able to dynamically dismiss an AlertDialog
from within it's own button callback:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (/* if some condition is met */) {
// dismiss the alert
} else {
// keep the alert open
}
}
});
final AlertDialog alert_dialog = alert.create();
alert_dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
alert_dialog.setCanceledOnTouchOutside(false);
alert_dialog.show();
I know that I can call dismiss()
on alert_dialog
but I cannot put this call within the very code that creates it.
Upvotes: 0
Views: 717
Reputation: 15145
The DialogInterface
in the callback is the Dialog
itself (Dialog
implements DialogInterface
), so all you have to do is call the DialogInterface#dismiss()
method:
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (/* if some condition is met */) {
dialog.dismiss(); // dismiss the alert
} else {
// keep the alert open
}
}
});
Upvotes: 2