By Michael Burton

Resources are additional static content that are an intrinsic part of your Android app but aren’t part of your Java code. The most common resources are as follows:

  • Layout

  • String

  • Image

  • Dimension

  • Style

  • Theme

  • Value

  • Menu

  • Color

Layouts, strings, and images are the most common types of resources that you use in everyday Android application development. The remaining resources may need some explanation, so the following few paragraphs will clear them up.

Dimensions

In an Android resource, a dimension is a number followed by a unit of measurement, such as 10px, 2.5in, 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. Android supports the following units of measure:

  • density-independent pixel (dp or dip): This is the most commonly used dimension. Dp 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.

  • scale-independent pixel (sp or sip): 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.

  • pixel (px): A pixel corresponds to a pixel on the screen. This unit of measure isn’t recommended for most cases. 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.

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

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

  • 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 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 attribute examples include text size, text color, and 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

  • 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.

  • 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 other elements.

  • Typed array: An array 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, typically located in the values/colors.xml file, lets you name colors, such as login_screen_font_color. This might depict the color of the text you’re using on the logon page, for example. Each color is defined as a hexadecimal value.