In a fragment, I would like to change the TextInputLayout hint text after the user touches the TextInputEditText. Before it animates up to the floating hint.
I can change the hint no problem, but I want the user to touch it first, then change it.
How can I do this?
Thank you
To change hint text color Use View.OnFocusChangeListener
to set the hintTextAppearance
. Try the config below.
<style name="inactive" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/darker_gray</item>
<item name="android:textColor">@android:color/darker_gray</item>
</style>
<style name="active" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:textColor">@android:color/black</item>
</style>
XMl
<android.support.design.widget.TextInputLayout
android:id="@+id/tet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et"
app:hintTextAppearance="@style/inactive"
android:layout_margin="@dimen/activity_horizontal_margin"
app:hintEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:hint="Floating Hint" />
</android.support.design.widget.TextInputLayout>
Change the style during focus change.
final TextInputLayout inputLayout=findViewById(R.id.tet);
final TextInputEditText edit=findViewById(R.id.edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
inputLayout.setHintTextAppearance(R.style.active);
else
inputLayout.setHintTextAppearance(R.style.inactive);
}
});
EDIT:- To change Hint text only you can just change it on focus change.
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
edit.setHint("Enter name");
else
edit.setHint("Name");
}
});