Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Akshay Shivpuri
2,917 PointsSetting RGBA to background color
Hi there, I want to set a background color of RGBA value(as it has opacity) to a TextView.
mAnswerlabel.setBackgroundColor(????);
I've my RGBA value as (0, 0, 0, 0.2). I googled a lot about it and found that only int value can be passed in setBackgroundColor and SetAlpha property would be used, but I'm not able to use it.
I hope someone can help me on this.
Thanks in advance, Akshay
5 Answers

Marcus Vieira
7,877 PointsTry this:
mAnswerlabel.setBackgroundColor(Color.argb(alpha, r, g, b));
edit: added argb

Ernest Grzybowski
Treehouse Project ReviewerI prefer to set up my colors in hex. You can do something like view.setBackground(0x80FF0000)
-
0x
signifies hex. -
80
means 50% alpha -
FF
for red -
0000
for no blue or green
As with most things in programming, there are many ways to accomplish the same task at hand. You can try any of these:
myView.setBackgroundColor(Color.argb(a_int, r_int, g_int, b_int));
myView.setBackgroundColor(Color.parseColor("#AARRGGBB"));
myView.setBackgroundColor(0xAARRGGBB);
For an alpha of 0.2, this is technically 20% so you could just use 0x33FF0000 for a 0.2 alpha red color.
I use https://kuler.adobe.com to find my hex color, and for alpha in hex I use:
100% — FF 95% — F2 90% — E6 85% — D9 80% — CC 75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C 50% — 80 45% — 73 40% — 66 35% — 59 30% — 4D 25% — 40 20% — 33 15% — 26 10% — 1A 5% — 0D 0% — 00

Akshay Shivpuri
2,917 PointsThanks Ernest Grzybowski ! That was a detailed answer. :)

Akshay Shivpuri
2,917 PointsAnd btw, we can also use XML format for setting background color.

Akshay Shivpuri
2,917 PointsBut it has alpha value also i.e. rgba and not rgb where alpha value is in decimal(.2)

Marcus Vieira
7,877 PointsI updated my answer :)

Akshay Shivpuri
2,917 PointsBut then again how will it accept "a" as 0.2. Only int values can be passed

Akshay Shivpuri
2,917 PointsOr wait, maybe I found out. "a" value would range from 0 to 255 and in this case for 0.2, would approximately be around 55. Thanks for your help. :)

Ernest Grzybowski
Treehouse Project ReviewerEdited my answer.