Google+

Android4Beginners.com

Apps development for Android step by step

Appendix B: Everything about colors in Android

Android uses standard RGB (red, green and blue) color model. Each primary color value is usually represented by hexadecimal number.  At the beginning of such a color definition you have to put a pound character (#).

The simplest is just #RGB format, where #000 is black and #FFF is white. But in this format we have only 16 values per color so it gives 4096 combinations. That’s why #RRGGBB format is mainly used. In this format we have 256 values per primary color, so 16 777 216 colors in total. Black is #000000 and white is #FFFFFF.

You could choose from more than 16 million colors for your Android app (Photo credit by SXC.hu/Gabrielaaa)

You could choose from more than 16 million colors for your Android app (Photo credit by SXC.hu/Gabrielaaa)

We could use also alpha channel information that defines transparency (opacity).  There are two formats available #ARGB and #AARRGGBB. The alpha value 00 means that color is fully transparent (so invisible) and FF that it’s not transparent at all (opaque), so you could skip this information.

There are various options how to set colors in Android app.

Option 1: You could put color definition directly into proper XML tag attribute, for instance:

If you want to set color in Java instead you have a few different options.

Option 2: Use hex value as in XML, but the format is 0xAARRGGBB (where every color is from 00 to FF)

Option 3: Parse hex value using parseColor method (where every color is from 00 to FF)

Option 4: Use decimal values and methods rgb or argb that includes alpha channel information (where every color is from 0 to 255)

Option 5: Use predefined colors – popular colors have already natural names that you can use instead of hex or dec values.

The COLOR_NAME could equal: BLACK, BLUE, CYAN, GRAY (DKGRAY for dark gray and LTGRAY for light gray), GREEN, MAGENTA, RED, WHITE or YELLOW. Mind that you have to use capital letters. By the way there is also special color that is called TRANSPARENT and it’s hex value is #00000000.

Option 6: The best solutions is to define your own colors set in the colors.xml file. It’s much easier to remember natural names than hex values, you could use names not only referring to direct colors like “dark_blue”, but use also various other names like “menu_color” or “color_of_introduction”, you could easily change color for all app modifying one line of code and you could even have different colors for smartphones and tablets. The colors defined in colors.xml could be used both in XML and in Java.

So how to do it?

Step 1. Create a new colors.xml file in values subfolder (located in res folder) or open existing one, if you have established one before. To create a new file right click on subfolder name and choose New | Values resource  file (look at the image below). Then you have to enter a name colors.xml and click OK.

First you have to create a new colors.xml file (Android Studio)

First you have to create a new colors.xml file (Android Studio)

You should see now ready to fill colors.xml file with resources as a root tag.

New colors.xml file has already a basic structure, so all you have to do it’s just adding color definitions (Android Studio)

New colors.xml file has already a basic structure, so all you have to do it’s just adding color definitions (Android Studio)

Step 2. To add definitions of your own colors you have to use a tag color with attribute name. The value of the tag could be #RGB, #ARGB, #RRGGBB or #AARRGGBB. For instance:

You could define as many colors as you wish.

Step 3. You could refer to your colors in the same way as to other resources.

So you use @color/your_color_name in the XML or R.color.your_color_name in Java. To resolve defined color in Java you have to use getResources().getColor(R.color.your_color_name). For instance:

This is an example of colors.xml file:

If you would like to have different color sets for various devices (screen sizes) add additional colors.xml files in related to them values subfolders, like values-sw720dp-land.

There are many online and offline tools that can help you to find right hex or decimal RGB values of colors you like, for instance  www.color-hex.com.

Previous:
Next: