Material-components-android: [MaterialAlertDialog] Dismiss Programmatically

Created on 2 Nov 2019  路  4Comments  路  Source: material-components/material-components-android

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();
bug

Most helpful comment

You need to call the build create 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();

All 4 comments

You need to call the build create 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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabrielemariotti picture gabrielemariotti  路  3Comments

sepehr-alipour picture sepehr-alipour  路  3Comments

MrCreeper1008 picture MrCreeper1008  路  3Comments

magnusfernandes picture magnusfernandes  路  3Comments

Mirmuhsin picture Mirmuhsin  路  3Comments