Android: Data Binding: providing values in included layouts

1.9k Views Asked by At

I have the following layout - text_input_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="textType" type="String"/>
    </data>
    <TextInputLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        app:errorEnabled="true"
      android:textColorHint="@color/white"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <android.support.design.widget.TextInputEditText
            android:textColor="@color/White"
            android:backgroundTint="@color/White"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:inputType="@{textType}"
            android:id="@+id/password" />
    </TextInputLayout>
</layout>

I would like to include this layout in another layout but change the inputType to textPassword, textEmail etc depending on the requirement.

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

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

        <include layout="@layout/text_input_layout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
app:textType="textPassword"/>

    </RelativeLayout>
</layout>

I tried to use data binding but it does not seem to work. Could someone please help?

Thanks.

1

There are 1 best solutions below

1
On

textPassword is not a String. It is an attribute so you can not pass it like app:textType="textPassword". app:textType accept integer value.

Solution

1) Take textType as Integer value.

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

        <variable
            name="textType"
            type="Integer"/>
    </data>

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

        <android.support.design.widget.TextInputEditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="@{textType}"
            />
    </android.support.design.widget.TextInputLayout>
</layout>

2) Pass Integer to <include tag app:textType.

<?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.text.InputType"/>

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            layout="@layout/sample"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:textType="@{(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)}"
            />

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

You can also pass 1,2,3... without importing InputType but that should be valid InputType.

int TYPE_CLASS_TEXT = 1;
int TYPE_TEXT_VARIATION_PASSWORD = 128;