How to reject DialogFragment when pressed outside of a dialog box?

I am using DialogFragment , and while I have successfully set the image to close (i.e. dismiss) the dialog when I click, it is difficult for me to find a way to reject the dialog when the user clicks on it outside of it, just as it does with regular dialogs. I thought there would be some

 dialogFragment.setCanceledOnTouchOutside(true); 

but I do not see this in the documentation.

Is this possible with DialogFragment at all? Or am I looking in the wrong place? I tried to capture touch events in the "parent" activity, but besides not receiving any touch events, this did not seem right to me.

+56
android fragment dismiss
Dec 06 '11 at 17:29
source share
7 answers
 DialogFragment.getDialog().setCanceledOnTouchOutside(true); 

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

+133
Jan 06 2018-12-12T00:
source share
  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... getDialog().setCanceledOnTouchOutside(true); ... } 
+48
Jan 06 '12 at 23:25
source share
  /** The system calls this only when creating the layout in a dialog. */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // The only reason you might override this method when using onCreateView() is // to modify any dialog characteristics. For example, the dialog includes a // title by default, but your custom layout might not need it. So here you can // remove the dialog title, but you must call the superclass to get the Dialog. Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCanceledOnTouchOutside(true); return dialog; } 
+18
Jul 08 '14 at 2:06
source share
 DialogFragment.getDialog().setCanceledOnTouchOutside(false); 

It was a mistake. I have the same problem. This is great for Java and Mono for Android Mono:

 this.getDialog().SetCanceledOnTouchOutside(false); 
+1
Oct 10 '12 at 20:50
source share

There are many answers, but when the dialog box opens, the application crashes. Record getDialog().setCanceledOnTouchOutside(true); inside onCreateView did not work and crashed my application.

(I use AppCompatActivity as my BaseActivity and android.app.DialogFragment as my fragment).

Which works, either one of the following two lines:

getDialog () setCanceledOnTouchOutside (true) ;.

OR

this.getDialog () setCanceledOnTouchOutside (true) ;.

inside onActivityCreated as

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom; //getDialog().getWindow().setDimAmount(0.85f); getDialog().setCanceledOnTouchOutside(true);//See here is the code } 

What not to use:

DialogFragment.getDialog () setCanceledOnTouchOutside (false) ;.

causes the following error

enter image description here

And writing code to onCreateView crashes the application! Update your answer if you find something is wrong.

+1
Apr 6 '17 at 8:48
source share

I would recommend using my solution only after testing the above solutions. I described my solution here . Just for brevity, I check the touch faces of DialogFragment.getView (). When the touch points are outside the dialog box, I reject the dialog box.

0
Jan 20 '15 at 10:43
source share
  Dialog.SetCanceledOnTouchOutside(true); 

Worked for me
My code

 class dlgRegister : DialogFragment { public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { .... .... } public override void OnActivityCreated(Bundle savedInstanceState) { Dialog.Window.RequestFeature(WindowFeatures.NoTitle); Dialog.SetCanceledOnTouchOutside(true); base.OnActivityCreated(savedInstanceState); Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; } } 
0
Mar 14 '17 at 8:55
source share



All Articles