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.
The syntax to reference a resource in an XML file is
@[<package_name>:]<resource_type>/<resource_name>
where in this expression
For example
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 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.
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 Building Your First App.) In the latter case the integers must generally be used as arguments for methods that retrieve the objects of interest. For example,
which sets an image resource for an imageView object corresponding to a file myimage.png in res/drawable.ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage);
which first converts the integer reference R.color.myColor to the color definition myColor in res/values/colors.xml into a string using getString(int), and then converts that to a Color using the parseColor(String) method of Color.Color.parseColor(getString(R.color.myColor));
An application's resources can also be obtained 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,
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 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 Android Studio, this information will be found in the file R.java. It may be located by choosing Project Files from the dropdown at the top of the project panel on the left of Android studio and then choosing app > app > build > generated/source > r/debug > <package name>/R.java. For example, in the Click Tester project, the file 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.
The R class is managed automatically by Android Studio. Thus, as you change assets in a project (add a .png image file to res/drawable, modify res/values/strings.xml in the editor, ...), R.java is 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.
For a full discussion of using and accessing Android resources, see
and the various example projects in the present website.
Last modified: July 25, 2016