Disabling User dragging on BottomSheet

102,921

Solution 1

It can be now no longer relevant, but I will leave it here:

import android.content.Context
import android.util.AttributeSet
import androidx.coordinatorlayout.widget.CoordinatorLayout
import android.view.MotionEvent
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetBehavior

@Suppress("unused")
class LockableBottomSheetBehavior<V : View> : BottomSheetBehavior<V> {
    constructor() : super()
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    var swipeEnabled = true

    override fun onInterceptTouchEvent(
        parent: CoordinatorLayout,
        child: V,
        event: MotionEvent
    ): Boolean {
        return if (swipeEnabled) {
            super.onInterceptTouchEvent(parent, child, event)
        } else {
            false
        }
    }

    override fun onTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean {
        return if (swipeEnabled) {
            super.onTouchEvent(parent, child, event)
        } else {
            false
        }
    }

    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: V,
        directTargetChild: View,
        target: View,
        axes: Int,
        type: Int
    ): Boolean {
        return if (swipeEnabled) {
            super.onStartNestedScroll(
                coordinatorLayout,
                child,
                directTargetChild,
                target,
                axes,
                type
            )
        } else {
            false
        }
    }

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout,
        child: V,
        target: View,
        dx: Int,
        dy: Int,
        consumed: IntArray,
        type: Int
    ) {
        if (swipeEnabled) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
        }
    }

    override fun onStopNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: V,
        target: View,
        type: Int
    ) {
        if (swipeEnabled) {
            super.onStopNestedScroll(coordinatorLayout, child, target, type)
        }
    }

    override fun onNestedPreFling(
        coordinatorLayout: CoordinatorLayout,
        child: V,
        target: View,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        return if (swipeEnabled) {
            super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY)
        } else {
            false
        }
    }
}

And use it in your xml file:

app:layout_behavior="com.your.package.LockableBottomSheetBehavior"

It disables all users actions, it can be used when you want control BottomSheet only programmatically.

Solution 2

check state in onStateChanged method of setBottomSheetCallback if state is BottomSheetBehavior.STATE_DRAGGING then change it to BottomSheetBehavior.STATE_EXPANDED this way you can stop STATE_DRAGGING by user. like below

final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        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) {
            }
        });

use button to open close bottom sheet like below

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }
        });

don't use setPeekHeight or app:behavior_peekHeight

by above way you can reach your goal

Solution 3

implementation 'com.google.android.material:material:1.2.0-alpha05'

you can disable dragging the BottomSheet like this.

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val dialog = BottomSheetDialog(requireContext(), theme)
            dialog.setOnShowListener {
                setBottomSheetExpanded(dialog)
            }
            return dialog
        }
    
   open fun setBottomSheetExpanded(bottomSheetDialog: BottomSheetDialog) {
            val bottomSheet =
                bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
            bottomSheet?.let {
                val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
                val layoutParams = bottomSheet.layoutParams
                bottomSheet.layoutParams = layoutParams
                behavior.state = BottomSheetBehavior.STATE_EXPANDED
                behavior.isDraggable = false / true
            }
        
    }

Edited) The library was updated! you can use new library version

implementation 'com.google.android.material:material:1.4.0'

The examples are the same, good luck and good code

Solution 4

Alright, so the accepted answer didn't work for me. However, Виталий Обидейко's answer inspired my final solution.

First, I created the following custom BottomSheetBehavior. It overrides all of the methods involving touch, and returns false (or did nothing) if it is locked. Otherwise, it acts like a normal BottomSheetBehavior. This disables the user's ability to drag down, and does not affect changing the state in code.

LockableBottomSheetBehavior.java

public class LockableBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {

    private boolean mLocked = false;

    public LockableBottomSheetBehavior() {}

    public LockableBottomSheetBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setLocked(boolean locked) {
        mLocked = locked;
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
        boolean handled = false;

        if (!mLocked) {
            handled = super.onInterceptTouchEvent(parent, child, event);
        }

        return handled;
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
        boolean handled = false;

        if (!mLocked) {
            handled = super.onTouchEvent(parent, child, event);
        }

        return handled;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes) {
        boolean handled = false;

        if (!mLocked) {
            handled = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
        }

        return handled;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed) {
        if (!mLocked) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
        if (!mLocked) {
            super.onStopNestedScroll(coordinatorLayout, child, target);
        }
    }

    @Override
    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY) {
        boolean handled = false;

        if (!mLocked) {
            handled = super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
        }

        return handled;

    }
}

Here's an example of how to use it. In my case, I needed it so the Bottom Sheet locked when expanded.

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|snap"
            app:titleEnabled="false"/>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
    </android.support.design.widget.AppBarLayout>

    <!-- Use layout_behavior to set your Behavior-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="com.myapppackage.LockableBottomSheetBehavior"/>

</android.support.design.widget.CoordinatorLayout>

HomeActivity.java

public class HomeActivity extends AppCompatActivity {
    BottomSheetBehavior mBottomSheetBehavior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setAdapter(new SomeAdapter());

        mBottomSheetBehavior = BottomSheetBehavior.from(recyclerView);
        mBottomSheetBehavior.setBottomSheetCallback(new MyBottomSheetCallback());
    }

    class MyBottomSheetCallback extends BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                if (mBottomSheetBehavior instanceof LockableBottomSheetBehavior) {
                    ((LockableBottomSheetBehavior) mBottomSheetBehavior).setLocked(true);
                }
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
    });
}

Hope this helps clear up a lot of the confusion!

Solution 5

I ended up writing a workaround to address this use case of dynamically disabling user dragging, whereby BottomSheetBehavior is subclassed to override onInterceptTouchEvent, and to ignore it when a custom flag (in this case mAllowUserDragging) is set to false:

import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class WABottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {
    private boolean mAllowUserDragging = true;
    /**
     * Default constructor for instantiating BottomSheetBehaviors.
     */
    public WABottomSheetBehavior() {
        super();
    }

    /**
     * Default constructor for inflating BottomSheetBehaviors from layout.
     *
     * @param context The {@link Context}.
     * @param attrs   The {@link AttributeSet}.
     */
    public WABottomSheetBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAllowUserDragging(boolean allowUserDragging) {
        mAllowUserDragging = allowUserDragging;
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
        if (!mAllowUserDragging) {
            return false;
        }
        return super.onInterceptTouchEvent(parent, child, event);
    }
}

And in your layout xml:

    <FrameLayout
        android:id="@+id/bottom_sheet_frag_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_hideable="true"
        app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
        app:elevation="@dimen/bottom_sheet_elevation"
        app:layout_behavior="com.example.ray.WABottomSheetBehavior" />

So far, this is the most consistently behaving solution for disabling user dragging on the Bottom Sheet on demand.

All of the other solutions that relied on firing another setState call in the onStateChanged callback resulted in the BottomSheet getting into a bad state, or causes significant UX issues (in the case of posting the setState call in a Runnable).

Hope this helps someone :)

Ray

Share:
102,921
Tonespy
Author by

Tonespy

Updated on January 28, 2022

Comments

  • Tonespy
    Tonespy over 2 years

    I am trying to disable user dragging on BottomSheet. The reason I want to disable is two things. 1. It's preventing the ListView from scrolling downward, 2. I don't want users to dismiss using dragging but with a button on the BottomSheetView. This is what I've done

     bottomSheetBehavior = BottomSheetBehavior.from(bottomAnc);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                    //Log.e("BottomSheet", "Expanded");
                } else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    //Log.e("BottomSheet", "Collapsed");
                }
            }
    
            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                // React to dragging events
                bottomSheet.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = MotionEventCompat.getActionMasked(event);
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                return false;
                            default:
                                return true;
                        }
                    }
                });
            }
        });
    

    The bottomSheetLayout

        <?xml version="1.0" encoding="utf-8"?><FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    android:id="@+id/bottomSheet">
    
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:elevation="10dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical">
    
                <TextView
                    android:id="@+id/text1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Order Items"
                    android:layout_margin="16dp"
                    android:textAppearance="@android:style/TextAppearance.Large"/>
    
    
                <Button
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/bg_accept"/>
    
                <Button
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="8dp"
                    android:background="@drawable/bg_cancel"/>
    
            </LinearLayout>
    
            <ListView
                android:id="@+id/item_edit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:divider="@color/md_divider_black"
                android:dividerHeight="1dp"/>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

  • Tonespy
    Tonespy about 8 years
    Nice trick. Didn't notice that. Thanks. And also, can you help with this. When I tell it to expand at first, it's transparent and I can see the view behind, but I can't interact not until I tap on the EditText in the SheetView before making it visible.
  • Tonespy
    Tonespy about 8 years
    I made my BottomSheet View match_parent and whenever I try to bring it up in my Activity I noticed it slides up, but it's not visible not until I tap the EditText in it which brings up the Keyboard and make the BottomSheet View visible
  • Tonespy
    Tonespy about 8 years
    Yeah. It's like it's active but totally transparent and not visible but responds to touch.
  • Dhaval Parmar
    Dhaval Parmar about 8 years
    for this you need bottom sheet layout have more content other wise its not show you as match_parent. I don't think its work as what you want.
  • Tonespy
    Tonespy about 8 years
    Let me tell you the problem with throwing it in a runnable unless that's what you want. You can't dismiss it with a button because, it needs to drag to dismiss. And, it'll always respond to dragging, just that it would prevent the user from dragging to dismiss
  • Gokhan Arik
    Gokhan Arik about 8 years
    I tried this, but the states ends up in STATE_SETTLING. I have a button to open and close bottom sheet, if it is HIDDEN, I expand it. If it is EXPANDED, I hide it. Since it gets stuck in SETTLING, my button doesn't work after dragging bottom sheet. Any idea on that?
  • Ray W
    Ray W about 8 years
    This solution is unreliable; the bottom sheet gets into a bad state, as Gokhan said... and when in that bad state, calls like loading a new fragment into the bottom sheet will simply blank out.
  • Odys
    Odys about 8 years
    That's pretty neat
  • Beeing Jk
    Beeing Jk almost 8 years
    Good solution but it doesn't work when there is a listview in the bottom sheet.
  • Afzal N
    Afzal N over 7 years
    @BeeingJk Instead of the FrameLayout, use NestedScrollView, and set bottomSheetFragContainer.setNestedScrollingEnabled(false);
  • LOG_TAG
    LOG_TAG over 7 years
    How make it usable for MapView (to avoid zooming problems )
  • LOG_TAG
    LOG_TAG over 7 years
    @AfzalivE it will work for MapView in BootomSheetFragment?
  • LOG_TAG
    LOG_TAG over 7 years
    SOLVED: by setting callback CoordinatorLayout.Behavior behavior = layoutParams.getBehavior(); if (behavior != null && behavior instanceof BottomSheetBehavior) { ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallbac‌​k); }
  • murt
    murt over 7 years
    I use this apporach in order to manage simultanously 4 bottom sheet at once. The problem I have is when i disable onInterceptTouchEvent() in all MyBottomSheetBehaviour the last BottomSheet is still draggable
  • murt
    murt over 7 years
    This is the best answer, for disabling BottomSheetBehaviour. A man above also posted similiar solution, but he didn't write to override others event like onTouchEvent(). In other hand you may improve your answer if you put a flag instead of false
  • Vitalii Obideiko
    Vitalii Obideiko over 7 years
    @murt, thanks. I posted this because in some cases other answers give me wrong behavior. Only if override all method we can obtain useful result. I think that who needs the flag can easy add it :)
  • Thorvald
    Thorvald over 7 years
    This doesn't wok for me ! PS : I have a scrollable text in the bottomsheet
  • Hammad Nasir
    Hammad Nasir over 7 years
    this also seems to be helpful: stackoverflow.com/a/38723483/6144372
  • Tunga
    Tunga over 7 years
    This solution was appealing but bizarrely causes the bottom sheet to appear from the top of the screen instead of the bottom! It disappears the normal way however. It's very Star Trek.
  • Leonid Ustenko
    Leonid Ustenko about 7 years
    How do you cast it during initialisation? This gives me warning WABottomSheetBehavior<View> behaviour = (WABottomSheetBehavior) BottomSheetBehavior.from(sheetView);
  • ᴛʜᴇᴘᴀᴛᴇʟ
    ᴛʜᴇᴘᴀᴛᴇʟ about 7 years
    @ВиталийОбидейко this is great! thank you! i tried all answer. Only this worked. Thanks a lot!
  • eRaisedToX
    eRaisedToX about 7 years
    This works for persistent bottom sheets of mine but is not working for modal bottom sheets..Any idea why?
  • HolySamosa
    HolySamosa over 6 years
    I needed to make a sight modification and instead use BottomSheetBehavior.STATE_HIDDEN. In such case, you must also not call setPeekHeight(). This is much less complicated than other solutions here.
  • Kailas Bhakade
    Kailas Bhakade over 6 years
    It worked for me. Thanks, But little bit doubt about that Is it correct way?
  • Dhaval Parmar
    Dhaval Parmar over 6 years
    there is not correct way to do something. whatever work for you choose it. and be prepare to change.
  • Vivek Kumar Srivastava
    Vivek Kumar Srivastava over 6 years
    this one is a perfect answer
  • Tấn Nguyên
    Tấn Nguyên about 6 years
    Nice, it's best answer that we can avoid workaround these states which lead to miss events. Thank you.
  • user3144836
    user3144836 about 6 years
    How do you use this class? I'm getting an IllegalArgumentException: The view is not associated with BottomSheetBehavior
  • user3144836
    user3144836 about 6 years
    How do you use this class? I'm getting an IllegalArgumentException: The view is not associated with BottomSheetBehavior
  • user3144836
    user3144836 about 6 years
    How do you use this with a BottomSheetFragment ?
  • Rishabh Chandel
    Rishabh Chandel about 6 years
    It will not work if you have nestedscrollview inside bottom sheet
  • Metwalli
    Metwalli about 6 years
    app:layout_behavior="UserLockBottomSheetBehavior"> in xml and then in code you do the following. // get the bottom sheet view LinearLayout llBottomSheet = (LinearLayout) findViewById(R.id.bottom_sheet); // init the bottom sheet behavior BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet);
  • Steve
    Steve almost 6 years
    You need to specifically refer to this class in your XML. app:layout_behavior="com.my.package.UserLockBottomSheetBehav‌​ior"
  • Adarsh Yadav
    Adarsh Yadav over 5 years
    @James - Nice answer but now I am not able to setPeekHeight(). Any idea?
  • Deepak Joshi
    Deepak Joshi over 5 years
    In some cases, this still doesn't work, if we have a list in bottom sheet fragment, it still drags
  • Vitalii Obideiko
    Vitalii Obideiko over 5 years
    @DeepakJoshi this solution was for static content. Need to look into scrollable content problem
  • Vitalii Obideiko
    Vitalii Obideiko over 5 years
    @DeepakJoshi what did you use Recycler or smth else?
  • Deepak Joshi
    Deepak Joshi over 5 years
    @ВиталийОбидейко, Yes, I used recycler view and when I try to scroll down on the area where recycler view is there, the parent bottom sheet also starts dragging.
  • Vitalii Obideiko
    Vitalii Obideiko over 5 years
    @DeepakJoshi I think it is because of developer.android.com/reference/android/support/v7/widget/… implements developer.android.com/reference/android/support/v4/view/…. But how u could fix it I can't suggest right now
  • Vitalii Obideiko
    Vitalii Obideiko over 5 years
    @DeepakJoshi maybe u may extends of RecyclerView and override few methods like 'hasNestedScrollingParent', but I am not sure
  • Deepak Joshi
    Deepak Joshi over 5 years
    @ВиталийОбидейко, thanks for the quick reply, currently I have changed the approach, but I will surely try this.
  • Usman Rana
    Usman Rana over 5 years
    Sometimes it stucks and stay locked even dragging is allowed
  • AdamHurwitz
    AdamHurwitz over 5 years
    This does not disable swiping. It collapses the bottom sheet completely.
  • AdamHurwitz
    AdamHurwitz over 5 years
    This is not ideal as it may cause unexpected animations.
  • AdamHurwitz
    AdamHurwitz over 5 years
    This causes unexpected animation when the Bottom Sheet is attempting to close.
  • pz64_
    pz64_ over 5 years
    In my case. It didn't caused any animations issue at all. It just don't move after card is expanded. It's not ideal but it did work as expected!
  • AdamHurwitz
    AdamHurwitz over 5 years
    Interesting, that could be the case. I resolved the issue with my Bottom Sheet closing unexpectedly by setting the CollapsingToolbarLayout's Toolbar to Invisible or Gone when the Bottom Sheet is open. A touch interaction related to the Toolbar even though it was underneath was causing the Bottom Sheet to close unexpectedly. The issue is fixed now.
  • Sup.Ia
    Sup.Ia over 5 years
    I tried this. it works for me. thanks bro for saving my ass
  • Ajay
    Ajay over 5 years
    This is a good workaround, although it is not updated as of today. The OnNestedPreScroll and certain other methods have been deprecated. Need to update those methods and it works just fine.
  • Debdeep
    Debdeep about 5 years
    @DeepakJoshi You might have certainly used SwipeRefreshLayout. Exclude that and bottom sheet won't be dragging down on RecyclerView up scroll.
  • Deepak Joshi
    Deepak Joshi about 5 years
    @Debdeep, I was using RecyclerView, now I have changed my use case.
  • Antroid
    Antroid about 5 years
    this trick is somewhat irrelevant if user experience is concerned, as it makes the bottom sheet behaviour abnormal. The sheet collapses when user taps anywhere .when the sheet's state is : Expanded
  • Abhishek
    Abhishek about 5 years
    This doens't work. Follow the previous answer where it has been shown how to override other methods.
  • florian-do
    florian-do about 5 years
    Hello, it doesn't work on a BottomSheetDialogFragment, i can still drag the bottomsheet
  • Eric Bachhuber
    Eric Bachhuber almost 5 years
    Nice concise solution. To anyone reading this, you'll (probably) want additional checks for event.isCanceled() and event.getAction() == MotionEvent.ACTION_UP before dismissing the dialog -- this will prevent mis-clicks from firing the dismissal.
  • Aman Verma
    Aman Verma over 4 years
    setBottomSheetCallback is deprecated API 28 not working crashing
  • zoha131
    zoha131 over 4 years
    What is the negative effect of using setPeekHeight ? I have set behavior.peekHeight = Int.MAX_VALUE and it's working for me. I am not sure if it has any side effects.
  • AVJ
    AVJ over 4 years
    Thanks for this. This is the simplest solution to disable dragging.
  • Aman Verma
    Aman Verma over 4 years
    @RishabhChandel did you find the solution yet?
  • Quentin vk
    Quentin vk over 4 years
    this works if you set var swipeEnabled = false for recyclerview in bottomsheet as well.
  • Pranav P
    Pranav P over 4 years
    Very solutions. But not sure why it did not work for me. Any thoughts @VitaliiObideiko I am using Viewpager with recyclerView. :(
  • Tyler
    Tyler about 4 years
    Works if you swap out the deprecated methods for the ones that include the extra type int parameter
  • Yuri Popiv
    Yuri Popiv about 4 years
    it doesn't work with NestedScrollView @RishabhChandel you saved my day! Many thanks!!!
  • Gastón Saillén
    Gastón Saillén about 4 years
    For some reason, I cant close the dialog touching outside, but it works to disable the drag
  • Fayçal
    Fayçal almost 4 years
    it's for iOS here not Android
  • Nirav Joshi
    Nirav Joshi almost 4 years
    @VitaliiObideiko how to change swipeEnabled value dynamically ?
  • Vitalii Obideiko
    Vitalii Obideiko almost 4 years
    @NiravJoshi just add public method where you may set value to it
  • Bob
    Bob almost 4 years
    In case of netstedscrollview, also set isNestedScrollingEnabledto false
  • Isuru Bandara
    Isuru Bandara over 3 years
    This method does not work if you are using nestedscrollview, It is working with Scrollview.
  • DavidHyogo
    DavidHyogo over 3 years
    The question is very detailed with plenty of relevant code to explain the problem, but it's hard to see the connection between your answer and the question because you haven't added any explanation to your code sample.
  • Ram Chhabra
    Ram Chhabra over 3 years
    @DavidHyogo sorry for no explanation I added.. but as in the question user wanted to disable bottom sheet dragging property so I suggested to use setCancelable(false); method of bottom sheet
  • sweet_vish
    sweet_vish about 3 years
    I am getting below issue- Could not inflate Behavior subclass com.rp.th.sit.order.cart.presentation.view.cartutil.Lockable‌​BottomSheetBehavior why is it?
  • Gabriel Trifa
    Gabriel Trifa almost 3 years
    Thank you! This worked for me. Just one comment: should be used sheetBehavior.addBottomSheetCallback instead of setBottomSheetCallback, which is deprecated.
  • Hank Chan
    Hank Chan almost 3 years
    This seriously needs to be the default answer. I've wasted precious time to try all the solutions before this one, and none compares with it.
  • Burak
    Burak over 2 years
    This worked perfect for me. Thank you!
  • Abdullah
    Abdullah over 2 years
    it works perfect but setBottomSheetCallback depreciated! please use addBottomSheetCallback
  • Pradeep Simba
    Pradeep Simba over 2 years
    I can't understand this answer. So , please explain in detail.