5

The line underneath the text is a drawable set to the background of the view and when editText.setBackgroundColor(color) is called the following happens internally:

setBackground(new ColorDrawable(color));

This removes the drawable that contained the line and replaces it with the color we gave it.

Is it possible to change the background color of an EditText without having the line under the text disappear?

Setting the EditText inside a layout and changing the layout's background color is not an option.

pau1adam
  • 577
  • 6
  • 15

1 Answers1

3

Try to make drawable some think like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-2dp" android:left="-2dp" android:right="-2dp">
        <shape> 
            <solid android:color="@color/background_color"/>
            <stroke android:color="@color/underline_color" android:width="2dp"/>
        </shape>
    </item>
</layer-list>

The set your EditText background from resources like this:

editText.setBackgroundResource(R.drawable.name_to_xml_file);
MeLean
  • 3,092
  • 6
  • 29
  • 43
  • But what if you want to able to change the color at runtime? I do this when selecting and deselecting lines in a rceyclerview. – pau1adam Mar 05 '18 at 07:51
  • 1
    use selector as described here https://stackoverflow.com/questions/14023886/android-button-selector – MeLean Mar 05 '18 at 07:54