android TextView: setting the background color dynamically doesn't work

Tawani picture Tawani · Sep 23, 2009 · Viewed 257.2k times · Source

Setting the background color programatically of an android TextView doesn't seem to work. I'm I missing something!

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);

I also have this file (colors.xml) in my res/values folder

<resources>
        <color name="white">#ffffffff</color>
        <color name="black">#ff000000</color>
</resources>

[EDIT]: Also, setting the text color causes the TextView to disappear.

TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");

Answer

bhatt4982 picture bhatt4982 · Sep 23, 2009

Use et.setBackgroundResource(R.color.white);