Home » Android » android – How to set layout_gravity with databinding

android – How to set layout_gravity with databinding

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to set layout_gravity to a LinearLayout using data binding.
This is my layout:

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

    <data>

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

        <variable
            name="viewModel"
            type="com.example.viewModel.MessageViewModel" />
    </data>

    <LinearLayout
        android:id="@+id/messageAdapter_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="@{viewModel.isReceived ? Gravity.START : Gravity.END}"
            android:orientation="vertical"
            app:setBackground="@{viewModel.isReceived}">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{viewModel.content}"
                android:textColor="@color/primaryTextColorInv" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="@color/secondaryTextColorInv"
                android:textStyle="italic" />
        </LinearLayout>
    </LinearLayout>
</layout>

The nested LinearLayout should be aligned to START when viewModel.isReceived is true and to END when viewModel.isReceived is false but I got this error:

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:layout_gravity' with parameter type int on android.widget.LinearLayout.

Is possible to do this without using a @BindingAdapter?

How to&Answers:

I found the solution setting gravity to the root LinearLayout instead of setting layout_grarvity to the nested one:

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

    <data>

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

        <variable
            name="viewModel"
            type="com.example.viewModel.MessageViewModel" />
    </data>

    <LinearLayout
        android:id="@+id/messageAdapter_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="@{viewModel.isReceived ? Gravity.START : Gravity.END}"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:setBackground="@{viewModel.isReceived}"
            app:setMargin="@{viewModel.isReceived}">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{viewModel.content}"
                android:textColor="@color/primaryTextColorInv" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="@color/secondaryTextColorInv"
                android:textStyle="italic" />
        </LinearLayout>
    </LinearLayout>
</layout>