Android FAQ: How can I create a Color
from a hexadecimal color string in Android?
The Android Color.parseColor method
Solution: Use the Android Color.parseColor method, like this:
int color = Color.parseColor("#519c3f");
I just used it like this in my code, where I directly set the Paint color:
paint.setColor(Color.parseColor("#519c3f"));
The string shown is a hexadecimal/HTML color string which represents a light shade of green. The important part of this is that I liked that color in a prototype, then got the HTML/hexadecimal color string using Gimp, and then could use that string directly in my Android/Java application to create a color, without having to manually convert the color string to RGB.
Using an alpha value with Color.parseColor
To add an alpha value, just add two more hex values at the front of the string. This alpha setting (FF
) means the color is “fully on,” i.e., it is not transparent at all:
paint.setColor(Color.parseColor("#ff519c3f"));
This alpha setting (22
) means the color is almost fully transparent:
paint.setColor(Color.parseColor("#22519c3f"));
Standard Android colors (Color class docs)
For the record, the full package for the Android Color class is android.graphics.Color. Also, those docs state:
Supported formats are: #RRGGBB #AARRGGBB or
one of the following names:
'aqua'
'black'
'blue'
'cyan'
'darkgray'
'darkgrey'
'fuchsia'
'gray'
'green'
'grey'
'lightgray'
'lightgrey'
'lime'
'magenta'
'maroon'
'navy'
'olive'
'purple'
'red'
'silver'
'teal'
'white'
'yellow'
Creating Android colors using RGB values
Also, while I’m in the neighborhood, this is one way to create an Android Color
instance using RGB values:
g.setColor(Color.rgb(130, 130, 130));