5.1 Understanding and Accessing Android Resources

In this chapter we will learn about Android resources. Android applications are composed of two things and they are:

  1. Android Code instructions
  2. Android Data / resources

Functionality is in the instruction coded which determines the way our application should behave. Other section of importance is resources i.e. text, images, icons, audio files, videos, etc.

5.1 Introduction to Android resources

Android resource files are stored separately from the rest of java files of application. Generally resources are stored as xml files. Graphics and raw data can be stored as resources.

All resources must be stored in the /res directory of project. So directory hierarchy is strictly maintained. Each resource type corresponds to a particular sub-directory. All the graphics files are stored under /res/drawable folder of directory. In fact it is a good practice to keep non code resources external to our code. Thus we can say android supports externalization of resources. It can range from simple values such as strings, colors to complex things like images, etc.

By storing these resources externally we ease their management, maintenance and updates. Android is intelligent enough to select correct resources without interruption of user explicitly. Thus customization becomes much easier.

Serial Number

Resource sub directory







Graphics resources



User interface



Simple data like strings, color values, etc

Table Default resource directories of android

ADT plug-in automatically detects any new resource file added to the project. These resources are compiled. This generates the R.java file which enables us to access resources programmatically.

5.2 Resolving Android Resources

There are few applications which work perfectly in every environment. But some special case handling is required. We can create alternative resources for android projects. Android resources can be organized into different resource types. Resources can be categorized according to screen characteristics, language and region, input methods, device configurations, etc. So we can say we have two resource hierarchies as shown below:

  • Default resources are the resources which are stored at top of hierarchy. These resources are used regardless of their device configurations.
  • Alternative resources are the specialized versions of these default resources. These are the resources designed while keeping in mind for the purpose of a specific configuration.

Figure - Android Resource hierarchy

Figure - Android Resource hierarchy

There are two specific reasons for opting alternative resources and they are as follows:

1. Internationalization

2. Localization

3. Design

The above listed features facilitate run of applications universally in different device configurations. Android platform has a superb mechanism of loading appropriate resources at runtime. When we create alternative resources few things are to be kept in mind.

  • Android platform is intelligent enough to load the most perfect available resource. In case there is no alternative resource then default resource is used. Thus alternative resources should be added after intelligent and suitable assessment of requirements.
  • Alternative resources are to be named exactly as default resource.
  • Alternative resource put burden over size of application package. Performance can be affected by their addition to application package. It is best to manipulate default resources so that it can add flexibility to application.

5.3 Accessing Android Resources

Android system provides system resources. All of these resources can be used directly from our application code. They can be referenced from within other resources.

We can access the resources in code by the static R class. This class is generated based on our external resources. They are created when the project is compiled. R class contains the sub classes for each resource type. Each of the subclass exhibits its associated resources as variables. These variables have a variable name which matches the resource identifiers. By default, a new project consists of R.string and R.drawable subclasses. The values of these variables are not the instance of resource itself. The value is an integer which represents particular resource’s location in the resource table.

When an instance of resource is required we need to use helper methods which in turn extract them from resource table. The resource table is represented as an instance of Resources class within the application. These helper methods look ups the current resource table. Hence these helper methods cannot be static.

getResources() method is used to access applications’ Resources instance i.e. we would have the following code snippet:

Resources tutionResources=getResources();

Resources classes include getters for each available resource type which usually works by passing resource ID.

We can also refer resources within other resources. We can use resource references as attribute values in other xml resources. In case of layouts and styles it is the best option. We can create themes; localize strings and image assets as per various sources. It is also a suitable method to support different images and spacing for a layout to ensure that it is optimized for different screen sizes and resolutions. The @ notation should be used to refer one resource from another.

Android avails lot of native system resources which we can use in our applications directly. When we use native resources of android they are made available to us from classes of android.R and not from any specific class of R class.

5.4 Android Resource Example

Create a project and name it StringResourceExample app. Open the activity_main.xml file and code it as shown in the following listing:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >
        android:text="@string/hello_world" />
        android:textStyle="italic" />

Figure - activity_main.xml file od Android resouce example

Open the file saying strings.xml under res directory. Open the file and code it as shown in the following listing:

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">StringResourceExample</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="intro">Hello Readers :-) This is just an demo text to demonstrate you the string resource file of application and its usage</string>

Figure - strings.xml file of Android resource example

Graphical layout should be similar to the following snapshot:

Graphical layout of Android resource application

Figure - Graphical layout of Android resource application

Open the manifest file and cross check with the following listing:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
        android:targetSdkVersion="18" />
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Figure - manifest file of  Android resource example app

Create a suitable emulator and run the application. Output should be similar to the following snapshot:


Android resource example app output on emulator

Android resource example app output on emulator

Congratulations !!! We are done. See you in the next section. Till then keep practicing and learning. Happy App Developing!!!