Issue
How to change startIconDrawable color when the focus is on the TextInputEditText
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
app:boxBackgroundColor="@color/white"
app:endIconMode="clear_text"
app:startIconDrawable="@drawable/person_24dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:hint="family"
android:imeOptions="actionDone"
android:singleLine="true"
android:textSize="@dimen/mediumTextSize" />
</com.google.android.material.textfield.TextInputLayout>
Solution
Kotlin:
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
val color = if (hasFocus) Color.BLUE else Color.GRAY
textInputLayout.setStartIconTintList(ColorStateList.valueOf(color))
}
Java:
textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
int color = hasFocus ? Color.BLUE : Color.GRAY;
textInputLayout.setStartIconTintList(ColorStateList.valueOf(color));
}
});
Java code can be simplified using lambda expression.
Answered By - miel3k
Answer Checked By - Marie Seifert (JavaFixing Admin)