27

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
        }
    });
Harish Vishwakarma
  • 480
  • 1
  • 5
  • 14

4 Answers4

42

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"
Mussa
  • 1,463
  • 21
  • 25
Leslie
  • 522
  • 5
  • 7
  • 14
    This solved it for me, just a note that since they migrated to AndroidX packages the name required is: `app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"` – Henrik Gyllensvärd Nov 19 '18 at 23:14
11

You have wrong app namespace declared. Replace line:

xmlns:app="http://schemas.android.com/tools"

with

xmlns:app="http://schemas.android.com/apk/res-auto"

in your CoordinatorLayout declaration in the layout file.

The tools namespace is used to provide additional information about the layout to "tools" (for example IDE). This information is stripped from the app.

On the other hand the app namespace is a global namespace for all the custom (that is, not declared by the Android system) attributes, either declared by you or by imported libraries (the design support library is your case). These are included in your app.

So what is actually tools namespace good for? The most common usage is to better render your layout's preview. Let's for example assume you have a TextView, which should be initially empty and filled in later.

You can add an attribute to your TextView declaration:

tools:text="Some example text here"

This text is not going to be displayed in your app. However, the layout preview rendered in your Android Studio will have it, so you can see what to expect on your mobile device.

Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19
6

I hope to help someone else... I had mi bottom_sheet.xml like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical"
    android:padding="16dp"
    app:behavior_hideable="false"
    app:behavior_peekHeight="90dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
    .....
    
</LinearLayout>

and then I Include my bottom_sheet.xml to my activity.xml like:

<include
layout="@layout/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

But that not works. I had the same error: "The view is not associated with BottomSheetBehavior"

I put the behavior properties in the Include, not at the botton and it worked:

<include
            layout="@layout/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="false"
            app:behavior_peekHeight="90dp"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>
Luis Moreno
  • 615
  • 6
  • 6
3

First, If using androidX, then "app:layout_behavior="@string/bottom_sheet_behavior"" or app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

Second, View with bottomsheet behavior has to be direct child of coordinator layout.

Last, Remove layout_width and layout_height attribute of that element if it is Do it programatically, if needed.

Max Droid
  • 924
  • 9
  • 10