Prevent Android activity dialog from closing on outside touch
Solution 1
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)
Solution 2
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);
Solution 3
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
Solution 4
What worked for me was to create DialogFragment
an set it to not be cancelable:
dialog.setCancelable(false);
Solution 5
When using dialog as an activity in the onCreate add this
setFinishOnTouchOutside(false);
Related videos on Youtube
Fergusmac
Updated on April 24, 2022Comments
-
Fergusmac about 2 years
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?
-
Kumar Bibek over 11 yearsWhy 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.
-
Leo over 9 years@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
-
SMBiggs over 7 yearsAnd 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.
-
-
Fergusmac over 11 yearsThe 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.
-
ChuckKelly over 10 yearsur post is the exact opposite of what was asked . he asked how to prevent closing not how to cause it .
-
metter over 10 yearsWhich is what he explained with "By catching the event and doing nothing, I think you can prevent the closing".
-
njzk2 over 10 yearsfor 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 ?
-
Alex Bonel over 10 yearsThis doesn't prevent you from pressing "back" button in your activity. So you also need to override
onBackPressed()
doing nothing in it's body -
Kunalxigxag about 10 yearsIt is not preventing "back" button in that dialog Activity without onBackPressed(). Works perfect with this.setFinishOnTouchOutside(false).
-
Paresh Mayani about 10 yearsThis answer is helpful but this question is about activity using Theme.Dialog attribute.
-
Singhak about 10 yearsI know when I realize it is too late since it help many people so did not delete it.
-
dennisdrew over 9 yearsI know this didn't really pertain to the question, but this helped me. Thanks!
-
Codebender over 8 yearsPlease also try to explain why this will work... Just a code dump is not a good answer.
-
alijunior about 8 yearsthis should be the correct answer, if he could does the correct question!
-
cegprakash almost 8 yearsHow can I identify if the cancel is called because of tapping outside the alert dialog?
-
CrandellWS almost 8 yearsdeveloper.android.com/reference/android/app/… thanks for being concise
-
Tim Malone almost 8 yearsHi 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!
-
aroth over 7 yearsThis 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. -
SMBiggs over 7 yearsNot helpful. This question is about Activities, not Dialogs.
-
SMBiggs over 7 yearsThis would be the correct answer, if the OP was asking about dialogs! But the question is about Activities--quite a different case.
-
SMBiggs over 7 yearsPlease note that the question pertains to Activities used as dialogs, NOT the Dialog class.
-
Saik Caskey over 7 yearsAlertDialog was exactly what I was trying to not close, thanks guys
-
Kobi Tate over 6 yearsOn newer versions of AlertDialog, you only need to use
setCancelable(false)
-
Lorenzo Von Matterhorn about 6 yearsNote 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.
-
Maximilian Peters about 6 yearsThank 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.
-
Randall Arms about 6 yearsThis method also worked for my custom
DialogFragment
, thanks. As @KobiTate mentioned, you only need to usefragment.setCancelable(false)
. My search query: android stop click outside dialog box -
Adam S. about 5 yearsVery helpful to create custom actions on tapping outside an alert dialog!
-
Code Wiget over 4 yearsThis shouldn't be the accepted answer. See below answer by @Singhak
-
francis almost 3 yearsSo
.setCanceledOnTouchOutside(false)
is only accessible after.create()
-
Himani over 2 yearsThe perfect answer for Activities used as dialogs, NOT for Alert Dialog class.
-
3c71 over 2 yearsOP's question is about activities, not alert dialog.
-
Giovanny Piñeros about 2 yearsAll 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.