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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With