Solution 1 :
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;
}
Problem :
I have a dialog that I want to be dismissed when I click outside it. However, I don’t want it to receive key events, because it’s a volume dialog, and I want the activity to be able to receive the key volume up and down events. So I setted FLAG_NOT_FOCUSABLE
, as you see below:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyDialogTheme);
AlertDialog alert = builder.create();
Window window = alert.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
alert.setView(view);
the problem is that, with this flag, I also can’t make the dialog dismiss when a touch outside it happens.
All the solutions on How to dismiss the dialog with click on outside of the dialog? like dialog.setCanceledOnTouchOutside(true);
wont work in this case.