Resource Directories and General Attributes

The UI display utilizes developer-generated resource files, some of which are discussed in Chapter 2,"Application Basics:Activities and Intents," in the context of the directory structure of an Android project. For completeness, the entire set of resource directories is summarized here:

■ res/anim/—Frame-by-frame animation or tweened animation objects.

■ res/drawable/—Image resources. Note these images can be modified and optimized during compilation.

■ res/layout/—eXtensible Markup Language (XML) files specifying screen layouts.

■ res/values/—XML files with resource descriptors.As with other resource directories, filenames are arbitrary, but common ones, as utilized in this book, are arrays.xml, colors.xml, dimens.xml, strings.xml, and styles.xml.

■ res/xml/—Other arbitrary XML files not covered previously.

■ res/raw/—Other arbitrary resources not covered previously, including images that should not be modified or optimized.

Each UI object has three definable attributes that customize the look and feel of the UI: the dimension of the object, text in the object, and the color of the object.The possible values for these three general UI attributes are summarized in Table 4.1. Note that for dimension, it is best to use dp or sp for device-independent compliance.

Table 4.1 Possible Values for the Three General UI Attributes Attribute Possible Values

Dimension Any number followed by one of the following dimensions: px—Actual pixels on the screen dp (or dip)—Device-independent pixels relative to a 160dpi screen sp—Device-independent pixels scaled by user's font size preference in—Inches based on physical screen size mm—Millimeters based on physical screen size pt—1/72 of an inch based on physical screen size String Any string, as long as apostrophes/quotes are escaped: Don\'t worry

Any properly quoted string: "Don't worry" Any formatted string, for example: Population: %1$d Can include HTML tags, such as <b>, <i>, or <u> Can include special characters, such as © given by &#169; Color Possible values are a 12-bit color #rgb, 16-bit color with alpha opacity #argb,

24-bit color #rrggbb, or 32-bit color with alpha opacity #aarrggbb. It is also possible to utilize the predefined colors in the Color class within Java files, such as Color.CYAN.

To unify the look and feel of the application, a global resource file can be used for each of these attributes.This is also useful in that it is easy to redefine the attributes later, as they are all collected in three files:

■ Measurements and dimensions of items are declared in the XML resource file res/values/dimens.xml. For example:

■ XML declaration—<dimen name="large">48sp</dimen>

■ Java reference—getResources().getDimension(R.dimen.large)

■ Label and text of items are declared in the XML resource file res/values/strings.xml. For example:

■ XML declaration—<string name="start_pt">I\'m here</string>

■ Java reference—getBaseContext().getString(R.string.start_pt)

■ The colors of items are declared in the XML resource file res/values/colors.xml. For example:

■ XML declaration—<color name="red">#f00</color>

■ Java reference—getResources().getColor(

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment