8

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
doe
  • 971
  • 2
  • 11
  • 27
  • You could make a custom EditText with your own states. [Here is an detail example.](https://stackoverflow.com/questions/4336060/how-to-add-a-custom-button-state) – Dmytro Ivanov Jan 01 '19 at 11:12
  • Why don't you use TextInputLayout and EditText which has error state and color itself? – Ehsan Mashhadi Jan 01 '19 at 11:58
  • @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 Jan 01 '19 at 12:43

1 Answers1

0

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.

milad salimi
  • 1,580
  • 2
  • 12
  • 31