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

梦想的初衷 提交于 2019-12-10 12:56:21

问题


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


回答1:


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>



回答2:


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




回答3:


The problem is that when nothing is assigned, 0 will be given as the resource ID. you're probably seeing an exception like this in your logcat:

android.content.res.Resources$NotFoundException: String resource ID #0x0

It is likely that you haven't assigned a value to the item before the first evaluation of the expression. You should assign the item immediately after creating the binding if you want to ensure that the resource isn't set to the value 0.

MyBinding binding = MyBinding.inflate(inflater, parent, true);
binding.setItem(item);


来源:https://stackoverflow.com/questions/44967669/float-label-hint-textinputlayout-not-works-with-android-data-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!