Search code examples
androiddata-bindingandroid-databinding

How to get included XML binding in DataBinding


I have a DataBinding class ActivityMainBinding where the XMl looks like this

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    <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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add" />

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

And the content_main.xml is another binding

    <?xml version="1.0" encoding="utf-8"?>
    <layout>

        <android.support.constraint.ConstraintLayout 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=".MainActivity"
            tools:showIn="@layout/activity_main">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:scrollbars="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </layout>

Now, in my activity I set the content view like this.

        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

Now I can access all XML elemets using this binding. like

        Toolbar toolbar = activityMainBinding.toolbar;            

but some of my widgets are in the included layout which is ContentMainBinding

Now, how can I create a ContentMainBinding instance from ActivityMainBinding ?

I tried

        contentMainBinding = DataBindingUtil.getBinding(activityMainBinding.container);

But, its crashing with error

         ActivityMainBindingImpl cannot be cast to ContentMainBinding                 

Solution

  • There is no need to add <layout>----</layout> tag in your content_main.xml file. You need to set id of included layout like :

     <?xml version="1.0" encoding="utf-8"?>
        <layout>
        <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:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
    
            <include layout="@layoutcontent_main" android:id="@+id/contentMain" />
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                app:srcCompat="@drawable/ic_add" />
    
        </android.support.design.widget.CoordinatorLayout>
        </layout>
    

    Then you can directly access included layout view using

    ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    activityMainBinding.contentMain.recyclerView.setAdapter();