Prevent dismissal of BottomSheetDialogFragment on touch outside Prevent dismissal of BottomSheetDialogFragment on touch outside android android

Prevent dismissal of BottomSheetDialogFragment on touch outside


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

    BottomSheetDialogFragment bottomSheetDialogFragment = new SequenceControlFragmentBottomSheet();    bottomSheetDialogFragment.setCancelable(false);    bottomSheetDialogFragment.show(getChildFragmentManager(), bottomSheetDialogFragment.getTag());


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).

@Override public void onActivityCreated(Bundle savedInstanceState) {    super.onActivityCreated(savedInstanceState);    View touchOutsideView = 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.

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {    final BottomSheetDialog bottomSheetDialog =        (BottomSheetDialog) super.onCreateDialog(savedInstanceState);    bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {      @Override public void onShow(DialogInterface dialog) {        FrameLayout bottomSheet =        bottomSheetDialog.findViewById(android.support.design.R.id.design_bottom_sheet);        if (null != bottomSheet) {          BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);        behavior.setHideable(false);        }      }    });    return bottomSheetDialog;  }


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.