Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change EditText background color and keep underline

Tags:

java

android

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.

like image 726
pau1adam Avatar asked Oct 17 '22 21:10

pau1adam


1 Answers

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);
like image 145
MeLean Avatar answered Oct 20 '22 17:10

MeLean