Skip to content Skip to sidebar Skip to footer

Prevent Dismissal Of Bottomsheetdialogfragment On Touch Outside

I have implemented a BottomSheet Dialog and I want to prevent the bottomsheet from dismissing when the user touches outside of the bottomsheet when it's peeking (Not fully expanded

Solution 1:

You should use #setCancelable(false) when you create an instance of it.

BottomSheetDialogFragmentbottomSheetDialogFragment=newSequenceControlFragmentBottomSheet();
    bottomSheetDialogFragment.setCancelable(false);
    bottomSheetDialogFragment.show(getChildFragmentManager(), bottomSheetDialogFragment.getTag());

Solution 2:

setCancelable(false) will prevent the bottom sheet dismiss on back press also. If we look at the layout resource for the bottom sheet in android design support library, there is a View component with ID touch_outside and there is an OnClickListener set in method wrapInBottomSheet of BottomSheetDialog, which is used for detecting clicks outside and dismiss the dialog. So, to prevent cancel on touch outside the bottom sheet we need to remove the OnClickListener.

Add these line to onActivityCreated method (or any other life cycle method after onCreateView).

@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ViewtouchOutsideView= getDialog().getWindow()
        .getDecorView()
        .findViewById(android.support.design.R.id.touch_outside);
    touchOutsideView.setOnClickListener(null);
}

Also if you want to prevent the bottom sheet dismiss by swiping down, change the bottom sheet dialog behaviour Hideable false. To setHideable(false) add the following code to the onCreateDialog method.

@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
    finalBottomSheetDialogbottomSheetDialog=
        (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

    bottomSheetDialog.setOnShowListener(newDialogInterface.OnShowListener() {
      @OverridepublicvoidonShow(DialogInterface dialog) {
        FrameLayoutbottomSheet=
        bottomSheetDialog.findViewById(android.support.design.R.id.design_bottom_sheet);

        if (null != bottomSheet) {
          BottomSheetBehaviorbehavior= BottomSheetBehavior.from(bottomSheet);
        behavior.setHideable(false);
        }
      }
    });
    return bottomSheetDialog;
  }

Solution 3:

all of the above answers are a little complex in case of simple bottom sheet dialog Just Use cancellable As it prevents the scrolling and clicking outside of the Dialog.

mBottomSheetDialog.setCancelable(false)
mBottomSheetDialog.setCanceledOnTouchOutside(false)

just use it for simple Bottom Sheet Dialog. it worked for Me.

Solution 4:

Simplest way is to set setCanceledOnTouchOutside to false on the BottomSheetDialogFragment's dialog. with Kotlin,

classXFragment : BottomSheetDialogFragment() {

    overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCanceledOnTouchOutside(false)
    }

}

Solution 5:

The answer by M. Erfan Mowlaei is useful but I was looking for a way to enforce this behavior every time an instance of the class is created without having to remember to call setCancelable(false). See below.

classMyBottomSheet : BottomSheetDialogFragment() {

companionobject {
    funnewInstance() = MyBottomSheet().apply {
        isCancelable = false
      }
   }
}

Post a Comment for "Prevent Dismissal Of Bottomsheetdialogfragment On Touch Outside"