BottomSheetBehaviour: The view is not associated with BottomSheetBehavior
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"/>
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"
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.
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.