Home Tutorials Hints & Tips

Getting Started in Android Development

Part 8 - Adding Color to a Layout

This tutorial looks at adding some color to the linear layout. This is a straightforward process. Like the string values, we can hard code the color value directly into the XML layout file, but just like the string values this can make our app difficult to support. What we really need to do is create an XML file with color resources.

First of all create the value file by right clicking over the values folder under the “res” folder as in the screen shot below.

Creating a resource value file in Android Studio

The dialog box below will appear.

Android resource file colors.xml dialog

Create a new file with the name “colors.xml” and click OK.

The newly created “colors.xml” file will appear in the Android Studio workspace. Adding color resources is similar to adding string resources. In the video example I added a single custom color called “slate_blue” with the hexadecimal value #357EC7. The example colors.xml file is below. You can add all the colors needed to file.

Referencing the colors in the layout

Just like the strings in the strings.xml file, we can reference a color by adding an XML attribute to the layout. In this example, I used the “slate_blue” we created as the background color for the entire layout. The relevant XML attribute is below and the “@color/” reference is the key to referencing the color in the colors.xml values file.


There is also a way to access predefined colors built into Android.  A list of predefined Android colors is here.

An example of how these are accessed using XML attributes is shown below:


The “@android:color/” statement is the key to accessing these. At the end of this tutorial your activity_main.xml layout should look similar to he one below.

There are some excellent guidelines for best practice in color design on the Android developers website. Preferred palettes are recommended for  Android 5.0 (API 21, Lollipop). These can be found at here.

Download Download tutorial set 1

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved