How to disable BottomSheetDialogFragment
dragging by finger?
I saw similar questions, but they\'re all about BottomSheet
not BottomSheetD
My version. It works perfectly.
Edit 09/04/2020: Replaced depreciated setBottomSheetCallback()
with addBottomSheetCallback()
class FragMenuBDrawer : BottomSheetDialogFragment() {
...
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener {
val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
...
}
This works for me sir
override fun onCreateDialog(savedInstanceState : Bundle?) : Dialog {
val dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener {
val bottomSheetDialog : BottomSheetDialog = it as BottomSheetDialog;
var bottomSheetBehavior = BottomSheetBehavior<FrameLayout>();
bottomSheetBehavior = bottomSheetDialog.getBehavior()
bottomSheetBehavior.setDraggable(false);
}
return dialog }
If you want to disable BottomSheetDialog
dragging, try to set setCancelable(false)
.
This is the Kotlin version of azizbekian's answer since someone asked about using data binding
@SuppressLint("RestrictedApi")
override fun setupDialog(d: Dialog?, style: Int) {
super.setupDialog(d, style)
dialogExampleBinding = DataBindingUtil
.inflate(LayoutInflater.from(context), R.layout.dialogExample, null, false) //This is for data binding only
d?.setContentView(R.layout.dialogExample)
val myDialog:BottomSheetDialog = d as BottomSheetDialog
val dField = myDialog.javaClass.getDeclaredField("behavior") //This is the correct name of the variable in the BottomSheetDialog class
dField.isAccessible = true
val behavior = dField.get(d) as BottomSheetBehavior<*>
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
This is my solution:
setOnShowListener {
Handler().post {
val bottomSheet = findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
bottomSheet?.let {
BottomSheetBehavior.from(it).state = STATE_EXPANDED
// Disable dialog dragging behavior which causes issue on EditText scroll!
BottomSheetBehavior.from(it).isDraggable = false
}
}
}
I get the answer here, I just added content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
to make Bottom Sheet Dialog Fragment height is match_parent and make it soft when it showed.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
// view hierarchy is inflated after dialog is shown
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
//this disables outside touch
d.getWindow().findViewById(R.id.touch_outside).setOnClickListener(null);
//this prevents dragging behavior
View content = d.getWindow().findViewById(R.id.design_bottom_sheet);
content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
((CoordinatorLayout.LayoutParams) content.getLayoutParams()).setBehavior(null);
}
});
return d;
}