Android Resources Access

There are many things used to build an excellent Android application. Besides application coding, you need to focus on various resources, such as various static content you use, like bitmaps, colors, layout definitions, user interface strings, animations, and so on. These resources are generally placed in independent subdirectories under the project's res/ directory.

This tutorial will teach you how to organize application resources, specify alternative resources, and access them in the application.


Organizing Resources in Eclipse

You need to place each resource in a specific subdirectory under the res/ directory in the project. For example, here is the file hierarchy of a simple project:

MyProject/
    src/  
        MyActivity.java  
    res/
        drawable/  
            icon.png  
        layout/  
            activity_main.xml
            info.xml
        values/  
            strings.xml 

The res/ directory contains all resources in various subdirectories. Here there is an image resource, two layout resources, and one string resource file. The table below details the resources supported in the res/ directory in the project.

Directory Resource Type
anim/ XML files that define animation properties. They are stored in the res/anim/ folder and accessed via the R.anim class.
color/ XML files that define color state lists. They are stored in the res/color/ folder and accessed via the R.color class.
drawable/ Image files, such as .png, .jpg, .gif, or XML files, are compiled into bitmaps, state lists, shapes, and animated images. They are stored in the res/drawable/ folder and accessed via the R.drawable class.
layout/ XML files that define user interface layouts. They are stored in the res/layout/ folder and accessed via the R.layout class.
menu/ XML files that define application menus, such as option menus, context menus, and submenus. They are stored in the res/menu/ folder and accessed via the R.menu class.
raw/ Arbitrary files saved in their original form. You need to open raw files by calling Resources.openRawResource() based on the resource ID named R.raw.filename.
values/ XML files containing simple values (such as strings, integers, colors, etc.). Here are some resource naming conventions under folders. arrays.xml represents array resources, accessed via the R.array class; integers.xml represents integer resources, accessed via the R.integer class; bools.xml represents boolean resources, accessed via the R.bool class; colors.xml represents color resources, accessed via the R.color class; dimens.xml represents dimension values, accessed via the R.dimen class; strings.xml represents string resources, accessed via the R.string class; styles.xml represents style resources, accessed via the R.style class.
xml/ You can read any XML file at runtime by calling Resources.getXML(). You can store various configuration files used at runtime here.

Alternative Resources

Your application needs to provide alternative resource support for specific device configurations. For example, you need to provide alternative image resources for different screen resolutions and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for the application.

To determine a set of alternative resources for a specific configuration, follow these steps:

  • Create a new directory under res/, named in the format <resource_name>_<config_qualifier>. Here, resources_name is any of the resources mentioned in the table above, such as layouts, images, etc. The qualifier will determine which resources are used for the specific configuration. You can check the official documentation for a complete list of qualifiers for different types of resources.
  • Save the corresponding alternative resources in this directory. These resource files must have the same names as the default resource files as shown in the example below; however, these files will substitute the determined content. For example, although the image file names are the same, for high-resolution screens, the image resolution will also be high.

The following is an example of specifying a default screen image and an alternative high-resolution image.

MyProject/
   src/
    main/
    java/
       MyActivity.java  
       res/
          drawable/  
            icon.png
            background.png
        drawable-hdpi/  
            icon.png
            background.png  
        layout/  
            activity_main.xml
            info.xml
        values/  
            strings.xml

The following is another example of specifying a default language layout and an alternative Arabic language layout.

MyProject/
   src/
    main/
    java/
       MyActivity.java  
      res/
         drawable/  
            icon.png
            background.png
        drawable-hdpi/  
            icon.png
            background.png  
        layout/  
            activity_main.xml
            info.xml
        layout-ar/
            main.xml
        values/  
            strings.xml

Accessing Resources

In application development, you need to access defined resources, whether through code or through XML files. The following sections describe how to access resources in these two scenarios respectively.

Accessing Resources in Code

When an Android application is compiled, an R class is generated that contains IDs for all resources in the res/ directory. You can use the R class to access resources by subclass + resource name or directly by resource ID.

Example

To access res/drawable/myimage.png and set it on an ImageView, you would use the following code:

ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

Here, the first line of code uses R.id.myimageview to obtain the ImageView defined as myimageview in the layout file. The second line uses R.drawable.myimage to obtain the image named myimage in the drawable subdirectory of res/.

Example

Consider the next example, where res/values/strings.xml has the following definition:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string  name="hello">Hello, World!</string>
</resources>

Now you can use the resource ID to set the text on the TextView object with ID msg, as follows:

TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);

Example

Consider the layout defined as res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical" >

   <TextView android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a TextView" />

   <Button android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a Button" />

</LinearLayout>

This application code will load this layout for the activity, in the onCreate() method as follows:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main_activity);
}

Accessing in XML

Consider the following XML resource file res/values/strings.xml, which contains a color resource and a string resource -

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <string name="hello">Hello!</string>
</resources>

Now, you can use these resources in the layout file below to set the text color and text content:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="@color/opaque_red"
    android:text="@string/hello" />

Now, if you go back again to the "Hello World!" example explained in the previous chapter, I am sure you will have a better understanding of all the concepts in this section. Therefore, I strongly recommend going back to the previous examples and reviewing the basic usage of the different resources I used.

Other Extensions