How to dismiss a DialogFragment when pressing outside the dialog?

前端 未结 8 562
盖世英雄少女心
盖世英雄少女心 2020-12-04 17:05

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

相关标签:
8条回答
  • 2020-12-04 17:43

    I would recommend to use my solution only after trying out above solutions. I have described my solution here. Just to brief, I am checking touch bounds of DialogFragment.getView(). When touch points are outside DialogFragment, I am dismissing the Dialog.

    0 讨论(0)
  • 2020-12-04 17:56
        /** 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;
        }
    
    0 讨论(0)
  • 2020-12-04 18:05
                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 讨论(0)
  • 2020-12-04 18:06
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           ...
           getDialog().setCanceledOnTouchOutside(true);
           ... 
           }
    
    0 讨论(0)
  • 2020-12-04 18:08
    DialogFragment.getDialog().setCanceledOnTouchOutside(true);
    

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

    0 讨论(0)
  • 2020-12-04 18:08

    Lot of answers here but, the app crash when dialog opens. Writing getDialog().setCanceledOnTouchOutside(true); inside onCreateView did not work and crashed my app.

    (I am using AppCompatActivity as my BaseActivity and android.app.DialogFragment as my Fragment).

    What works is either of the two following lines:

    getDialog().setCanceledOnTouchOutside(true);

    OR

    this.getDialog().setCanceledOnTouchOutside(true);

    inside onActivityCreated like

    @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);

    throws following error

    And writing the code in onCreateView crashes the App! Please update the answer if you find something wrong.

    0 讨论(0)
提交回复
热议问题