Login     Sign Up
Innocent Cyril (@admin)
4 months ago
81 Views

Using Resources

As well as the resources you create, Android supplies several system resources that you can use in your applications. The resources can be used directly from your application code and can also be referenced from within other resources (e.g., a dimension resource might be referenced in a layout definition).

Later in this chapter, you’ll learn how to define alternative resource values for different languages, loca-tions, and hardware. It’s important to note that when using resources you cannot choose a particular specialized version. Android will automatically select the most appropriate value for a given resource identifier based on the current hardware and device settings.

Using Resources in Code

You access resources in code using the static R class. R is a generated class based on your external resources and created by compiling your project. The R class contains static subclasses for each of the resource types for which you’ve defined at least one resource. For example, the default new project includes the R.string and R.drawable subclasses.

If you are using the ADT plug-in in Eclipse, the R class will be created automatically when you make any change to an external resource file or folder. If you are not using the plug-in, use the AAPT tool to compile your project and generate the R class. R is a compiler-generated class, so don’t make any manual modifications to it as they will be lost when the file is regenerated.

Each of the subclasses within R exposes its associated resources as variables, with the variable names matching the resource identifiers — for example,

 R.string.app_name 

or

R.drawable.icon

The value of these variables is a reference to the corresponding resource’s location in the resource table, not an instance of the resource itself.

Where a constructor or method, such as setContentView, accepts a resource identifier, you can pass in the resource variable, as shown in the code snippet below:

Inflate a layout resource. setContentView(R.layout.main);

Display a transient dialog box that displays ther Error message string resource.

Toast.makeText(this, R.string.app_error, Toast.LENGTH_LONG).show();

When you need an instance of the resource itself, you’ll need to use helper methods to extract them from the resource table, represented by an instance of the Resources class.

Because these methods perform lookups on the application’s resource table, these helper methods can’t be static. Use the getResources method on your application context as shown in the snippet below to access your application’s Resource instance:

Resources myResources = getResources();

The Resources class includes getters for each of the available resource types and generally works by passing in the resource ID you’d like an instance of. The following code snippet shows an example of using the helper methods to return a selection of resource values:

Resources myResources = getResources();

CharSequence styledText = myResources.getText(R.string.stop_message); Drawable icon = myResources.getDrawable(R.drawable.app_icon);

int opaqueBlue = myResources.getColor(R.color.opaque_blue);

float borderWidth = myResources.getDimension(R.dimen.standard_border);

Animation tranOut;

tranOut = AnimationUtils.loadAnimation(this, R.anim.spin_shrink_fade);

String[] stringArray;

stringArray = myResources.getStringArray(R.array.string_array);

int[] intArray = myResources.getIntArray(R.array.integer_array);

Frame-by-frame animated resources are inflated into AnimationResources. You can return the value using getDrawable and casting the return value as shown below:

AnimationDrawable rocket;


rocket = (AnimationDrawable)myResources.getDrawable(R.drawable.frame_by_frame);

|At the time of going to print, there is a bug in the AnimationDrawable class. Currently, AnimationDrawable resources are not properly loaded until |some time after an Activity’s onCreate method has completed. Current work-arounds use timers to force a delay before loading a frame-by-frame |resource.

Referencing Resources in Resources

You can also reference resources to use as attribute values in other XML resources.

This is particularly useful for layouts and styles, letting you create specialized variations on themes and localized strings and graphics. It’s also a useful way to support different images and spacing for a layout to ensure that it’s optimized for different screen sizes and resolutions.

To reference one resource from another, use @ notation, as shown in the following snippet:

attribute=”@[packagename:]resourcetype/resourceidentifier”

Android will assume you’re using a resource from the same package, so you only need to fully qualify the package name if you’re using a resource from a different package.

The following snippet creates a layout that uses color, dimension, and string resources:

<?xml version=”1.0” encoding=”utf-8”?>

<LinearLayout

xmlns:android=”http://schemas.android.com/apk/res/android”

android:orientation=”vertical”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:padding=”@dimen/standard_border”>

<EditText

android:id=”@+id/myEditText”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”@string/stop_message”

android:textColor=”@color/opaque_blue”

/>

</LinearLayout>

Using System Resources

The native Android applications externalize many of their resources, providing you with various strings, images, animations, styles, and layouts to use in your applications.

Accessing the system resources in code is similar to using your own resources. The difference is that you use the native android resource classes available from android.R, rather than the application-specific R class. The following code snippet uses the getString method available in the application context to retrieve an error message available from the system resources:

CharSequence httpError = getString(android.R.string.httpErrorBadUrl);

To access system resources in XML, specify Android as the package name, as shown in this XML snippet:

<EditText

android:id=”@+id/myEditText”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content” android:text=”@android:string/httpErrorBadUrl” android:textColor=”@android:color/darker_gray”

/>