Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close an Android alertdialog

Tags:

android

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();  
like image 989
Engmex Avatar asked Dec 02 '10 15:12

Engmex


People also ask

How do I dismiss Materialalertdialogbuilder in Android?

setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this. Save this answer.

How do I dismiss custom alert dialog?

item_img_close); closeIcon_img. setOnClickListener(new View. OnClickListener() { // I want To dismiss Here }); dialog. show(); } });

How do I close dialog on Android?

You can use the methods cancel() or dismiss() .


2 Answers

try using

dialog.dismiss()

instead of using

dialog.cancel()

like image 38
user508412 Avatar answered Oct 02 '22 16:10

user508412


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).

like image 139
PowerAktar Avatar answered Oct 02 '22 16:10

PowerAktar