TextInputLayout hint not working when using databinding
Asked Answered
A

1

6

I am using databinding together with the TextInputLayout/TextInputEditText combo as shown in the xml.

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/lastname_input_layout"
                style="@style/TextInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:endIconMode="clear_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/firstname_input_layout"
                app:visible="@{viewModel.showFields}">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/lastname_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/lastname"
                    android:inputType="textCapWords"
                    android:text="@={viewModel.lastName}"
                    android:textAppearance="@style/TextAppearance.Input" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/email_input_layout"
                style="@style/TextInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:endIconMode="clear_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lastname_input_layout"
                app:visible="@{viewModel.showFields}">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/email_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@{viewModel.watchMediaTextHint}"
                    android:inputType="@{viewModel.watchMediaInputType}"
                    android:text="@={viewModel.mediaText}"
                    android:textAppearance="@style/TextAppearance.Input" />

            </com.google.android.material.textfield.TextInputLayout>

As you can see, both fields are theoretically the same, the only difference is that one of them has the hint text given with a string resource, the other one with a databinding. Both have the same style, text appearance and so on, yet, the hint on the TextInputEditText using the databinding has different color. Also, when the view gets focus, the label doesn't animate up, as it does with the first field.

Setting the hint with a string resource gets this behavior to go back to normal, any ideas on why is this anomaly happening? I would prefer to solve this with a databinding rather than a programmatic solution.

Thanks.

Andrea answered 27/4, 2020 at 19:31 Comment(2)
The hint attribute needs to go on the <TextInputLayout>, instead of the <TextInputEditText>. It's only set from that attribute on the <TextInputEditText> once, when it's added during inflation. After that, setting the hint on the EditText in code has no effect on the TextInputLayout's hintGebhardt
@MikeM. You nailed it, thank you very much! Didn't know this particular thing of the hint, very interesting indeed.Africa
O
2

The hint attribute only works in combination with databinding, if it is set to <TextInputLayout> instead of <TextInputEditText>.

The <TextInputEditText>s hint attribute is only applied once during inflation and thereby will not be updated/applied when used in combination with databinding.


Heads up for @Mike M.'s comment. I converted it as an answer, in order to be found more easily by others.

Overload answered 8/2, 2022 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.