How to Set a Color In Android

Changing Colors in Android and Naming Them for Convenience

An Android color is a 32-bit integer value consisting of four eight bit parts, ARGB. The four parts are the amount of red, green and blue that is in the color, plus how opaque (see through) is the color, which is called the alpha value, the lower the alpha value the more transparent the color appears. (Note that in the United Kingdom color is spelt colour.)

The alpha value is the highest (first) byte in the 32-bit value followed by the red, then green and finally the blue byte. Hence it is referred to as an ARGB value with each letter representing the type and order of the byte. This format allows for easy representation as a hexadecimal number in Java:

The three byes representing the color values provide over 16 million color possibilities in Android (256 x 256 x 256 = 16,777,216). A color depth better than the human eye (stated in the Wikipedia article). Plus all these colors can range from transparent, 0, to opaque, 255.

Named Color Resources in Android

To help with handling colors in Android they can be made into a resource for easy of reuse. Either open an existing resource file or create a new one. For example in Eclipse with a Android project open select the res/values folder. Then use the File menu (or the context menu on the values folder) to add an Android XML File. Give the file a name, e.g. colors.xml and add a color element:

Use getColor() to read the color value from the resource.

If the color is solid (full alpha value) then the color resource can leave the alpha value out. Though for clarity and future maintenance it is wise to be explicit and always define all four parts of an Android color:

The use of a named color in a resource file is handy when dealing with common colors. For example the standard HTML named web colors (CSS colors) or X Window System and SVG color names. You can create a resource file to define the common colors for use in an App. Here is a resource file to use the HTML/CSS color names in Android code:

Then just use them as required:

Referencing Colors in Android XML Layouts

To reference an Android color resource in an XML layout simple use @color/name_of_color, the same as other resources are referenced, such as strings:

Accessing Android System Color Resources

Are there predefined colors in Android? There are existing color resources, but not many. Also Google does not recommend using certain system resources as they are not guaranteed to be the same in future releases, or even be there. You can browse the Android color resources in a App project. Expand the Android library in the Package Explorer, expand the android.jar file, then the android package and the R.class and view the items under color. There are resources such as and R.color.holo_purple. The full list is also available in the R.color developer documentation. To access these resources you need the package qualifier, as in @android:color/black. In code add android before the R:

Note that the Android color resources starting holo are not available on versions of Android prior to API level 14 (Ice Cream Sandwhich).

The Android Color Class

There is a helper class in Android that has functions to ease the generation of color values from individual alpha, red, green and blue components, from strings and from HSV, and back again. The Color class defines a limited range of static colors, e.g. Color.YELLOW, see the Android developer documentation for full information on the Color class:


Color class methods: HSVToColor, RGBToHSV, alpha, argb, blue, colorToHSV, green, parseColor, red, rgb

Changing Text Color in Android

Use the textColor attribute to modify the color of text when it is defined in an XML layout:

To do the same in Android code, i.e. set the text colour programmatically, use the setTextColor() method:

Adding a Color Picker to an App

Android Color PickerIf you need to add the ability to configure a color for a setting in an App it is easily done. The Android SeekBar can be used for a quick and easy color picker, drop four SeekBars onto a layout, one for each component of a color (only three are need if you do not want to change the transparency). Use the methods from the Color class to combine the SeekBar outputs into the color value.

The APIDemos sample App that comes with the Android SDK also features a color picker in one of the demos, seen in the picture here. If you run the API demos you can access the color picker by choosing Color on the menu from the FingerPaint option under Graphics. The color picker is in There are also other color pickers available, with full source, on Google Code simply search for Android color picker.

And Finally…

What is the color of the Android Robot? Android green is 0xA4C639.


Storing ARGB color values in Android resource files, e.g. <color name=”Red”>#FFFF0000</color>, makes it easy to remember the color values for Widgets and Views in Layouts, the names are easier to remember than hex strings. There is a Color helper class available to help with color conversion and value extraction. Several free color picker Views are available to add color selection functionality to Apps. See also Color State List.


Open Clip Art Library user inky2010 for the Prism image that headed this article.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.