Runoob Tutorial ArchiveEnglish

Android Resource Management

Accessing Android Resources

There are many things to build a great Android application. Besides the application code, you need to pay attention to various resources such as the various static content you use, like bitmaps, colors, layout definitions, UI strings, animations, etc. These resources are generally placed in separate subdirectories under the project's res/ directory.

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 type of 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 one string resource file. The table below details the resources supported in the res/ directory of the project.

Directory Resource type
anim/ XML files that define animation attributes. 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, compiled into bitmaps, state lists, shapes, or animated images. They are stored in the res/drawable/ folder and accessed via the R.drawable class
layout/ XML files defining the user interface layout. They are stored in the res/layout/ folder and accessed via the R.layout class.
menu/ XML files defining application menus, such as options menus, context menus, submenus, etc. They are stored in the res/menu/ folder and accessed via the R.menu class.
raw/ Arbitrary files are saved in their raw form. Raw files need to be opened by calling Resource.openRawResource() using the resource ID named R.raw.filename.
values/ XML files containing simple values (such as strings, integers, colors, etc.). Here are the naming conventions for resources in some 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(). Various configuration files used at runtime can be saved here.

Alternate resources

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

To provide a set of alternate resources for a specific configuration, follow these steps:

The following is an example specifying the default screen image and the high-resolution alternate 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 specifying the default language layout and the Arabic language alternate 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


Access Resources

In application development, you need to access defined resources, either 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 in the res/ directory. You can use the R class to access resources either by subclass + resource name or directly by 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 following 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 the ID msg, as follows:

TextView msgTextView = (TextView) findViewById(R.id.msg);

msgTextView.setText(R.string.hello);

Instance

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 into the activity in the onCreate() method, as follows:

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.main_activity);

}

Access 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 am sure you will 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 the basic usage of different resources.