How to get a Color from hexadecimal Color String
Convert that string to an int
color which can be used in setBackgroundColor
and setTextColor
String string = "#FFFF0000";
int color = Integer.parseInt(string.replaceFirst("^#",""), 16);
The 16 means it is hexadecimal and not your regular 0-9. The result should be the same as
int color = 0xFFFF0000;
This question comes up for a number of searches related to hex color so I will add a summary here.
Color from int
Hex colors take the form RRGGBB
or AARRGGBB
(alpha, red, green, blue). In my experience, when using an int
directly, you need to use the full AARRGGBB
form. If you only have the RRGGBB
form then just prefix it with FF
to make the alpha (transparency) fully opaque. Here is how you would set it in code. Using 0x
at the beginning means it is hexadecimal and not base 10.
int myColor = 0xFF3F51B5;
myView.setBackgroundColor(myColor);
Color from String
As others have noted, you can use Color.parseColor
like so
int myColor = Color.parseColor("#3F51B5");
myView.setBackgroundColor(myColor);
Note that the String must start with a #
. Both RRGGBB
and AARRGGBB
formats are supported.
Color from XML
You should actually be getting your colors from XML whenever possible. This is the recommended option because it makes it much easier to make color changes to your app. If you set a lot of hex colors throughout your code then it is a big pain to try to change them later.
Android material design has color palates with the hex values already configured.
- Material Design color styling
- Material Design color palette generator
These theme colors are used throughout your app and look like this:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#FF4081</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
If you need additional colors, a good practice to follow is to define your color in two steps in xml. First name the the hex value color and then name a component of your app that should get a certain color. This makes it easy to adjust the colors later. Again, this is in colors.xml.
<color name="orange">#fff3632b</color>
<color name="my_view_background_color">@color/orange</color>
Then when you want to set the color in code, do the following:
int myColor = ContextCompat.getColor(context, R.color.my_view_background_color);
myView.setBackgroundColor(myColor);
Android Predefined colors
The Color class comes with a number of predefined color constants. You can use it like this.
int myColor = Color.BLUE;
myView.setBackgroundColor(myColor);
Other colors are
Color.BLACK
Color.BLUE
Color.CYAN
Color.DKGRAY
Color.GRAY
Color.GREEN
Color.LTGRAY
Color.MAGENTA
Color.RED
Color.TRANSPARENT
Color.WHITE
Color.YELLOW
Notes
- A quick way to find hex colors is to open the color chooser dialog in Gimp (or some other photo editing software).
- Standard opacity levels in Material Design
Try:
myLayout.setBackgroundColor(Color.parseColor("#636161"));
Try Color
class method:
public static int parseColor (String colorString)
From Android documentation:
Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
AndroidX: String.toColorInt()