Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why setCanceledOnTouchOutside(false) doesn't work in Alert builder?

I have an alert dialog in my activity and don't want user can dismiss it by clicking outside of the dialog. Based on my research (like this) I found setCanceledOnTouchOutside(false); method. However, I couldn't use it in my application and it is possible to dismiss dialog while I have this method.

this is my code:

private AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setTitle("");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
        switch (intAlertAction) {
            case 1:
            case 2:
            case 3:
            default:
        }
}
});

any suggestion would be appreciated.

like image 660
Hesam Avatar asked Nov 23 '12 09:11

Hesam


4 Answers

setCanceledOnTouchOutside only prevents dismissing by clicking outside of the dialog. But you can still dismiss it with back button for instance.

If you don't want your dialog to be cancelable at all use dialog.setCancelable(false)

I just tested your (fixed) code and it works as expected: the user cannot dismiss dialog when clicking out of it. Try it:

    AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.setTitle("");
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    alertDialog.show();
like image 150
sdabet Avatar answered Oct 07 '22 01:10

sdabet


This is an interesting question and I think I know your answer.

I have been testing an application on different platforms and I noticed a small difference between them. Above android 4.0 when you touch a Toast message, it simply disappears. I guess it is the same with dialogs (and AlertDialogs). It simply "disappears" when touching (but it is not dismissed! - just cannot be seen).

Hope it helped!

like image 25
keybee Avatar answered Oct 06 '22 23:10

keybee


Just add dialog.setCancelable(false) to disable the back button.

like image 22
the hien duong Avatar answered Oct 07 '22 01:10

the hien duong


Add setCancelable(false) in your AlertDialog, example :

AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setTitle("");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
alertDialog.show();
like image 29
Harsh Dev Chandel Avatar answered Oct 07 '22 01:10

Harsh Dev Chandel