Javier Mendonça
Javier Mendonça

Reputation: 2060

TextInputLayout hint not working when using databinding

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.

Upvotes: 6

Views: 1570

Answers (1)

goemic
goemic

Reputation: 1339

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.

Upvotes: 2

Related Questions