N J
N J

Reputation: 27505

Getting RGB at Runtime from color

I want to extract the RGB value from color

My color is declared in xml as

<color name="color_primary">#009688</color>

Now I want the RGB value of this in my Activity at runtime. How to convert color_primary in Color object ? Any help? Thanks in advance.

Upvotes: 0

Views: 48

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

Your color is an int. You can use

int color = getResources().getColor(R.color.color_primary);

in onCreate of your Activity. If you need the RGB components of your color, you can use the Color class:

int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color); 

Upvotes: 3

Related Questions