How to change edittext line color programmatically

Asked

Viewed 3,886 times

3

I need to change the line color of Edittext programmatically, as I do?

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

2 answers

2

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

In the archive 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 for dark utilize @color/accent_material_dark
  • To make yours work Activity will have to inherit from AppCompatActivity

Add the attribute android:theme to the declaration of Edittext:

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

Adapted from here

  • I tried to set this in my style, but it displays "Error: No Resource found that Matches the Given name: attr 'colorControlActivated'." for the 3 items

  • Look at my issue

  • Have you tested after the issue I made? It answers your question?

  • Still gives the same error, my api is lvl 8 if I am not mistaken, will be incompatibility?

  • minSdkVersion="8" no problem, the problem may be in the targetSdkVersion. You should use in targetSdkVersion always the latest version available, which at this time is the 23. I tested with minSdkVersion="8" and targetSdkVersion=23

  • I just tested with minSdkVersion="8" and targetSdkVersion=8 and it works. I think the only requirements are: V22.1 from appcompat-v7 and Activity inherit from Appcompatactivity.

  • I solved my case by changing the file colors.xml in the briefcase values

  • @Yeah, that’ll do, too. However, in addition to the edittext line color, it will also change the color of other components and this may not be what you want.

  • Really @ramaral, in my case I wanted to change everything anyway

  • Why Ramaral needs to be the latest version available in targetSdkVersion?

  • @Aline, I wanted to say compileSdkVersion. It is advisable to always use the latest version of the SDK to compile. The reason is that the latest version, in addition to possible bugs being fixed, adds other features, usually in conjunction with the latest version of appcompat.

  • Hummm when I create a project I chose the minimum API 17...( minSdkVersion 17) and my compileSdkVersion came in the latest android version...I changed the compileSdkVersion to version 23. No problem for an older device??? Leaving compileSdkVersion in the most current version of the device

  • 1

    @Aline, no. Always use the latest in compileSdkVersion and the corresponding appcompat. What has an impact on older devices is minSdkVersion. Look at this reply where a small explanation of the different xxxSdkVersion. See also this post.

  • Thank you. I get it!

Show 9 more comments

0

There is no way to change the line color. It is possible to change the background of Edittext and you can put a .PNG transparent with a colored line underneath, like this for example:

To generate these .PNGs colorful online you can use this site Android Holo Generator.

However, I recommend downloading the library Materialedittext that implements Edittexts with Material Design and has the feature to change the color of the line programmatically

Browser other questions tagged

You are not signed in. Login or sign up in order to post.