How can I dismiss MaterialAlertDialog programmatically?
The following code does not work like AlertDialog.Builder:
MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(this)
.setTitle("Example")
.setPositiveButton("OK", null)
.show();
materialAlertDialog.dismiss();
You need to call the buildcreate method on the AlertDialog.Builder class (which MaterialAlertDialogBuilder extends from) in order to get an instance of the dialog:
AlertDialog alertDialog = new MaterialAlertDialogBuilder(this)
.setTitle("Example")
.setPositiveButton("OK", null)
.create(); // Not the `build()` method!
// Show the dialog
alertDialog.show();
// Dismiss the dialog once you're done handling stuff
alertDialog.dismiss();
@lucianocn Does my solution solve your problem? If it does, please reply and close the issue.
@lucianocn Any updates?
@EdricChan03
Yes, your solution solved my problem.
Thanks you!
Most helpful comment
You need to call the
buildcreatemethod on theAlertDialog.Builderclass (whichMaterialAlertDialogBuilderextends from) in order to get an instance of the dialog: