android – How to programmatically change the edittext line color

Question:

I need to change the EditText line color programmatically, how do I?

I tried to define an xml in the drawable folder, but it doesn't change the line color anyway…

Answer:

From version v22.1 of appcompat-v7 it is possible to define a style and assign it to EditText through the android:theme attribute.

In the res/style.xml file declare a new style :

<style name="MyEditTextTheme">
    <item name="colorControlNormal">#ff0000</item>
    <item name="colorControlActivated">#00ff00</item>
    <item name="colorControlHighlight">@color/accent_material_light</item>
</style>

Grades:

  • Change the colors to your liking.
  • With these settings the line is red when "out focus" and green "in focus"
  • If your theme is dark use @color/accent_material_dark
  • In order for your Activity to work, it will have to inherit from AppCompatActivity

Add the android:theme attribute to the EditText declaration:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    .....
    .....
    android:theme="@style/MyEditTextTheme"/>

adapted from here

Scroll to Top