Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically changing underline color of EditText

Tags:

android

I have an ordinary EditText field that I would like to programmatically change the underline color for.

<EditText
    android:id="@+id/edit_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"/>

Other answers suggest changing the background color filter like so:

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

However, I don't see any change when I run the app. Changing the background itself:

editText.setBackground(color)

changes the entire EditText to color - not what I want!

How do I programmatically change the underline color for an EditText, AppCompatEditText or TextInputEditText? I am using version 25.0.1 of the Support Library.

like image 430
degs Avatar asked Nov 28 '16 06:11

degs


People also ask

How do you change the color of the TextInputLayout underline?

You can apply a custom style to change the colors. To change the hint color you have to use these attributes: hintTextColor and android:textColorHint . To change the bottom line color you have to use the attribute: boxStrokeColor .

How can I remove underline from EditText?

How can I remove underline from text in Android? Just add android:background="@null" this line of code into your EditText xml.


1 Answers

@degs's answer is correct. But just add one little note: the AppCompatEditText#setSupportBackgroundTintList is now annotated with @RestrictTo(LIBRARY_GROUP) which means:

Restrict usage to code within the same group of libraries

Instead use ViewCompat#setBackgroundTintList. So in your example, it should look like this:

ColorStateList colorStateList = ColorStateList.valueOf(color);
ViewCompat.setBackgroundTintList(editText, colorStateList);
like image 59
Aaron He Avatar answered Sep 25 '22 11:09

Aaron He