Understanding Resources

Resources are no fly-by-night type of Android idiom. They're first-class citizens in the Android platform. In Android, resources can be any of the following:

  • Layouts
  • Strings
  • Images
  • Dimensions
  • Styles
  • Themes
  • Values
  • Menus
  • Colors

You've already been introduced to layouts, strings, and images because they are the most common types of resources that you will use in everyday Android application development. The other resources may be a little muddy for you, so let me clear those up here.

Dimensions

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

  • dp (density-independent pixels): I use this unit of measure most when I'm developing my layouts. This abstract unit is based on the physical density of the screen. These units are relative to a 160-dots-per-inch (dpi) screen; therefore, 1 dp is equivalent to one pixel on a 160-dpi screen. The ratio of dp to pixels changes with screen density, but not necessarily within proportion. The dp topic is quite in-depth and should be investigated if you plan to actively support multiple screen densities. Density is similar to screen resolution except that density refers to the number of pixels you can squeeze onto the screen. Squeezing more pixels into the screen space (essentially by making pixels smaller) changes a high-resolution screen into a high-density screen. You can read more information about this topic at Supporting Multiple Screen Sizes, located here: http://developer.android.com/guide/practices/screens_support.html.
  • sp (scale-independent pixels): This unit is like the dp unit but is scaled according to the user's font-size preference. You should use sp dimensions when specifying font sizes in your application.
  • pt (points): A point is 1/72 inch, based on the physical size of the screen.
  • px (pixels): These correspond to actual pixels on the screen. This unit of measure is not recommended because your app may look great on a medium-density device but look very distorted and out of place on a high-density screen (and vice versa) because the dpi differs on both devices.
  • mm (millimeters): Based on the size of the screen.
  • in (inches): Based on the physical size of the screen.

Styles

Styles allow you to — well, you guessed it — style your application! In Android, styles are very similar to Cascading Style Sheets (CSS) in the web development realm. A style is a collection of properties that can be applied to any individual view (within the layout file) or activity, or to your entire application (from within the manifest file). Styles support inheritance, so you can provide a very basic style and then modify it for each particular use you have in your application. Example style properties include font size, font color, and screen background.

Themes

A theme is a style applied to an entire activity or application, rather than just an individual view. When a style is applied as a theme, every view in the activity or application inherits the style settings. For example, setting all TextView views in the theme to be a particular font makes all views in the themed activity or application display text in that font.

Values

Values can contain many different types of value type resources for your application, including the following:

  • Bool: A Boolean value defined in XML whose value is stored with an arbitrary filename in the res/values/<filename>.xml file, where <filename> is the name of the file. An example is bools.xml.
  • 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.
  • 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 so on.
  • Typed array: A typed array is used to create an array of resources, such as drawables. You can create arrays of mixed types, so the arrays are not required to be homogeneous — however, you must be aware of the array item data type so that you can appropriately cast it. As with the others, the filename is arbitrary in the res/values/<filename>.xml file. An example is types.xml.

Menus

Menus can either be defined through code or through XML. The preferred way to define menus is through XML, so the various menus you create should be placed into the menus/ directory. Each menu has its own .xml file.

Colors

The values/colors.xml file allows you to give colors a name, such as login_screen_font_color, to, say, define the color of the font you use in the logon page. 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
3.147.83.176