Float Label hint (TextInputLayout) not works with Android Data Binding

Viewed 1904

I need create fields edit page in app. There are list of EditText for edit Contact. For each field type i have an layout. As example - for name and last name it is float labeled edit text. For facebook - simple edit text with icon. I am creating this list with Android Data Binding Library

I've created layout

<data>

    <variable
        name="item"
        type="app.core.db.model.Field" />
</data>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="65dp"
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
        app:theme="@style/EditFieldFloatLabeled">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:hint="@{item.getHint()}"
            android:imeOptions="actionNext"
            android:maxLines="1" />
    </android.support.design.widget.TextInputLayout>

</FrameLayout>

But float label not works. I starts commenting line by line to find out reason. Float label became work when I've commented line

android:hint="@{item.getHint()}

(And replace it with hardcoded text). getHint() returns R.string.{something} based on field type.

There I've found out than programmatically setting hint produce float label disappearing. I can not use static layout (instead recycler view) because fields list can increase dynsmically (ex: I am writing phone number in field and empty Phone field inserts after it for other phone number).

Is there way to create float label same way (by defining hint with field type)?

3 Answers

I had the same problem.

The solution is setting the hint with the data binding on the TextInputLayout.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:hint="@{item.getHint()}"
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
    app:theme="@style/EditFieldFloatLabeled">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:imeOptions="actionNext"
        android:maxLines="1" />
</android.support.design.widget.TextInputLayout>

The answer given by FerdyRod is the correct one.

The problem is that when TextInputLayout is being created it tries to read the hint from it's EditText and from this point forward it's not reading it ever again. TextInputLayout

    ...
    // If we do not have a valid hint, try and retrieve it from the EditText
    if (TextUtils.isEmpty(mHint)) {
        setHint(mEditText.getHint());
        // Clear the EditText's hint as we will display it ourselves
        mEditText.setHint(null);
    }

So, data binding is indeed updating the hint but the wrong one (EditText hint) that TextInputLayout doesn't read anymore

Related