How to dismiss the dialog with click on outside of the dialog?

android alertdialog prevent dismiss when click outside
android alertdialog touch outside listener
android dialog click outside listener
android click behind dialog
android dialog outside touch event
android dialog disable background click
dismiss view when touched outside android
dialog.setcanceledontouchoutside(true) not working

I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed. What do I have to do for this?

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

Something like,

  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(true);

Or if your Dialog in non-model then,

1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.

3 - Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

public boolean onTouchEvent(MotionEvent event)  
{  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
        System.out.println("TOuch outside the dialog ******************** ");  
               this.dismiss();  
       }  
       return false;  
}  

For more info look at How to dismiss a custom dialog based on touch points? and How to dismiss your non-modal dialog, when touched outside dialog region

Prevent Android activity dialog from closing on outside touch, Dialog dialog = new Dialog(context) dialog.setCanceledOnTouchOutside(true); // use this to dismiss the dialog on outside click of dialog dialog. This example demonstrates how do I dismiss the dialog with the click on outside of the dialog box in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Simply use

dialog.setCanceledOnTouchOutside(true);

How to dismiss the dialog with click on outside of the dialog?, This example demonstrate about how to dismiss the dialog with click on outside of the dialogStep 1 − Create a new project in Android Studio,� 3 - Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

You can use this implementation of onTouchEvent. It prevent from reacting underneath activity to the touch event (as mentioned howettl).

@Override
public boolean onTouchEvent ( MotionEvent event ) {
  // I only care if the event is an UP action
  if ( event.getAction () == MotionEvent.ACTION_UP ) {
    // create a rect for storing the window rect
    Rect r = new Rect ( 0, 0, 0, 0 );
    // retrieve the windows rect
    this.getWindow ().getDecorView ().getHitRect ( r );
    // check if the event position is inside the window rect
    boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
    // if the event is not inside then we can close the activity
    if ( !intersects ) {
      // close the activity
      this.finish ();
      // notify that we consumed this event
      return true;
    }
  }
  // let the system handle the event
  return super.onTouchEvent ( event );
}

Source: http://blog.twimager.com/2010/08/closing-activity-by-touching-outside.html

How to dismiss the dialog with the click on outside of the dialog box , This example demonstrates how do I dismiss the dialog with the click on outside of the dialog box in android.Step 1 − Create a new project in� How to dismiss the dialog with click on outside of the dialog? (8) I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed. What do I have to do for this?

Or, if you're customizing the dialog using a theme defined in your style xml, put this line in your theme:

<item name="android:windowCloseOnTouchOutside">true</item>

Prevent Android activity dialog from closing on outside touch in , It will work fine when user clicks inside the dialog box but when he clicks outside then it will disappear.Then I found the solution given below. A dialog contains interactions itself, and that could be dismissing the dialog or doing something such as choosing items from a list of things, having a online talk as in a chat room. So, I think, a click at the outside area to dismiss the dialog will blur the boundary between dialog and message box. (By a non-native English speaker.

dialog.setCanceledOnTouchOutside(true); 

to close dialog on touch outside.

And if you don't want to close on touch outside, use the code below:

dialog.setCanceledOnTouchOutside(false);

android.app.Dialog.setCanceledOnTouchOutside java code , How to dismiss the dialog with click on outside of the dialog? Dialog dialog = new Dialog(context) dialog.setCanceledOnTouchOutside(true);. I am using a DialogFragment, and while I have successfully set an image to close (i.e. dismiss) the dialog when pressed, I am having a hard time finding the way to dismiss the dialog when the user clicks anywhere outside it, just as it works with normal dialogs. I thought there would be some sort of

android How to dismiss the dialog with click on outside of the dialog , android alertdialog prevent dismiss when click outside prevent dialog from closing android android close dialog on touch outside disable click outside dialog The dialog route created by this method is pushed to the root navigator. If the application has multiple Navigator objects, it may be necessary to call Navigator.of(context, rootNavigator: true).pop(result) to close the dialog rather just Navigator.pop(context, result). so I'd assume one of these two should do what you want.

How to Dialogfragment disable click outside on android?, How to stop dialog from dismissing when clicked on positive button , If it is i How can I achieve dismissing the DialogFragment when user clicks outside the� When we write a custom dialog class or set theme style/Theme.Dialog to activity, we might be interested in dismissing the dialog if user touches outside the dialog visible area. This can be achieved through a special class, android.graphics.Regio n. It has a api contains(x,y) which is used to tell you if the passed x,y falls in the region defined.

AlertDialog dialog.dismiss(), dialog.close() not working, Sets whether this dialog is canceled when touched outside the window's bounds. Set a listener to be invoked when the dialog is dismissed. How to dismiss the dialog with click on outside of the dialog? 229. Prevent Android activity dialog from closing on outside touch. 410.

Comments
  • This works great except that the activity underneath also reacts to the touch event. Is there some way to prevent this?
  • Yeah. window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); causes this problem. I've posted a solution below :)
  • Is it possible to propagate 'on-touch-outside' events to the underneath activity using a non-custom dialog too?
  • @howettl I have solved your problem in my solution which I have posted below where I don't need to set any flags to window.
  • @MuhammedRefaat - Please look at this thread groups.google.com/forum/#!topic/android-developers/VhaiIMl6E_w . They nicely described it.
  • I know that this should the right answer, but doesn't work for me, and I just don't know why.
  • This does not work for me on Samsung Galaxy Tab 2 WiFi. dialog.setCanceledOnTouchOutside(true); does work wonderfully.
  • Cant you just make your dialog touch modal if you dont want other windows recieving events?
  • Very bad answer! Of course this can be done, but please do it the right way!