Previous  | Next  | Home

Managing Android Resources


 

Android best practices suggest that functionality and the resources supporting that functionality should be as separated as possible. This is typically accomplished by accessing resources when needed within the Java code from external files. Therefore, we need a systematic way to access resources from XML files and from Java code. This appendix outlines the most important issues associated with that management.

 

Accessing Resources from XML

The syntax to reference a resource in an XML file is

    @[<package_name>:]<resource_type>/<resource_name>

where in this expression


For example
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/go" />
assigns to the button the label specified by the string variable go defined in a file in the res/values subdirectory by syntax of the form
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="go">Go</string>
    </resources>
Although for organizational purposes we will usually place string variable definitions in a file res/values/strings.xml, this isn't essential. What is essential is that the file in which the string is defined be in the res/values directory. For example, I could define this string along with colors in the file colors.xml, or create a new file stuff.xml and define it there, as long as the file is in the res/values directory and the string definition is through a <string></string> tag contained within a <resource></resource> parent.

If you reference an Android system resource, as opposed to one defined within your package, you must use the package qualifier with the colon. For example, in

    <?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="@android:color/secondary_text_light"
        android:text="@string/hello" />

@android:color/secondary_text_light is a reference to an Android constant of the R.color class, and so is prefixed by @android:. For a more extensive discussion, see the document Accessing Resources from XML.

 

Accessing Resources from Code

To access resources from within XML files one uses constructions like @string/myString, but to access resources from within Java code we use integers from R.java, like R.drawable.theImage. (The Android R class is discussed further in the next section; see also the discussion in Hello World.) In the latter case the integers must generally be used as arguments for methods that retrieve the objects of interest. For example,


You can also get an application's resources using methods of the class Resources, which you can get an instance of by using the getResources() method of Context. For example, given a Context named context,

    Drawable dr = context.getResources().getDrawable(R.drawable.fig1);

will use the getDrawable(int) method of Resources to assign to the Drawable variable dr the image fig1.png stored in res/drawable.


A summary of the basic syntax to use in referencing Android resources from within both XML and Java code is given in the following table.


 Referencing Android Resources from within XML and Java 
Android Resource Reference from XML Reference from Java
res/layout/main.xml @layout/main R.layout.main
res/drawable-hdpi/file.png @drawable/file R.drawable.file
<string name="myString"> @string/myString R.string.myString
@+id/myButton @id/myButton R.id.myButton

 

The Android R Class

The R class used in the examples of the preceding section is a special class generated automatically at build time that provides user-accessible references to resources in terms of static, final constants of the R class. After a build with Eclipse, this file will be found in gen/<package name>/R.java. For example, in the Click Tester project, the file gen/com.lightcone.clicktester/R.java has the following content:

    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
    *
    * This class was automatically generated by the
    * aapt tool from the resource data it found.  It
    * should not be modified by hand.
    */
    
    package com.lightcone.clicktester;
    
    public final class R {
        public static final class array {
            public static final int colornames=0x7f040000;
        }
        public static final class attr {
        }
        public static final class color {
            public static final int blueback=0x7f050002;
            public static final int greenback=0x7f050001;
            public static final int redback=0x7f050000;
            public static final int yellowback=0x7f050003;
        }
        public static final class drawable {
            public static final int icon=0x7f020000;
        }
        public static final class id {
            public static final int blueblock=0x7f070004;
            public static final int button1=0x7f070000;
            public static final int greenblock=0x7f070002;
            public static final int redblock=0x7f070001;
            public static final int yellowblock=0x7f070003;
        }
        public static final class layout {
            public static final int main=0x7f030000;
        }
        public static final class string {
            public static final int app_name=0x7f060001;
            public static final int black_label=0x7f06000a;
            public static final int blueString=0x7f060008;
            public static final int buttonString=0x7f060002;
            public static final int gray_label=0x7f06000b;
            public static final int greenString=0x7f060006;
            public static final int green_label=0x7f06000d;
            public static final int hello=0x7f060000;
            public static final int longclick=0x7f060004;
            public static final int processMenu_title=0x7f06000e;
            public static final int redString=0x7f060005;
            public static final int red_label=0x7f06000c;
            public static final int shortclick=0x7f060003;
            public static final int white_label=0x7f060009;
            public static final int yellowString=0x7f060007;
        }
    }

The R class integers are given in hexadecimal notation (the prefix 0x indicates that what follows is a hex number). You do not need to know these numbers; they are for Android internal use to allow the resource manager to load the actual resource data (strings, images, and so on) that are compiled into your package. You will reference these resources from Java code using the R.something notation illustrated in the preceding section. As indicated in the comment statements for the listing above, this file is re-generated by Android at each compilation using the aapt (Android Asset Packaging Tool).


Depending on the operating system version, the aapt is probably available in a platform-tools subdirectory of your Android SDK installation. It provides a tool for examining and packaging apk and other Zip-compatible formats like zip and jar. The aapt is not often employed directly in Android programming unless you are writing build scripts or IDE plugins, in which case it can be used to package .apk files. You may obtain a listing of aapt usage and options by navigating to its directory and issuing ./aapt (Linux or Mac) or aapt.exe (Windows).

The R class is managed automatically by the Android plugin for Eclipse. Thus, as you change assets in a project (add a .png image file to res/drawable, modify res/values/strings.xml in the editor, ...), the plugin causes R.java to be regenerated on the fly to reflect the changes. You may reference the R.java constants, as illustrated by examples in the previous section, but you should never edit R.java directly.

 

Further Information

For a full discussion of using and accessing Android resources, see

  1. Resource Types

  2. Providing Resources

  3. Accessing Resources

  4. Accessing Resources from XML

  5. Accessing Resources in Code

and the various example projects in the present website.

Last modified: April 3, 2014


Previous  | Next  | Home