BottomSheetBehavior The view is not a child of CoordinatorLayout
Asked Answered
U

4

9

I am trying to implement a persistent BottomSheet, my layout that is expected to be a BottomSheet is already a child of CoordinatorLayout but i don't know why i'm getting this Exception.

this is my BottomSheet Layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="10dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="?actionBarSize"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical"
        android:weightSum="3">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_weight="2"
            android:text="Order Details"
            android:textColor="#444"
            android:textSize="18dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:gravity="right"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="15dp"
            android:text="₹435.00"></TextView>
    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chicken Fried Rice 1x1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Paneer Tikka 1x2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Delivery Address"
        android:textColor="#444"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flat No 404, Skyline Apartments, Vizag - 500576" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#000"
        android:foreground="?attr/selectableItemBackground"
        android:text="PROCEED PAYMENT"
        android:textColor="#fff" />

</LinearLayout>

my activity layout :

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#efefef"
    tools:context=".activities.CityCouponActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content" />
    <!-- Adding bottom sheet after main content -->
    <include layout="@layout/bottom_sheet_filter" />

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

activity code:

LayoutInflater inflater = LayoutInflater.from(this);
View LayoutView = inflater.inflate(R.layout.bottom_sheet_filter, null, false);

mBottomSheetBehavior = BottomSheetBehavior.from(LayoutView);
/*....*/
Unamuno answered 31/3, 2018 at 16:36 Comment(0)
U
1

I collapsed by <merge> tag my modal sheet layout and changed to CoordinatorLayout top layout of activity's xml. After included bottomsheet on activity's layout. Then it worked.

Ungotten answered 19/4, 2020 at 12:44 Comment(0)
T
0

You can use bottomSheetDialog.getBehavior(); instead of BottomSheetBehavior.from(LayoutView);

Transgress answered 27/12, 2022 at 6:43 Comment(0)
M
-1

Well you have to use merge instead of include.

if you see BottomSheetBehavior.java source, you will see check:

ViewGroup.LayoutParams params = view.getLayoutParams();
        if (!(params instanceof CoordinatorLayout.LayoutParams)) {
            throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
        }

When you use include, it wraps your layout with additional one, so your layout params will not be instance of CoordinatorLayout.LayoutParams anymore.

Mcmillian answered 27/2, 2019 at 12:20 Comment(0)
S
-1

This is how I had to get bottom sheet behaviour to fix this issue

 dialog?.setOnShowListener { dialog ->
        val d = dialog as BottomSheetDialog
        val bottomSheetInternal =
            d.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        val bottomSheetBehaviour=BottomSheetBehavior.from(bottomSheetInternal)
       //use this behaviour
    }
Schutt answered 3/11, 2020 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.