Reputation: 451
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();
Upvotes: 45
Views: 125149
Reputation: 1
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.create()
ad.show();
You didn't use ad.create()
before ad.show();
Upvotes: -1
Reputation: 15789
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..
Upvotes: 11
Reputation: 175
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()
}
Upvotes: 2
Reputation: 2428
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).
Upvotes: 91
Reputation: 59
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
}
});
Upvotes: -2
Reputation: 126
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();
Upvotes: 0
Reputation: 719
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();
Upvotes: 5
Reputation: 21
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.
Upvotes: 2
Reputation: 159
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();
Upvotes: 6
Reputation: 131
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();
}
});
Upvotes: 0
Reputation: 2291
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.
Upvotes: 2
Reputation: 9154
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());
Upvotes: 0
Reputation: 4497
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();
}
Upvotes: 1
Reputation: 9361
Use setNegative button, no Positive button required! I promise you'll win x
Upvotes: 11
Reputation: 973
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.
Upvotes: 1