aksh1618
aksh1618

Reputation: 2571

Android Data Binding for alternative layouts declares differing root layout as View

I am using Data Binding for the very first time. Everything was working as expected till I decided to create separate layout for landscape mode. Now, I have two layouts for main activity, respectively for portrait and landscape modes:

activity_main.xml:

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

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--Some Views-->

    </LinearLayout>
</layout>

activity_main.xml (land):

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

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--Some Views-->

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

The problem is that I am unable to use binding.mainLayout as a ViewGroup because it is declared as a view in ActivityMainBinding.java:

public final android.view.View mainLayout;

Shouldn't it be declared as a ViewGroup, seeing that both LinearLayout and ConstraintLayout directly extend ViewGroup? Am I doing something wrong? Is there a logical reason behind this or is it just an oversight?

Upvotes: 1

Views: 2240

Answers (1)

Kamil
Kamil

Reputation: 346

Together with ActivityMaindBinding, you should get autogenerated implementation for default & land layouts. They should be called ActivityMainBindingImpl and ActivityMainBindingLandImpl respectively.

In the runtime you can check with instanceof operator which one of the implemenations is in the use and access mainLayout with a proper type.

Upvotes: 3

Related Questions