Working with Application Resources

Application resources are created and stored within the Android project files under the / res directory. Using a well-defined but flexible directory structure, resources are organized, defined, and compiled with the application package. Application resources are not shared with the rest of the Android system.

Storing Application Resources

Defining application data as resources is a good programming practice. Grouping application resources together and compiling them into the application package has the following benefits:

► Code is cleaner and easier to read, leading to fewer bugs.

► Resources are organized by type and guaranteed to be unique.

► Resources are conveniently located for handset customization.

► Localization and internationalization are straightforward.

The Android platform supports a variety of resource types (see Figure 4.1), which can be combined to form different types of applications.

Android applications can include many different kinds of resources. The following are some of the most common resource types:

► Strings, colors, and dimensions

► Drawable graphics files

Resource types are defined with special XML tags and organized into specially named project directories. Some / res subdirectories, such as the /drawable, /layout, and /values directories, are created by default when a new Android project is created, while others must be added by the developer when required.

Android Application Resources

Game Example: "Chippy's Revenge"

Android Application Resources

Game Example: "Chippy's Revenge"

Chippy Revenge

FIGURE 4.1

Android applications can use a variety of resources.

LAYOUT FILES (Screen User Interfaces)

LAYOUT FILES (Screen User Interfaces)

Game Screen

Game Screen

CD CD

CD CD

Help Screen

This is the help text for Chippy's Revenge, a game about collecting nuts and avoiding cats.

FIGURE 4.1

Android applications can use a variety of resources.

Resource files stored within / res subdirectories must abide by the following rules:

► Resource filenames must be lowercase.

► Resource filenames may contain letters, numbers, underscores, and periods only.

► Resource filenames (and XML name attributes) must be unique.

Did you Know?

Referencing Application Resources

All application resources are stored within the /res project directory structure and are compiled into the project at build time. Application resources can be used pro-grammatically. They can also be referenced in other application resources.

WOSt!

Application resources can be accessed programmatically using the generated class file called R.java. To reference a resource, you must retrieve the application's Resources object using the getResources() method and then make the appropriate method call, based on the type of resource you wish to retrieve.

For example, to retrieve a string named hello defined in the strings.xml resource file, you would use the following method call:

String greeting = getResources().getString(R.string.fteiio);

We will talk more about how to access different types of resources later in this hour.

To reference an application resource from another compiled resource, such as a layout file, use the following format: ^[resource type]/[resource name]

For example, the same string used earlier would be referenced as follows: @string/hello

We will talk more about referencing resources later in the hour, when we talk about layout files.

When resources are compiled, their name dictates their variable name. For example, a graphics file saved within the /drawable directory as mypic.jpg is referenced as @drawable/mypic. It is important to name resource names intelligently.

Consult the Android documentation for specific project directory naming conventions.

Each time you save a resource file (that is, copy a resource file such as a graphics file into the appropriate directory) within Eclipse, the R.java class file is recompiled to incorporate your changes. If you have not used the correct directory-or file-naming conventions, you see a compiler error in the Eclipse Problems tab.

0 0

Post a comment