BottomSheetBehaviour: The view is not associated with BottomSheetBehavior

Harish Vishwakarma picture Harish Vishwakarma · Nov 19, 2016 · Viewed 17.9k times · Source

I am using Google Design Support Library 25.0.0 In my activity I have a relative layout with

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

Now when I reference it for adding BottomSheetBehaviour, I get the error

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior

Here is the layout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/maps_colayout"
xmlns:app="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
android:background="@color/white">

...

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="280dp"
    android:layout_gravity="bottom"
    android:id="@+id/rl_bottomsheet"
    android:background="#F3F3F3"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    ...

</RelativeLayout>

And here is the activity relevant code:

CoordinatorLayout colayout = (CoordinatorLayout) findViewById(R.id.maps_colayout);
    View bottomSheet = colayout.findViewById(R.id.rl_bottomsheet);
    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });

Answer

Leslie picture Leslie · Mar 23, 2018

You will need to add this line within your linear layout tag. The one that is being referenced.

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

or for androidx

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"