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.
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 theEditText
in code has no effect on theTextInputLayout
's hint – Gebhardt