YouTip LogoYouTip

Android Resources

There are many things used to build an excellent Android application. In addition to the application's code, you need to pay attention to various resources, such as various static content you use, such as bitmaps, colors, layout definitions, user interface strings, animations, etc. These resources are generally placed in independent subdirectories under the project's res/.

This tutorial will learn 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 of 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 is an image resource, two layout resources, and a string resource file. The table below details the resources supported in the res/ directory in the project.

DirectoryResource Type
anim/XML files that define animation properties. They are saved in the res/anim/ folder and accessed through the R.anim class
color/XML files that define color state lists. They are saved in the res/color/ folder and accessed through the R.color class
drawable/Image files, such as .png, .jpg, .gif or XML files, compiled into bitmaps, state lists, shapes, animated images. They are saved in the res/drawable/ folder and accessed through the R.drawable class
layout/XML files that define user interface layouts. They are saved in the res/layout/ folder and accessed through the R.layout class
menu/XML files that define application menus, such as option menus, context menus, submenus, etc. They are saved in the res/menu/ folder and accessed through the R.menu class
raw/Arbitrary files saved in their raw form. Need to open raw files by calling Resource.openRawResource() according to the resource ID named R.raw.filename
values/XML files containing simple values (such as strings, integers, colors, etc.). There are some resource naming conventions for files in this folder. arrays.xml represents array resources, accessed through the R.array class; integers.xml represents integer resources, accessed through the R.integer class; bools.xml represents boolean resources, accessed through the R.bool class; colors.xml represents color resources, accessed through the R.color class; dimens.xml represents dimension values, accessed through the R.dimen class; strings.xml represents string resources, accessed through the R.string class; styles.xml represents style resources, accessed through the R.style class
xml/Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various configuration files used at runtime can be saved 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 <resource_name>_<config_qualifier> format. Here resources_name is any resource mentioned in the table above, such as layout, image, etc. The qualifier will determine which resources to use for the specific configuration. You can check the official documentation for a complete qualifier list of different types of resources.
  • Save the corresponding alternative resources in this directory. These resource files must be consistent with the default resource file names shown in the example below, but 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 higher.

Here is an example specifying default screen images and high-resolution alternative images.

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

Here is another example specifying default language layout and Arabic language alternative 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 introduce how to access resources in these two scenarios respectively.

Accessing Resources in Code

When an Android application is compiled, an R class is generated, which contains the IDs of all resources under the res/ directory. You can use the R class to access resources through subclass + resource name or directly using the resource ID.

Example

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

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

The first line of code here uses R.id.myimageview to get the ImageView defined as myimageview in the layout file. The second line uses R.drawable.myimage to get 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 set the text on a TextView object with ID msg using the resource ID, as follows:

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

Example

Consider the layout defined as follows 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 following layout file 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 to the "Hello World!" example explained in the previous chapter, I can be sure that you have a better understanding of all the concepts in this section. Therefore, I strongly recommend going back to look at the previous example and check my basic usage of different resources.

← Python3 CalculatorAndroid Environment Setup β†’