19

I've been developing an app with data binding & MVVM.

I'm trying to use an alternative layout for my app on landscape mode. I have:

layout/fragment_content.xml
layout-land/fragment_content.xml

Both layouts have same views with different look, and get feeds from same view models, like this:

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

<data class="MyBinding">

    <variable
        name="viewModel"
        type="com.myapp.package.viewModel.VMFirst"/>

    <variable
        name="controlModel"
        type="com.myapp.package.viewModel.VMSecond"/>
</data>

<DIFFERENT CONTENT HERE>

All the views and id's exist in both layouts.

Well, problem is, it doesn't compile, error is simply "cannot find symbol method getViewModel" and getter for the other variable.

What I tried so far:

  1. Using layout and layout-land folders ( Failed, error is explained above )

  2. Using layout aliases Use Layout Aliases which I found here Issue 199344: Data binding does not work with layout aliases. I didn't change anything in xml files while trying this approach. This also failed, error is Could not write to com.myapp.package.databinding.MyBinding

Is it not possible to use data binding data tag in multiple layout files ? What should I use to use different layouts for different states while using data binding ? Thanks !

Edit: deleting class="MyBinding" did not change errors.

3
  • 1
    Can you post your VMFirst or your VMSecond class?
    – yennsarah
    Commented Sep 13, 2016 at 6:01
  • Did you find a solution to this? I'm having the exact same problem. I have a standard layout file and a sw720dp file and I get the exact problem.
    – jlively
    Commented Dec 21, 2016 at 14:51
  • Why don't you simply create a third layout file like base_data_xyz containing the <data> but no views, then <include> it in both layouts.
    – Ali Kazi
    Commented Dec 10, 2018 at 2:22

3 Answers 3

7

If anyone searches for this question, after 2 years I tried to do the same, and I saw it's working all fine now.

I created a layout file activity_main under layout and layout_sw600dp. Here's the layout under layout resources:

<layout 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">

    <variable
        name="small_variable"
        type="Integer"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/myRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <View
            android:id="@+id/small_square"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@android:color/holo_blue_bright"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

This one is the layout under layout_sw600dp folder:

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

<layout 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">

    <variable
        name="big_variable"
        type="Long"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/myRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <View
            android:id="@+id/big_square"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@android:color/holo_blue_bright"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Both has a view but it has different id in each: small_square and big_square.

I run the project on phone & tablet. Here are my findings:

  • DataBinding creates an implementation that contains ALL views and variables under all layout files of same name in different layout folders.
  • Views that exists in all layouts are not nullable, all others are nullable. In above XML's, myRoot is not a nullable view when using binding from Kotlin, while big_square and small_square are nullable views. Variables are nullable whether or not they exists in all layouts ( which is expected behaviour ).
  • You cannot name binding classes different in each file. It has to be same ( MainBinding in above examples, or if you don't define it LayoutResourceName + Binding by default ).
  • Names for views and variables on binding implementation are camel case. So my small_variable & small_square was binding.smallVariable and binding.smallSquare on code side.
  • With Kotlin, you can just use views like binding.bigSquare?.operation, which is great that you don't need to check if it's tablet or phone or view is null or not beforehand.
  • Just a tip, you can assign binding fields even if layout that they are in won't be used. You can still say binding.smallVariable = 3 on code and it'll do the assignment and save the value. I think it's good to be careful.
1
  • Thanks for this insight! Commented Feb 18, 2019 at 11:56
2

I heavily use MVVM in my apps and am also building a library around it.

I follow the convention that there is a single ViewModel in every XML. Also, the name of the viewmodel variable is same in all XMLs.

So, in your case, you can create another ViewModel class that contains VMFirst and VMSecond.

public class ParentVM {
   VMFirst first;
   VMSecond second;
}

Both the XMLs (portrait and landscape) will have same names, say activity_main.xml.

<layout>
    <data>
      <variable 
          type="ParentViewModel"
          name="vm"/>
    </data>

Then no check is required in MainActivity code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.setVariable(BR.vm, new ParentViewModel());
}

This works.

Advantages of single ViewModel

In fact, because I follow same variable name throughout all xmls, I am able to include the binding logic in a base class MvvmActivity itself. So, all my activities look like:

public class MainActivity extends MvvmActivity {

    @NonNull
    @Override
    protected ViewModel createViewModel() {
        return new MainViewModel();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}

MvvmActivity implementation: MvvmActivity.java

Another advantage of keeping a constant data binding variable is that you can setup RecyclerView or ViewPager adapters in XML itself. See Setup RecyclerView from XML for more details.

0

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing "Binding" to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding. --Binding Data

and generated at compile time.

so, select different layout by java code.

layout/
R.layout.activity_main
R.layout.activity_main_tablet

values/
    <bool name="is_mobile">true</bool>
    <bool name="is_tablet">false</bool>
values-w820dp/
    <bool name="is_mobile">false</bool>
    <bool name="is_tablet">true</bool>



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(getResources().getBoolean(R.bool.is_mobile)) {
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);            
    } else {
        ActivityMainTabletBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main_tablet);   
    }

}
1
  • True, but what I seek is the correct way of using data binding with different layout varieties for same view. As far as I tried changing binding name does not solve this error. Approach that same page suggests ( layout aliases) also not working. I don't see the point of using different layouts if I'll check the device size and inflate with an if / else block. Do you know / came across any working code examples for this ?
    – Mel
    Commented Sep 26, 2016 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.