Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 945
iamthevoid Avatar asked Jul 07 '17 09:07

iamthevoid


People also ask

How do you set different gravity for TextInputLayout hint and text in Android?

To set a different gravity for the EditText , don't set any gravity attributes in the layout, then set the EditText 's gravity programmatically, in your code. That is, after setContentView() , use findViewById() to get the EditText , then call setGravity(Gravity. CENTER_HORIZONTAL) on it.

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it. Works back and forth.

What is the use of TextInputLayout in Android?

The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.

How do I remove TextInputLayout padding?

By default, some space is reserved for assistive labels below SfTextInputLayout control. These reserved spaces can be removed by setting ReserveSpaceForAssistiveLabels property as false when helper and error text are empty and ShowCharCount is false.


Video Answer


2 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>
like image 180
FerdyRod Avatar answered Dec 06 '22 08:12

FerdyRod


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

like image 34
Guilherme V. Avatar answered Dec 06 '22 06:12

Guilherme V.