Android Resources Access
There are many elements that contribute to building an excellent Android application. Beyond the application's coding, you need to focus on various resources such as the static content you use, like bitmaps, colors, layout definitions, UI strings, animations, and more. These resources are typically placed in separate subdirectories under the project's res/
directory.
This tutorial will guide you on how to organize application resources, specify alternative resources, and access them within your application.
Organizing Resources in Eclipse
You need to place each type of resource in a specific subdirectory under the project's res/
directory. 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 a string resource file. The following table details the supported resources in the res/
directory of a project.
Directory | Resource Type |
---|---|
anim/ | XML files defining animation properties, stored in res/anim/ and accessed via R.anim class |
color/ | XML files defining color state lists, stored in res/color/ and accessed via R.color class |
drawable/ | Image files like .png , .jpg , .gif , or XML files compiled into bitmaps, state lists, shapes, animated images, stored in res/drawable/ and accessed via R.drawable class |
layout/ | XML files defining UI layouts, stored in res/layout/ and accessed via R.layout class |
menu/ | XML files defining application menus, such as options, context, and submenus, stored in res/menu/ and accessed via R.menu class |
raw/ | Any files saved in their raw form, accessed by calling Resource.openRawResource() with the resource ID named R.raw.filename |
values/ | XML files containing simple values like strings, integers, colors, etc. Resource naming conventions include arrays.xml for array resources accessed via R.array class, integers.xml for integer resources accessed via R.integer class, bools.xml for boolean resources accessed via R.bool class, colors.xml for color resources accessed via R.color class, dimens.xml for dimension values accessed via R.dimen class, strings.xml for string resources accessed via R.string class, styles.xml for style resources accessed via R.style class |
xml/ | Any XML files that can be read at runtime by calling Resources.getXML() , used for various configuration files |
Alternative Resources
Your application may need to provide alternative resources for specific device configurations, such as different screen resolutions or languages. At runtime, Android detects the current device configuration and loads the appropriate resources.
To specify 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,resource_name
is any resource mentioned in the table above, such as layouts or images, andqualifier
specifies which configuration to use. Refer to 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 name as the default resource files but contain alternative content. For example, although the image filenames are the same, the images for high-resolution screens have higher resolutions.
Here is an example specifying default screen images and alternative images for high-resolution screens:
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
Another example specifying default language layouts and alternative layouts for Arabic:
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, either through code or XML files. The following sections explain how to access resources in both scenarios.
Accessing Resources in Code
When an Android application is compiled, an R
class is generated, which contains IDs for all resources in the res/
directory. You can access resources using the R
class, either by subclass and resource name or directly by resource ID.
Example
To access res/drawable/myimage.png
and set it to an ImageView
, you would use the following code:
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Here, the first line retrieves the ImageView
defined as myimageview
in the layout file using R.id.myimageview
. The second line retrieves the image named myimage
in the drawable
subdirectory of res/
using R.drawable.myimage
.
Example
Consider the following definition in res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>
You can now use the resource ID to set the text on a TextView
object with ID msg
as follows:
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
Example
Consider the following layout definition in 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 in the onCreate()
method as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
Accessing Resources in XML
Consider the following XML resource file res/values/strings.xml
, which includes 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>
You can use these resources in a layout file to set text color and content as follows:
<?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 revisit the "Hello World!" example from the previous chapter, you should have a better understanding of all the concepts covered in this section. I strongly recommend reviewing the previous example to see the basic usage of different resources.