locoboy
locoboy

Reputation: 38950

set colors in android

I'm trying to set the background color for a tablerow in android and am having trouble referencing the proper int. Below is the code. Am I doing something wrong? The color that turns up in the background is a light grey.

   <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Trackfolio</string>
    <color name="colorWhite">#FFFFFF</color>
    <color name="colorBlack">#000000</color>
    <color name="colorLightBlue">#6495ED</color>
</resources>

row.setBackgroundColor(R.color.colorLightBlue);

Also is there a way to set the text color?

tv.setTextColor(R.color.colorBlack);

Upvotes: 0

Views: 440

Answers (3)

Will Tate
Will Tate

Reputation: 33509

cfarm54,

The way you are accessing the colors you are getting the offset location in R.java gen file.

You need to access them like this...

Resources res = context.getResources();
row.setBackgroundColor(res.getColor(R.color.high_priority));

Upvotes: 3

Kevin Coppock
Kevin Coppock

Reputation: 134684

Try setBackgroundResource(int color). Using setBackgroundColor uses the int that R.color.colorLightBlue uses to reference your defined color, and tries to parse it as a color, rather than retrieving the referenced color.

Upvotes: 0

Oops: dumb me, you should use tableRow.setBackgroundResource() when referecing a color resource.

Upvotes: 0

Related Questions