degs degs - 4 days ago 5
Android Question

Programmatically changing underline color of EditText

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.

Answer

You need to set the backgroundTintList (or supportBackgroundTintList) on the EditText to an instance of ColorStateList containing only the color you wish to change the tint to. An easy way to do this in a backwards-compatible way looks like this:

ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)

This will give the EditText the desired underline color.

Comments