Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss a DialogFragment when pressing outside the dialog?

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

dialogFragment.setCanceledOnTouchOutside(true); 

call, but I don't see that in the documentation.

Is this possible with DialogFragment at all? Or am I looking in the wrong places? I tried intercepting touch events in the 'parent' activity but apart from not getting any touch event, it didn't seem right to me.

like image 721
sole Avatar asked Dec 06 '11 17:12

sole


2 Answers

DialogFragment.getDialog().setCanceledOnTouchOutside(true); 

Must be called in onCreateView (as Apurv Gupta pointed out).

like image 64
Hamed Avatar answered Sep 23 '22 21:09

Hamed


    @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        ...        getDialog().setCanceledOnTouchOutside(true);        ...         } 
like image 39
manuzhang Avatar answered Sep 19 '22 21:09

manuzhang