android – Dismiss DialogFragment(not Dialog) onTouchOutside-ThrowExceptions

Exception or error:

I have searched through all the answers about dismissing a Dialog onTouchOutside, however, I am using DialogFragment in my application. How can I achieve dismissing the DialogFragment when user clicks outside the DialogFragment’s region.

I have examined Dialog‘s source code for setCanceledOnTouchOutside

public void setCanceledOnTouchOutside(boolean cancel) {
    if (cancel && !mCancelable) {
        mCancelable = true;

    mCanceledOnTouchOutside = cancel;

There’s another function which may be interesting which is isOutOfBounds

private boolean isOutOfBounds(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop)
    || (x > (decorView.getWidth()+slop))
    || (y > (decorView.getHeight()+slop));

but I couldn’t figure out a way to make use of these for DialogFragment

In addition to these I have examined the state of the application with hierarchyviewer
and as I understand it, I can only see the region of the dialog and not the outsied part of it (I mean the remaining part of the screen after the DialogFragment).

Can you suggest a way of implementing this setCanceledOnTouchOutside for DialogFragment and if possible with a sample code?

How to solve:

The answer is pretty simple:

MyDialogFragment fragment = new MyDialogFragment(); // init in onCreate() or somewhere else
if ( fragment.getDialog() != null )
    fragment.getDialog().setCanceledOnTouchOutside(true); // after fragment has already dialog, i. e. in onCreateView()

See for more info about dialogs in DialogFragments.


In most cases, getDialog() is null, since you won’t get it immediately after you make a new instance of your dialog.

As suggested above onViewCreated is also nor correct for DialogFragment especially when using

The below works well, as it’s placed on the onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (getDialog() != null) {
    return super.onCreateView(inflater, container, savedInstanceState);


Why not just override onCreateDialog and just set it there. That way you don’t have to check for null on the getDialog() call all the time.

 public Dialog onCreateDialog(Bundle savedInstanceState) {
     Dialog d =  super.onCreateDialog(savedInstanceState);
     return d;


I was reading:

It states: “Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.”


I have answered to another question, but that answer is more appropriate for this problem, My solution . Just to brief here, I simply implemented onTouch() of DialogFragment‘s getView() and checked touch bounds.

Leave a Reply

Your email address will not be published. Required fields are marked *