Using Resources in Code

Using resources in code is just a matter of knowing the full resource ID and what type of object your resource has been compiled into. Here is the syntax for referring to a resource:

R.resource_type.resource_name or android.R.resource_type.resource_name

Where resource_type is the R subclass that holds a specific type of resource. resource_name is the name attribute for resources defined in XML files, or the file name (without the extension) for resources defined by other file types. Each type of resource will be added to a specific R subclass, depending on the type of resource it is; to learn which R subclass hosts your compiled resource type, consult the Available Resources document. Resources compiled by your own application can be referred to without a package name (simply as

R.resource_type.resource_name). Android contains a number of standard resources, such as screen styles and button backgrounds. To refer to these in code, you must qualify them with android, as in android.R.drawable.button_background.

Here are some good and bad examples of using compiled resources in code:

// Load a background for the current screen from a drawable resource. this.getWindow().setBackgroundDrawableResource(R.drawable.my background image);

// WRONG Sending a string resource reference into a // method that expects a string.

this.getWindow().setTitle(R.string.main title);

// RIGHT Need to get the title from the Resources wrapper. this.getWindow().setTitle(Resources.getText(R.string.main title));

// Load a custom layout for the current screen. setContentView(R.layout.main screen);

// Set a slide in animation for a ViewFlipper object. mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace in));

// Set the text on a TextView object.

TextView msgTextView = (TextView)findViewByID(R.id.msg); msgTextView.setText(R.string.hello message);

References to Resources

A value supplied in an attribute (or resource) can also be a reference to a resource. This is often used in layout files to supply strings (so they can be localized) and images (which exist in another file), though a reference can be any resource type including colors and integers.

For example, if we have color resources, we can write a layout file that sets the text color size to be the value contained in one of those resources:

<?xml version="1.0" encoding="utf-8"?> <EditText id="text"

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="Hello, World!" />

Note here the use of the '@' prefix to introduce a resource reference -- the text following that is the name of a resource in the form of @[package:]type/name. In this case we didn't need to specify the package because we are referencing a resource in our own package. To reference a system resource, you would need to write:

<?xml version="1.0" encoding="utf-8"?> <EditText id="text"

xmlns:android="http://schemas.android.com/apk/res/android" android:layout width="fill parent" android:layout height="fill parent" android:textColor="@android:color/opaque red" android:text="Hello, World!" />

As another example, you should always use resource references when supplying strings in a layout file so that they can be localized:

<?xml version="1.0" encoding="utf-8"?> <EditText id="text"

xmlns:android="http://schemas.android.com/apk/res/android" android:layout width="fill parent" android:layout height="fill parent" android:textColor="@android:color/opaque red" android:text="@string/hello world" />

This facility can also be used to create references between resources. For example, we can create new drawable resources that are aliases for existing images:

http://developer.android.com/guide/topics/resources/resources-i18n.html Page 4 of 9

<?xml version="1.0" encoding="utf-8"?> <resources>

<drawable id="my_background">@android:drawable/theme2_background</drawable> </resources>

0 0

Post a comment