How to disable BottomSheetDialogFragment
dragging by finger?
I saw similar questions, but they're all about BottomSheet
not BottomSheetDialogFragment
.
How to disable BottomSheetDialogFragment
dragging by finger?
I saw similar questions, but they're all about BottomSheet
not BottomSheetDialogFragment
.
There is simpler way of achieving the same after material design 1.2.0 was released.
When calling from BottomSheetDialogFragment
:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheetDialog.setOnShowListener {
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
if (bottomSheet != null) {
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
behavior.isDraggable = false
}
}
return bottomSheetDialog
}
Or with styling:
<style name="SomeStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="behavior_draggable">false</item>
</style>
And then in onCreate
of your dialog fragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.SomeStyle)
}
Having created MyActivity
as follows:
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
new MyBottomSheetFragment().show(getSupportFragmentManager(), "tag");
}
public static class MyBottomSheetFragment extends BottomSheetDialogFragment {
@Override
public void setupDialog(Dialog dialog, int style) {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialog;
bottomSheetDialog.setContentView(R.layout.sample);
try {
Field behaviorField = bottomSheetDialog.getClass().getDeclaredField("behavior");
behaviorField.setAccessible(true);
final BottomSheetBehavior behavior = (BottomSheetBehavior) behaviorField.get(bottomSheetDialog);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING{
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
Where R.layout.sample
is a simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#e479da" />
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#798de4" />
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#e4db79" />
</LinearLayout>
You'll get following output:
A part of solution is borrowed from this answer.
Too late but worth to share.
behavior.setDraggable(false)
This line did the job.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Disable dragging by set isDraggable to false
val bottomSheetDialog = dialog as BottomSheetDialog
val bottomSheetBehavior = bottomSheetDialog.behavior
bottomSheetBehavior.isDraggable = false
}
If you want to disable BottomSheetDialog
dragging, try to set setCancelable(false)
.
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
}
...
}
In your oncreateView
//Kotlin
val baseDialog = dialog
if (baseDialog is BottomSheetDialog) {
baseDialog.behavior.isDraggable = false
}
//If cancelable also not required.
isCancelable = 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) {}
})
}
Just Add bottomSheetBehavior.setHideable(false);
You can get Object of BottomSheetBehaviour
in BottomSheetDialogFragment
.
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
View parent = (View) view.getParent();
getHeight(view);
((BottomSheetBehavior) behavior).setFitToContents(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
bottomSheetBehavior.setHideable(false);
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 }
This is how I managed to fix it:
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
mBehavior.setHideable(false);
Top rated answer contains boilerplate code such as Field and try-catches.
So here is a better version of it in Kotlin:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState).apply {
setOnShowListener(::onShow)
}
}
private fun onShow(dialogInterface: DialogInterface) {
val dialog = dialogInterface as BottomSheetDialog
val frameLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
?: return
BottomSheetBehavior.from(frameLayout).apply {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING)
state = BottomSheetBehavior.STATE_EXPANDED
}
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
})
}
}
Use it in BottomSheetDialogFragment
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;
}
Simple solution
CoordinatorLayout.Behavior<View> behavior;
View profile_config_layout_bottom_sheet = findViewById(R.id.list_view_audience_profile_config_layout);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) profile_config_layout_bottom_sheet.getLayoutParams();
behavior = layoutParams.getBehavior();
assert behavior != null;
((BottomSheetBehavior<View>) behavior).addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
((BottomSheetBehavior<View>) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
});
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
}
}
}
my simple solution:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle)
dialog.behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
isDraggable = false
}
return dialog
}
They can also do this by setting up draggable, so here's another example of mine.
My BottomSheetDialogFragment contains a RecyclerView inside, if you need a BottomSheetDialogFragment banned drag up, can drag down, What I did is I set the RecyclerView to an absolute height
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="367dp"/>
If you want to just disable the dismissing of bottomsheet, you can simply set isCancelable false to fragment instance
val bSheet = BottomSheetFragment.newInstance("") bSheet.isCancelable = false bSheet.show(supportFragmentManager, "sheet")