Wednesday, 2 September 2015

android how to convert resource color to int color




It is good to put color settings into resource files like this:

<color name="progressBar_loading_color">#FFFF0000</color>

The benefit of doing this is you can see the color in Android studio:



However when you programmatically set colors in your code apis usually ask for int value.


So, how to convert a color from resource type (Hex) to integer is a question.

Solution:


http://stackoverflow.com/questions/5255141/android-colors-xml-resource-to-int-value


The getColor() method of Resources returns the color in 0xAARRGGBB format;
int color = getResources().getColor(R.color.white); // color is now 0xFFFFFFFF
int alpha = Color.alpha(color);
int red = Color.red(color);
...




1 comment: