Enumerating Key Android Resources

Now that we've been through the basics of resources, we'll enumerate some of the other key resources that Android supports, their XML representations, and the way they're used in Java code. (You can use this section as a quick reference as you write resource files for each resource.) To start with, take a quick glance at the types of resources and what they are used for (see Table 3-1).

Table 3-1. Types of Resources

Resource Type Location

Description

Color

String

/res/values/any-file

/res/values/any-file

Dimension /res/values/any-file

Image

/res/drawable/multiple-files

Color Drawable

/res/values/any-file also

/res/drawable/multiple-files

Arbitrary XML Files

Arbitrary Raw /res/raw/*.* Resources

Represents color identifiers pointing to color codes. These resource IDs are exposed in R.java as R.color.*.

Represents string resources. String resources allow Java-formatted strings and raw HTML in addition to simple strings. These resource IDs are exposed in R.java as R.string.*.

Represents dimensions or sizes of various elements or views in Android. Supports pixels, inches, millimeters, density-independent pixels, and scale-independent pixels. These resource IDs are exposed in R.java as R.dimen.*.

Represents image resources. Supported images include .jpg, .gif, and .png. Each image is in a separate file and gets its own ID based on the file name. These resource IDs are exposed in R.java as R.drawable.*. The image support also includes an image type called a stretchable image that allows portions of an image to stretch while other portions of that image stay static.

Represents rectangle of colors to be used as view backgrounds or general drawables like bitmaps. You can use this instead of specifying a single colored bitmap as a background. In Java, this is equivalent to creating a colored rectangle and setting it as a background for a view. The <drawable> value tag in the values subdirectory supports this. These resource IDs are exposed in R.java as R.drawable.*. Android also supports rounded rectangles and gradient rectangles through XML files placed in /res/drawable with the root XML tag <shape>. These resource IDs are also exposed in R.java as R.drawable.*. Each file name in this case translates to a unique drawable ID.

Android allows arbitrary XML files as resources. These files will be compiled by the AAPT compiler. These resource IDs are exposed in R.java as R.xml.*.

Android allows arbitrary noncompiled binary or text files under this directory. Each file gets a unique resource ID. These resource IDs are exposed in R.java as R.raw.*.

Android allows arbitrary files in arbitrary subdirectories, starting at the /assets subdirectory. These are not really resources, but raw files. This directory, unlike the /res subdirectory, allows an arbitrary depth of subdirectories. These files do not generate any resource IDs. You have to use a relative path name starting at and excluding /assets.

Each of the resources specified in this table are further elaborated in the following sections with XML and Java code snippets.

0 0

Post a comment