Creating Resources
Android supports string, bitmap, and many other types of resource. The syntax and format
of each, and where they're stored, depends upon the type of object. In
general, though, you create resources from three types of files: XML files
(everything but bitmaps and raw), bitmap files(for images) and Raw files (anything
else, for example sound files, etc.). In fact, there are two different types of
XML file as well, those that get compiled as-is into the package, and those that
are used to generate resources by aapt. Here is a list of each
resource type, the format of the file, a description of the file, and details
of any XML files.
You will create and store your resource files under the appropriate
subdirectory under the res/
directory in your project. Android
has a resource compiler (aapt) that compiles resources according to which
subfolder they are in, and the format of the file. Table 1 shows a list of the file
types for each resource. See the
Available Resources for
descriptions of each type of object, the syntax, and the format or syntax of
the containing file.
Table 1
Directory |
Resource Types |
res/anim/ |
XML files that are compiled into
frame by
frame animation or
tweened
animation objects |
res/drawable/ |
.png, .9.png, .jpg files that are compiled into the following
Drawable resource subtypes:
To get a resource of this type, use mContext.getResources().getDrawable(R.drawable.imageId)
Note: Image resources placed in here may
be automatically optimized with lossless image compression by the
aapt tool. For example, a true-color PNG
that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette.
This will result in an image of equal quality but which requires less memory. So be aware that the
image binaries placed in this directory can change during the build. If you plan on reading
an image as a bit stream in order to convert it to a bitmap, put your images in the
res/raw/ folder instead, where they will not be optimized.
|
res/layout/ |
XML files that are compiled into screen layouts (or part of a screen).
See Declaring Layout. |
res/values/ |
XML files that can be compiled into many kinds of resource.
Note: Unlike the other res/ folders, this one
can hold any number of files that hold descriptions of resources to create
rather than the resources themselves. The XML element types control
where these resources are placed under the R class.
While the files can be named anything, these are
the typical files in this folder (the convention is to name
the file after the type of elements defined within):
- arrays.xml to define arrays
- colors.xml to define color
drawables and color string values.
Use
Resources.getDrawable() and
Resources.getColor(), respectively,
to get these resources.
- dimens.xml to define dimension value. Use
Resources.getDimension() to get
these resources.
- strings.xml to define string values (use either
Resources.getString or preferably Resources.getText()
to get
these resources. getText() will retain any rich text styling
which is usually desirable for UI strings.
- styles.xml to define style objects.
|
res/xml/ |
Arbitrary XML files that are compiled and can be read at run time by
calling Resources.getXML(). |
res/raw/ |
Arbitrary files to copy directly to the device. They are added uncompiled
to the compressed file that your application build produces. To use these
resources in your application, call Resources.openRawResource() with the resource ID, which is R.raw.somefilename. |
Resources are compiled into the final APK file. Android creates a wrapper class,
called R, that you can use to refer to these resources in your code. R contains subclasses
named according to the path and file name of the source file