Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Android activity dialog from closing on outside touch

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?

like image 380
Fergusmac Avatar asked Aug 24 '12 03:08

Fergusmac


People also ask

How do I stop dialog close on click outside Android?

Simply, alertDialog. setCancelable(false); prevent user from click outside of Dialog Box.

How do I keep my dialog from closing?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);

How do I stop dialog close on click outside flutter?

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.

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.


2 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); 
like image 104
Singhak Avatar answered Sep 18 '22 08:09

Singhak


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

like image 31
artex Avatar answered Sep 21 '22 08:09

artex