Skip to content Skip to sidebar Skip to footer

How To Remove Alertdialog Programmatically

In an android application, I'm showing to the user an AlertDialog with no buttons, just a message. How can I destroy the AlertDialog programmatically so that it's not visible anymo

Solution 1:

You should refer to the AlertDialog itself, not the builder.

AlertDialog.Buildertest=newAlertDialog.Builder(context);
test.setTitle("title");
test.setCancelable(true);
test.setMessage("message...");
ALertDialogtestDialog= test.create();
testDialog.show();  // to show
testDialog.dismiss();  // to dismiss

Solution 2:

AlertDialog.Buildertest=newAlertDialog.Builder(context);
...

AlertDialogdialog= test.create().show();

Later you want to hide it:

 dialog.dismiss();

Solution 3:

add this alertDialog.setCanceledOnTouchOutside(true); to dismiss dialog if user touch outside

OR by click Device back button

alertDialog.setOnKeyListener(newDialogInterface.OnKeyListener() {
    @OverridepublicbooleanonKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            alertDialog.dismiss();
            returntrue;
        }
        returnfalse;
    }
})

Post a Comment for "How To Remove Alertdialog Programmatically"