I have an activity that is using the Theme.Dialog style such that it is a floating window over another activity. However, when I click outside the dialog window (on the background activity), the dialog closes. How can I stop this behaviour?
-
1Why would you do that btw. IF the behaviour you want is of a dialog, why not use a dialog? Coming to your question, I don't think there's a solution to that. – Kumar Bibek Aug 24 '12 at 04:19
-
@KumarBibek dialogs are limited when it comes to layout customizations...that's why an activity. Even the developer docs recommends this approach for a flexible customisation – Leo Aug 28 '14 at 12:47
-
1And what the OP wants is a **Modal** dialog, ie a dialog that forces the user to respond, such as OK, or Yes/No. The user should not be able to just click away. – SMBiggs Aug 16 '16 at 14:57
20 Answers
To prevent dialog box from getting dismissed on back key pressed use this
dialog.setCancelable(false);
And to prevent dialog box from getting dismissed on outside touch use this
dialog.setCanceledOnTouchOutside(false);
-
7This answer is helpful but this question is about activity using Theme.Dialog attribute. – Paresh Mayani Apr 26 '14 at 08:28
-
19I know when I realize it is too late since it help many people so did not delete it. – Singhak Apr 28 '14 at 05:09
-
13I know this didn't really pertain to the question, but this helped me. Thanks! – dennisdrew Oct 14 '14 at 20:53
-
https://developer.android.com/reference/android/app/Dialog.html#setCanceledOnTouchOutside(boolean) thanks for being concise – CrandellWS May 26 '16 at 21:26
-
7This is the best answer. I suspect _most_ of the people who find this question are looking for a way to prevent the standard `AlertDialog` from closing on touch outside, and that's what this answer provides. – aroth Aug 09 '16 at 02:34
-
AlertDialog was exactly what I was trying to not close, thanks guys – Saik Caskey Dec 14 '16 at 14:58
-
31On newer versions of AlertDialog, you only need to use `setCancelable(false)` – Kobi Tate Aug 15 '17 at 19:18
-
1This method also worked for my custom `DialogFragment`, thanks. As @KobiTate mentioned, you only need to use `fragment.setCancelable(false)`. My search query: android stop click outside dialog box – Randall Arms Apr 25 '18 at 07:15
What you actually have is an Activity (even if it looks like a Dialog), therefore you should call setFinishOnTouchOutside(false)
from your activity if you want to keep it open when the background activity is clicked.
EDIT: This only works with android API level 11 or greater

- 1,776
- 2
- 11
- 16
-
5This doesn't prevent you from pressing "back" button in your activity. So you also need to override `onBackPressed()` doing nothing in it's body – Alex Bonel Nov 20 '13 at 13:23
-
It is not preventing "back" button in that dialog Activity without onBackPressed(). Works perfect with this.setFinishOnTouchOutside(false). – Kunalxigxag Mar 05 '14 at 09:25
What worked for me was to create DialogFragment
an set it to not be cancelable:
dialog.setCancelable(false);
-
2
-
All Dialogs belong to Dialog class, in the case of AlertDialog just set this flag to false and should work for outside touching and back button. – Giovanny Piñeros Feb 16 '22 at 18:16
This could help you. It is a way to handle the touch outside event:
How to cancel an Dialog themed like Activity when touched outside the window?
By catching the event and doing nothing, I think you can prevent the closing. But what is strange though, is that the default behavior of your activity dialog should be not to close itself when you touch outside.
(PS: the code uses WindowManager.LayoutParams)

- 1
- 1

- 1,785
- 13
- 16
-
1The object isn't a dialog though, it's an Activity that uses the dialog style. Activity doesn't have this method, and can't be cast to Dialog. – Fergusmac Aug 24 '12 at 03:47
-
ur post is the exact opposite of what was asked . he asked how to prevent closing not how to cause it . – ChuckKelly Sep 22 '13 at 21:11
-
4Which is what he explained with "By catching the event and doing nothing, I think you can prevent the closing". – metter Oct 17 '13 at 15:23
-
4for some reason, after doing the whole FLAG_NOT_MODAL, WATCH_OUTSIDE_TOUCH, the outside touch does it fact not close the activities, but the button behind the activity is clicked. any idea for that ? – njzk2 Nov 07 '13 at 21:22
-
Note that Dialogs have a shadow (at least till now (8.0)): if you click on the shadow Android will interpret that click just like it was inside the dialog. Just wanted to share this with the world, it took me 10 minutes to get it. – Lorenzo Von Matterhorn Feb 22 '18 at 15:53
-
This shouldn't be the accepted answer. See below answer by @Singhak – Code Wiget Jan 02 '20 at 02:56
When using dialog as an activity in the onCreate add this
setFinishOnTouchOutside(false);

- 2,030
- 22
- 25
For higher API 10, the Dialog disappears when on touched outside, whereas in lower than API 11, the Dialog doesn't disappear. For prevent this, you need to do:
In styles.xml
: <item name="android:windowCloseOnTouchOutside">false</item>
OR
In onCreate()
method, use: this.setFinishOnTouchOutside(false);
Note: for API 10 and lower, this method doesn't have effect, and is not needed.

- 2,499
- 5
- 30
- 54
Setting the dialog cancelable to be false is enough, and either you touch outside of the alert dialog or click the back button will make the alert dialog disappear. So use this one:
setCancelable(false)
And the other function is not necessary anymore:
dialog.setCanceledOnTouchOutside(false);
If you are creating a temporary dialog and wondering there to put this line of code, here is an example:
new AlertDialog.Builder(this)
.setTitle("Trial Version")
.setCancelable(false)
.setMessage("You are using trial version!")
.setIcon(R.drawable.time_left)
.setPositiveButton(android.R.string.yes, null).show();

- 74,896
- 15
- 165
- 198

- 403
- 4
- 5
Alert Dialog is deprecated so use Dialog dialog = new Dialog(this);
For prevent close on outside touch
dialog.setCanceledOnTouchOutside(false);

- 7,909
- 2
- 19
- 28
Use This Code it's Working For me
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setCancelable(false);

- 874
- 6
- 19
-
this should be the correct answer, if he could does the correct question! – alijunior Feb 28 '16 at 07:49
-
2This would be the correct answer, if the OP was asking about *dialogs*! But the question is about Activities--quite a different case. – SMBiggs Aug 16 '16 at 15:00
-
So `.setCanceledOnTouchOutside(false)` is only accessible after `.create()` – francis Jul 09 '21 at 09:52
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
//use this to dismiss the dialog on outside click of dialog
dialog.setCanceledOnTouchOutside(false);
//use this for not to dismiss the dialog on outside click of dialog.
Watch this link for more details about dialog.
dialog.setCancelable(false);
//used to prevent the dismiss of dialog on backpress of that activity
dialog.setCancelable(true);
//used to dismiss the dialog on onbackpressed of that activity

- 4,281
- 4
- 44
- 47
-
Please note that the question pertains to Activities used as dialogs, NOT the Dialog class. – SMBiggs Aug 16 '16 at 15:01
Simply,
alertDialog.setCancelable(false);
prevent user from click outside of Dialog Box.
I use this in onCreate(), seems to work on any version of Android; tested on 5.0 and 4.4.x, can't test on Gingerbread, Samsung devices (Note 1 running GB) have it this way by default:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
setFinishOnTouchOutside(false);
}
else
{
getWindow().clearFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
}
super.onCreate(savedInstanceState);

- 4,313
- 31
- 43
-
The perfect answer for Activities used as dialogs, NOT for Alert Dialog class. – Himani Dec 08 '21 at 09:28
-
Use setFinishOnTouchOutside(false)
for API > 11 and don't worry because its android's default behavior that activity themed dialog won't get finished on outside touch for API < 11 :) !!Cheerss!!

- 696
- 8
- 7
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
I guess this will help you.It Worked For me

- 59
- 1
- 1
-
2Please also try to explain why this will work... Just a code dump is not a good answer. – Codebender Sep 14 '15 at 05:06
Here is my solution:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setCancelable(false);

- 852
- 1
- 11
- 25
Also is possible to assign different action implementing onCancelListener:
alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialogInterface) {
//Your custom logic
}
});

- 183
- 1
- 18
-
How can I identify if the cancel is called because of tapping outside the alert dialog? – cegprakash May 13 '16 at 13:37
-
Very helpful to create custom actions on tapping outside an alert dialog! – Adam S. Mar 29 '19 at 03:27
I was facing the same problem. To handle it I set a OntouchListener
to the dialog and do nothing inside. But Dialog dismiss when rotating screen too. To fix it I set a variable to tell me if the dialog has normally dismissed. Then I set a OnDismissListener
to my dialog and inside I check the variable. If the dialog has dismmiss normally I do nothin, or else I run the dialog again (and setting his state as when dismissing in my case).
builder.setCancelable(false);
public void Mensaje(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("¿Quieres ir a el Menú principal?");
builder.setMessage("Al presionar SI iras a el menú y saldras de la materia.");
builder.setPositiveButton("SI", null);
builder.setNegativeButton("NO", null);
builder.setCancelable(false);
builder.show();
}
-
1Hi Alex. Welcome to StackOverflow and thank you for your answer. Could you please edit your answer to describe what your code does and how this solves the OP's problem? Code only answers are discouraged as they don't teach or explain the _why_. Thank you! – Tim Malone Jul 12 '16 at 23:42
In jetpack compose, use dismissOnClickOutside = false
property to prevent from closing.
AlertDialog(
title = {
Text("Title")
},
text = {
Text(text = name)
},
onDismissRequest = onDismiss,
confirmButton = {
TextButton(onClick = onDismiss ) {
Text("Yes")
}
},
dismissButton = {
TextButton(onClick = onDismiss ) {
Text("Cancel")
}
},
properties = DialogProperties(
dismissOnClickOutside = false
)
)
}

- 395
- 5
- 6
This is the perfect answer to all your questions.... Hope you enjoy coding in Android
new AlertDialog.Builder(this)
.setTitle("Akshat Rastogi Is Great")
.setCancelable(false)
.setMessage("I am the best Android Programmer")
.setPositiveButton("I agree", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();

- 41
- 9
-
1Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made. – Maximilian Peters Apr 22 '18 at 08:58