9

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.

Community
  • 1
  • 1
Hesam
  • 52,260
  • 74
  • 224
  • 365

4 Answers4

31

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();
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • Thanks fiddler, but based on complain :) my client doesn't want the dialog dismissed by clicking outside of dialog. Therefore, back button is not the issue at the moment. another thing, I think above method works fine for dialog (based on accepted answers, i didn't test it) but it doesn't work in AlertDialog. – Hesam Nov 23 '12 at 10:09
  • I don't get you. 1) Do you want the dialog to be dismissed when clicking outside ? 2) Do you want the dialog to be dismissed when hitting back button ? – sdabet Nov 23 '12 at 10:10
  • I have AlertDialog not Dialog. The difference based on my codding is in Dialog thos two methods (dialog.setCancelable(false) and dialog.setCanceledOnTouchOutside(false);) works fine. But in AlertDialog these are not working. The request is don't let user to dismiss dialog (in my case alert dialog) by tapping outside of dialog. Hope to explain it well, sorry my English is horrible. – Hesam Nov 23 '12 at 10:21
  • I just tested the code with AlertDialog and it works well. See my edit – sdabet Nov 23 '12 at 10:29
  • wow, I didn't find any difference between your code and mine. I'm testing on Android v4.1. Is it same as your testing Android version? – Hesam Nov 23 '12 at 10:32
  • dialog.setCancelable(false) works like a charm! thanks. – marcwjj Jul 08 '15 at 22:32
7

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!

twlkyao
  • 14,302
  • 7
  • 27
  • 44
keybee
  • 1,498
  • 20
  • 32
  • Thanks Keybee, yes you are right. I ran above code on android 2.3.3 and result was as I expected. I couldn't dismiss dialog by clocking off the dialog while in Android 4.1 dialog dismisses. Android is bl.sht :( – Hesam Nov 27 '12 at 02:13
  • just use alertDialog.setCancelable(false); and it will work..:) @Hesam – Vivek Raj Agarwal Nov 23 '18 at 13:58
3

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

Yun
  • 3,056
  • 6
  • 9
  • 28
2

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();
Aldan
  • 674
  • 9
  • 23