[android] How do I close an Android alertdialog

I am developing a quiz and I need the user to answer all the questions before proceeding. When the user has not answered all the questions I display a simple alertdialog informing him or her. The problem is whatever I do I can't get the alertdialog to close. Why isn't dialog.cancel working?`This is the code:

AlertDialog.Builder ad = new AlertDialog.Builder(this);  
ad.setTitle("Unanswered Questions");  
ad.setMessage("You have not answered all the questions.");   
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int id) {  
     dialog.cancel(); 
}  
});  
ad.show(); 

This question is related to android

The answer is


AlertDialog.Builder ad = new AlertDialog.Builder(this);  
ad.setTitle("Unanswered Questions");  
ad.setMessage("You have not answered all the questions.");   
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int id) {  
     dialog.dismiss(); 
}  
});  
ad.show(); 

The AlertDialog.Builder itself does not contain a dismiss() or cancel() method.

It is a convenience class to help you create a Dialog, which DOES have access to those methods.

Here is an example:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

AlertDialog alert = builder.create();

alert.show();

You can then call the alert.cancel() method on the alert (not the builder).


Use setNegative button, no Positive button required! I promise you'll win x


Use Dialog instead of AlertDialog

AlertDialog doesn't have dismiss() but AlertDialog has some methods for button like setPositiveButton().

I recommend to use Dialog if you want customized dialog.


Just in case anyone was looking for the Kotlin version of this, it is as follows:

alertDialog.setPositiveButton(resources.getString(R.string.split_okay)) { 
    dialog, _ ->
        dialog.dismiss()
}

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
AlertDialog alert = builder.create(); 
alert.show();

The above code works but make sure you make alert a global variable so you can reach it from within the onClick method.


you can simply restart the activity where your alertdialog appear or another activity depend on your judgement. if you want to restart activity use this finish(); startActivity(getIntent());


alertDialog.setPositiveButton("SAVE",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*Write your code here */
                        dialog.dismiss();
                    }
                });
        alertDialog.setNegativeButton("CANCEL",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });

I would try putting a

Log.e("SOMETAG", "dialog button was clicked");

before the dialog.dismiss() line in your code to see if it actually reaches that section.


try using

dialog.dismiss()

instead of using

dialog.cancel()


Replying to an old post but hopefully somebody might find this useful. Do this instead

final AlertDialog builder = new AlertDialog.Builder(getActivity()).create();

You can then go ahead and do,

builder.dismiss();

If you already use positive and negative button (like I do in my project) you can use Neutral Button to close the dialog.

I also noticed that in Android version >5 the dialog is closed by clicking outside of dialog window but in older version this is not happening.

ad.setNeutralButton("CLOSE", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // close dialog
    }
});

put this line in OnCreate()

Context mcontext = this;    

and them use this variable in following code

final AlertDialog.Builder alert = new AlertDialog.Builder(mcontext);
alert.setTitle(title);
alert.setMessage(description);
alert.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
alert.show();

Try this code.. It is running successfully..


final AlertDialog.Builder alert = new AlertDialog.Builder(mcontext);
alert.setTitle(title);
alert.setMessage(description);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
  @Override                                 
  public void onClick(DialogInterface dialog,int which) {
        cancelDialog(); //Implement method for canceling dialog
         }
   });
alert.show();
void cancelDialog()
{
   //Now you can either use  
   dialog.cancel();
    //or dialog.dismiss();
}

I tried the solution of PowerAktar, but the AlertDialog and the Builder always kept seperate parts. So how to get the "true" AlertDialog?

I found my solutions in the show-Dialog: You write

ad.show();

to display the dialog. In the help of show it says "Creates a AlertDialog with the arguments supplied to this builder and Dialog.show()'s the dialog." So the dialog is finally created here. The result of the show()-Command is the AlertDialog itself. So you can use this result:

AlertDialog adTrueDialog;
adTrueDialog = ad.show();

With this adTrueDialog it is possible to cancel() ...

adTrueDialog.cancel()

or to execute a buttons command within the dialog:

Button buttonPositive = adTrueDialog.getButton(Dialog.BUTTON_POSITIVE);
buttonPositive.performClick();