How to disable BottomSheetDialogFragment dragging
29,254
Solution 1
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.
Solution 2
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)
}
Solution 3
Too late but worth to share.
behavior.setDraggable(false)
This line did the job.
Solution 4
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
}
Solution 5
If you want to disable BottomSheetDialog
dragging, try to set setCancelable(false)
.
Author by
FarshidABZ
Updated on October 16, 2021Comments
-
FarshidABZ over 2 years
How to disable
BottomSheetDialogFragment
dragging by finger?I saw similar questions, but they're all about
BottomSheet
notBottomSheetDialogFragment
. -
FarshidABZ over 6 yearsThis is good sample code, but there are some issues. First, setupDialog isn't the BottomSheetDialogFragment method. Second, I have a recycler view in my bottom sheet view, and when I drag bottom sheet from recyclerView's blank spaces bottom sheet slides down.
-
azizbekian over 6 years
setupDialog()
is a method fromDialogFragment
class. Have substituted it withonCreateDialog()
. Cannot understand your use case. Can you post a simple project at github with that behavior? -
Ahmad Reza Enshaee over 5 yearsit works in my case, just had a problem where clicking on the outside field would become disabled after setting cancelable to false. thanks. +1
-
axita.savani about 5 yearsit's getting warning in my fragment: java.lang.NoSuchFieldException: No field mBehavior in class Landroid/support/design/widget/BottomSheetDialog; (declaration of 'android.support.design.widget.BottomSheetDialog' ) System.err: at java.lang.Class.getDeclaredField(Native Method)
-
Tamoxin about 5 yearsThe field's name is behavior, no mBehavior. That's why you are getting that error.
-
azizbekian about 5 yearsThanks, @MarcoCarrizales, updated answer from
mBehavior
tobehavior
. -
Tamoxin about 5 yearsI also changed the code to be in setupDialog() instead of onCreateDialog()
-
Tamoxin about 5 years@sagarsuri I just posted the Kotlin version with data binding of this answer. I hope you find it useful.
-
FarshidABZ over 4 yearsAnd how can we get bottomSheetBehavior object?
-
Dilavar Malek over 4 yearsplease check answer
-
Andrey Uglev over 4 yearswhat class has isDraggable?
-
Devendra Singh over 4 years
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
stackoverflow.com/questions/35618998/… -
C.Schone over 3 yearsThis is the correct answer and should have way more up votes!
-
lasec0203 over 3 years@AndreyUglev it comes from
BottomSheetDialog.behavior
. -
DIRTY DAVE over 3 yearsUsing the styling is way better than the other answers. Thanks!
-
Bugs Happen about 3 yearsAnswer by @jakubbialkowski below is better in my opinion.
-
Stephen Ruda almost 3 yearsThis is the correct answer. This method was added in a new version of the material library. I am using
com.google.android.material:material:1.3.0
and I have thesetDraggable()
method. I did not have the method when I was on version1.1.0
. It is in the documentation as well... developer.android.com/reference/com/google/android/material/… -
Saka almost 3 yearsisDraggable in Kotlin. if you use Java use setDraggable(boolean draggable)
-
Nishant Jalan over 2 yearsSimplest and most elegant.
-
Steffen Funke over 2 yearsNot too late, perfect timing :)