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 when your project is compiled. 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 ivhen 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 following code snippet.

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

// Display a transient dialog box that displays the // error message string resource.

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

When you need an instance of the resource itself, you'll need to use helper methods to extract them from the resource table. The resource table is represented within your application as 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 following snippet, to access your application's Resources 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 here:

AnimationDrawable rocket;

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

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment