15

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?

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Try instead using the command setBackgroundResource, ie

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.