8
votes

I have an Edittext with below style

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_focused="true" />
    <item android:color="@color/yellow" android:state_focused="false" />
    <item android:color="@color/white" android:state_active="true" />
</selector>

And in the Edittext style I apply

<item name="backgroundTint">@drawable/states_edit_text</item>

and it works. This changes the EditTexts bottom line color in states normal and focused . I need to change the tint color to red when its in the error state.

Something like

<item android:state_error="true" android:color="@color/red"></item>

But there is no state called error, I referred to other answers and they recommend to achieve it through code. is there any way using android styles, I can set the EditText tint to red on error state?

1
You could make a custom EditText with your own states. Here is an detail example.Dmytro Ivanov
Why don't you use TextInputLayout and EditText which has error state and color itself?Ehsan Mashhadi
@EhsanMashhadi I knew the solution with TextinputLayout, but this was an already existing code where a lot of EditTexts are there in several screens. We need to make this change in every EditText. Since all these EditTexts have the same style, I was trying some solution with the styles only.doe

1 Answers

0
votes

I think you can change it programmatically :

if (!trueState){

ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)

}  

This will give the EditText the desired underline color.