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?
Simply, alertDialog. setCancelable(false); prevent user from click outside of Dialog Box.
AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);
To make your AlertDialog widget not close when user taps on the screen space outside the alert box, set barrierDismissible property to false in showDialog() helper method. By default, if you do not provide any value to barrierDismissable property, alert dialog closes when user taps on the area outside the alert.
You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With