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();
setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this. Save this answer.
item_img_close); closeIcon_img. setOnClickListener(new View. OnClickListener() { // I want To dismiss Here }); dialog. show(); } });
You can use the methods cancel() or dismiss() .
try using
dialog.dismiss()
instead of using
dialog.cancel()
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).
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