How do I close a Dialog in android programmatically for example by a button?
Imagine I have a Dialog with a OK button on it, and want to close it by OK button, but I cant do that!
I googled and found nothing useful, and almost all of them for closing AlertDialog not a Dialog.
setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this.
You may call dismiss(); on the dialog. This work for me.
I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.
Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.
You can call dismiss
on the dialog.
This is an example of how to create a AlertDialog with 2 Buttons (OK and cancel). When clicking the cancel button,
dialog.dismiss()
is called to close the dialog.
From anywhere outside, you could call
builder.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage("Some message.") .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // do something } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With