Android resources crib notes

I don’t want to complain about Android development too much. It has its quirks, but generally its satisfactory. One area however that was totally opaque to me, and frustrating, is Android resources. I never saw them explained very well in tutorials. The documentation, when read through thoroughly, is very good. It was probably just too much for me at first.

So for everyones enjoyment I present my Android resources crib notes. Writing these down helped me get to grips with that pesky res directory.

Android Resources

Android resources are used to separate out non programatic elements of your application such as images, user-facing strings, colours and application layouts. This helps when writing applications for multiple devices and languages by separating out the presentation of the application.

Resource types

The res directory of an Android project holds all the application resources. Android is very restrictive about where resources can be placed within this directory, even to the point of refusing to build if there are any resources placed in the root res directory.

Android groups certain types of resources together and forces you to place these types in specific directories. This directory layout is very important. A table of the directories and the general types of resources allowed is found at the Providing Resources page of the Android documentation.

A directory doesn’t necessarily hold one single type of resource. The drawable directory for example can contain bitmap files, jpeg files and XML files. However, the structure of the XML files must be such that they describe a type of object that can be drawn to the screen. All resources in the drawable directory must be able to instantiate a Drawable class.

The values directory

One directory that may confuse is the values directory. This is because it is a grab-bag of different types of resources including colours, scalar values, strings and ‘styles’. The only common thing about these types is that they are described via XML files. Documentation on the different types and how they are described in XML is contained at Style Resources, String Resources and More Resource Types.

Accessing Resources

Within the program, resources are accessed via the R class. From there, access follows the directory structure. R.drawable contains all the resources in the drawable directory, R.layout contains all resources in the layout directory. The name of each resource is generally the same as the filename without the file extension. The resource res/drawable/blue_circle.xml would be accessed as R.drawable.blue_circle.

The values directory does not follow the normal scheme. There is no R.values package, instead the many different types of resource that can be placed in the values directory each have their own package. Some examples include R.dimen for dimensions, R.string for string values and R.color for colour values. Also different from the rest of the resources is the naming of the value resources. Instead of being named after the filename these resources are named directly in the XML. This allows many resources, even of different types to be placed in one XML file, the name of which has no effect. There are however strong conventions about the file names. All strings are usually placed in res/values/strings.xml and all colours res/values/colors.xml.

Suppose you had two string tags in your strings.xml with attributes name="app_name" and name="app_description". These two resources would then be accessed as R.string.app_name and R.string.app_description

One thing to keep in mind is that when using R.string.app_name, or anything inside the R package is that they are only references to the real values. R.drawable.blue_circle doesn’t contain a Drawable object but a reference that can be used in different locations within the Android API. R.drawable.blue_circle for example could be used to instantiate a Drawable class using getDrawable.

Accessing Resources from XML

Very often you will need to reference a resource not within code but within XML files. These XML files are generally other resources, but also include the AndroidManifest.xml file. The naming scheme for the XML reference follows the R package. If a resources is accessed as R.type.name in code it is accessed as @type/name within XML.

Resource Namespaces

Resources are placed in Java packages. The canonical way to access a resource would be package.R.type.name. So assuming that the base package for your application is com.example.firstapp resources could be accessed as com.example.firstapp.R.string.app_name, or @com.example.firstapp:string/app_name in XML. The package name is in this case superfluous. One instance where you will often need to use the package name, is when you are accessing resources from the base Android platform. For this you would use android.R.type.name or @android:type/name. I have most commonly used this for accessing Android styles and themes such as @android:style/TextAppearance.Large.

Alternative Resources

Android provides a very restricted workflow for creating applications that work with different screen sizes, resolution densities and languages. This is based on resources, and is again dependant on the naming of directories. While the drawable, values and layout directories could be considered the base resources, you are allowed to create directories that contain resources specific to specific device configurations. The configuration is determined using ‘qualifiers’. These are specific strings, placed after the base directory name, and separated by dashes.

For example, to create drawables that will be used only on high dpi screens a directory called drawable-hdpi should be created and populated. For french translation strings a directory called values-fr should be used. For drawables that should only be used in landscape mode on low resolution displays, a directory called drawable-land-ldpi will be useful. Any number of qualifiers can be used after the base name, but there seem to be some complicated rules on how the resources are then determined. See Providing Resources for details.

Roundup

Android resources are a varied bunch. From simple bitmap files, to XML files with complicated schemas such as layouts. Generally the XML schemas for each type of resource are well documented, but there are lots of them. Just keep in mind that each resource directory generally holds a completely different type of object, and remember to look up the rules for the one that applies. The Providing Resources documentation is usually the best place to start.

The use of resources is as varied as the types. Methods taking resource references are to be found everywhere within the Android API, but a few of them, layout, strings and drawables are core. The use of these is included in every example android Application and tutorial.