Change background color in an EditText question

I have a simple layout with edittext.

When I set the background EditTextto color

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/white" />

Then the strange thing happens, when the focus of the edittext focuses, the background color of the layout changes (!).

I need to change the background color dynamically, and after the call, I get the same result:

subject.setBackgroundColor(Color.parseColor(mycolor));

I also tried the following method:

subject.setBackground(new ColorDrawable(Color.parseColor(mycolor)));

The result was the same.

Basically I want to change the background color of the Edittext at runtime.

+3
source share
2 answers

The solution was to add a parent layout to hold the background and set the background EditTextto null.

I do not like this solution, but it works.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"  >

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null" />
</LinearLayout>
+2
source

Try the following:

 String myHexColor = "#CC2233";// color u get from webserver
EditText myView = (EditText) findViewById(R.id.myEditText);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));
0
source

All Articles