Prevent Android activity dialog from closing on outside touch

200,443

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);
Share:
200,443

Related videos on Youtube

Fergusmac
Author by

Fergusmac

Updated on April 24, 2022

Comments

  • Fergusmac
    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
      Kumar Bibek over 11 years
      Why 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
      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
      SMBiggs over 7 years
      And 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
    Fergusmac over 11 years
    The 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
    ChuckKelly over 10 years
    ur post is the exact opposite of what was asked . he asked how to prevent closing not how to cause it .
  • metter
    metter over 10 years
    Which is what he explained with "By catching the event and doing nothing, I think you can prevent the closing".
  • njzk2
    njzk2 over 10 years
    for 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
    Alex Bonel over 10 years
    This 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
    Kunalxigxag about 10 years
    It is not preventing "back" button in that dialog Activity without onBackPressed(). Works perfect with this.setFinishOnTouchOutside(false).
  • Paresh Mayani
    Paresh Mayani about 10 years
    This answer is helpful but this question is about activity using Theme.Dialog attribute.
  • Singhak
    Singhak about 10 years
    I know when I realize it is too late since it help many people so did not delete it.
  • dennisdrew
    dennisdrew over 9 years
    I know this didn't really pertain to the question, but this helped me. Thanks!
  • Codebender
    Codebender over 8 years
    Please also try to explain why this will work... Just a code dump is not a good answer.
  • alijunior
    alijunior about 8 years
    this should be the correct answer, if he could does the correct question!
  • cegprakash
    cegprakash almost 8 years
    How can I identify if the cancel is called because of tapping outside the alert dialog?
  • CrandellWS
    CrandellWS almost 8 years
  • Tim Malone
    Tim Malone almost 8 years
    Hi 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
    aroth over 7 years
    This 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
    SMBiggs over 7 years
    Not helpful. This question is about Activities, not Dialogs.
  • SMBiggs
    SMBiggs over 7 years
    This would be the correct answer, if the OP was asking about dialogs! But the question is about Activities--quite a different case.
  • SMBiggs
    SMBiggs over 7 years
    Please note that the question pertains to Activities used as dialogs, NOT the Dialog class.
  • Saik Caskey
    Saik Caskey over 7 years
    AlertDialog was exactly what I was trying to not close, thanks guys
  • Kobi Tate
    Kobi Tate over 6 years
    On newer versions of AlertDialog, you only need to use setCancelable(false)
  • Lorenzo Von Matterhorn
    Lorenzo Von Matterhorn about 6 years
    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.
  • Maximilian Peters
    Maximilian Peters about 6 years
    Thank 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
    Randall Arms about 6 years
    This 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
  • Adam S.
    Adam S. about 5 years
    Very helpful to create custom actions on tapping outside an alert dialog!
  • Code Wiget
    Code Wiget over 4 years
    This shouldn't be the accepted answer. See below answer by @Singhak
  • francis
    francis almost 3 years
    So .setCanceledOnTouchOutside(false) is only accessible after .create()
  • Himani
    Himani over 2 years
    The perfect answer for Activities used as dialogs, NOT for Alert Dialog class.
  • 3c71
    3c71 over 2 years
    OP's question is about activities, not alert dialog.
  • Giovanny Piñeros
    Giovanny Piñeros about 2 years
    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.