Understanding Resources

Resources are the additional static content and files that are an intrinsic part of your application but aren’t part of your Java code. Resources can take these forms:

check.png Layout

check.png String

check.png Image

check.png Dimension

check.png Style

check.png Theme

check.png Value

check.png Menu

check.png Color

Earlier chapters in this book introduce you to layouts, strings, and images because they’re the most common types of resources that you use in everyday Android application development. The remaining resources may be muddy, so the following few sections clear them up.

Dimensions

In an Android resource, a dimension is a number followed by a unit of measurement, such as 10px, 2in, or 5sp. You use a dimension when specifying any property in Android that requires a numeric unit of measure. For example, you may want the padding of a layout to be 10px. The following units of measure are supported by Android:

check.png density-independent pixel (dp): This abstract unit is based on the physical density of the screen. These units are relative to a screen measuring 160 dots per inch (dpi); therefore, 1 dp is equivalent to 1 pixel on a 160 dpi screen. The ratio of dp to pixels changes with screen density, but not necessarily in proportion. This unit of measure is the one that most developers use when developing layouts.

tip.eps The dp concept is complex; if you plan to actively support multiple screen densities, the Supporting Multiple Screen Sizes article at http://developer.android.com/guide/practices/screens_support.html is a must read.

check.png scale-independent pixel (sp): This unit resembles the dp unit but is scaled according to the user’s font-size preference. Use sp dimensions when specifying font sizes in your application.

check.png pixel (px): A pixel corresponds to a pixel on the screen. This unit of measure isn’t recommended. Your app may look great on a medium- density device but look distorted and out of place on a high-density screen (and vice versa) because the dpi differs.

check.png point (pt): A point is 172 inch, based on the physical size of the screen. Like px, pt is not recommended.

check.png millimeter (mm): This unit is based on the size of the screen. Like px, mm is not recommended.

check.png inch (in): This unit is based on the physical size of the screen. Like px, in is not recommended.

Styles

Styles in Android are similar to Cascading Style Sheets (CSS) in the web development realm: You use styles to (you guessed it) style an application. A style is officially a collection of properties that can be applied to an individual view (within the layout file) or to an activity or to your entire application (from within the manifest file). Styles support inheritance, so you can provide a basic style and then modify it for each particular use case in your application. Style property examples include font size, font color, and screen background.

Themes

A theme is a style applied to an entire activity or application, rather than an individual view. When a style is applied as a theme, every view in the activity and/or application inherits the style settings. For example, you can set all TextView views to a particular font, and all views in the themed activity or application then display their text in that font.

Values

The value resource can contain many different types of value type resources for your application, including

check.png Bool: A Boolean value defined in XML whose value is stored in an arbitrary filename in the res/values/ <filename>.xml file, where <filename> is the name of the file. An example is bools.xml.

check.png Integer: An integer value defined in XML whose value is stored with an arbitrary filename in the res/values/ <filename>.xml file. An example is integers.xml.

check.png Integer array: An array of integers defined in XML whose set of values is stored with an arbitrary name in the res/values/ <filename>.xml file, where <filename> is the name of the file. An example is integers.xml. You can reference and use these integers in your code to help define loops, lengths, and other elements.

check.png Typed array: A typed array is used to create an array of resources, such as drawables. You can create arrays of mixed types. Therefore, the arrays aren’t required to be homogeneous — however, you must be aware of the data type so that you can appropriately cast it. As with other resources, the filename is arbitrary in the res/values/ <filename>.xml file. An example is types.xml.

Menus

Whether your app is using the action bar or a menu, Android treats them both the same and you’ll define them the same way. A menu can be defined via either code or XML. The preferred way to define one is via XML; therefore, the various menus you create should be placed into the menu/ directory. Each menu has its own .xml file.

Colors

The colors file, located in the values/colors.xml file, lets you name colors, such as login_screen_font_color. This might depict the color of the font you’re using on the logon page, for example. Each color is defined as a hexadecimal value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset