3

I have a simple layout with an edittext.

When I set the background of the EditText to a 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 a strange thing happens when the edittext gains focus, the background color of the layout changes(!).

I need to change the background color dynamically, and I have the same result after calling:

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

I have 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 an Edittext in runtime.

Bucket
  • 7,415
  • 9
  • 35
  • 45
user1940676
  • 4,348
  • 9
  • 44
  • 73

2 Answers2

2

The solution was adding a parent layout to hold the background and set the EditText background to null.

I don't like that 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>
user1940676
  • 4,348
  • 9
  • 44
  • 73
0

Try this one::

 String myHexColor = "#CC2233";// color u get from webserver
EditText myView = (EditText) findViewById(R.id.myEditText);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));
ASP
  • 3,645
  • 1
  • 31
  • 45
  • the method pasrsehexString(String) is undefined for the type Color, parseColor is what I use and it actually works. – user1940676 Feb 18 '14 at 13:26
  • i think this works:: myView.setBackgroundColor(Color.parseColor("#636161")); – ASP Feb 18 '14 at 13:29