How to change the color of the edittext line programmatically

3

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

I tried to set an xml in the drawable folder, but it does not change the line color anyway ...

    
asked by anonymous 11.11.2015 / 18:24

2 answers

2

From the appcompat-v7 version and assign it to EditText via the android:theme attribute.

In the file res/style.xml 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>

Notes:

  • 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, you will 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

    
11.11.2015 / 18:58
0

You can not change the color of the line. You can change the background of EditText and you can put a transparent% color with a colored line underneath, like this for example:

Togeneratethese%color%onlineyoucanusethissite Android Holo Generator .

However, I recommend downloading the MaterialEditText library that implements EditTexts with Material Design and has the ability to programmatically change the color of the line

    
11.11.2015 / 18:53