android-integrate databinding with BottomSheet

2.2k Views Asked by At

while integrating the BottomSheet we need to make the parent layout as the CoordinatorLayout but in databinding we use <layout>. While implementing this it throws an exception :-

Caused by: java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout.

How to integrate BottomSheet with databinding

<layout>

<data>

    <import type="android.view.View" />

    <variable
        name="cabLayoutBinder"
        type="newage.com.hopin.rideBooking.CabSelectActivity" />

    <variable
        name="modelBinder"
        type="newage.com.hopin.rideBooking.model.DataBinders" />

    <variable
        name="fareSetters"
        type="newage.com.hopin.rideBooking.model.FareDetails" />
</data>

<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"
    tools:context=".rideBooking.CabSelectActivity">

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

</layout>
2

There are 2 best solutions below

0
On

You put context in your coordinator, but in databinding you don't implement this way.

Try to remove this line and try again:

tools:context=".rideBooking.CabSelectActivity"

Let me know if it worked.

0
On

This is how I am using it

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".com.abc.Activity">

    <data>

        <variable
            name="viewModel"
            type=".com.abc.ViewModel" />
    </data>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <include
                layout="@layout/toolbar" />

        </FrameLayout>

        <!-- Adding bottom sheet after main content -->
        <include layout="@layout/bottom_sheet" />
    </android.support.design.widget.CoordinatorLayout>

</layout>