Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

colors.xml resource does not work

I created a colors.xml file in my Android app under /res/values/colors.xml. The contents are...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="Green">#00ff00</color>
</resources>

I try to update the background of my a TableRow using...

    TableRow test = (TableRow)findViewById(R.id.tableRow2);
    test.setBackgroundColor(R.color.Green);

This does not set it as green, it is gray instead. No matter what values I add to the colors.xml file, it is always the same gray color. However this does work...

    TableRow test = (TableRow)findViewById(R.id.tableRow2);
    test.setBackgroundColor(android.graphics.Color.GREEN);

Is something wrong with my colors.xml?

like image 975
b10hazard Avatar asked Jul 10 '11 12:07

b10hazard


2 Answers

You should use this instead:

TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(getResources().getColor(R.color.Green));

Its unfortunate that resource ID and color have same type: int. You should get color value from resources via getColor() and use that valu as color. While you are using resource ID as color.

like image 109
inazaruk Avatar answered Oct 16 '22 12:10

inazaruk


Try instead using the command setBackgroundResource, ie

TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundResource(R.color.Green);
like image 22
PearsonArtPhoto Avatar answered Oct 16 '22 13:10

PearsonArtPhoto