It’s always good practice to keep non-code resources, such as images and string constants, external to your code. Android supports the externalization of resources, ranging from simple values such as strings and colors to more complex resources such as images (Drawables), animations, themes, and menus. Perhaps the most powerful externalizable resources are layouts.
By externalizing resources, you make them easier to maintain, update, and manage. This also lets you easily define alternative resource values for internationalization and to include different resources to support variations in hardware — particularly, screen size and resolution
Application resources are stored under the res folder in your project hierarchy. Each of the available resource types is stored in subfolders, grouped by resource type.
Note that three drawable resource folders contain three different icons: one each for low, medium, and high density displays respectively.
Each resource type is stored in a different folder: simple values, Drawables, colors, layouts, animations, styles, menus, XML fi les (including searchables), and raw resources. When your application is built, these resources will be compiled and compressed as effi- ciently as possible and included in your application package
This process also generates an R class fi le that contains references to each of the resources you include in your project. This enables you to reference the resources in your code, with the advantage of design-time syntax checking
In all cases, the resource filenames should contain only lowercase letters, numbers, and the period (.) and underscore (_) symbols
Supported simple values include strings, colors, dimensions, styles, and string or integer arrays. All simple values are stored within XML fi les in the res/values folder
<?xml version=”1.0” encoding=”utf-8”?> <resources> <string name=”app_name”>To Do List</string> <plurals name=”androidPlural”> <item quantity=”one”>One android</item> <item quantity=”other”>%d androids</item> </plurals> <color name=”app_background”>#FF0000FF</color> <dimen name=”default_border”>5px</dimen> <string-array name=“string_array“> <item>Item 1</item> <item>Item 2</item> <item>Item 3</item> </string-array> <array name=“integer_array“> <item>3</item> <item>2</item> <item>1</item> </array> </resources>
Externalizing your strings helps maintain consistency within your application and makes it much easier to internationalize them
String resources are specified with the string tag, as shown in the following XML snippet:
Android supports simple text styling, so you can use the HTML tags <b>, <i>, and <u> to apply bold, italics, or underlining, respectively, to parts of your text strings, as shown in the following example:
You can use resource strings as input parameters for the String.format method. However, String.format does not support the text styling previously described. To apply styling to a format string, you have to escape the HTML tags when creating your resource, as shown in the following snippet:
<string name=”stop_message”><b>Stop</b>. %1$s</string>
Use the color tag to define a new color resource. Specify the color value using a # symbol followed by the (optional) alpha channel, and then the red, green, and blue values using one or two hexadecimal numbers with any of the following notations:
The following example shows how to specify a fully opaque blue and a partially transparent green:
<color name=”opaque_blue”>#00F</color> <color name=”transparent_green”>#7700FF00</color>
Dimensions are most commonly referenced within style and layout resources. They’re useful for creating layout constants, such as borders and font heights.
To specify a dimension resource, use the dimen tag, specifying the dimension value, followed by an identifi er describing the scale of your dimension:
- px (screen pixels)
- in (physical inches)
- pt (physical points)
- mm (physical millimeters)
- dp (density-independent pixels)
- sp (scale-independent pixels)
Although you can use any of these measurements to defi ne a dimension, it’s best practice to use either density- or scale-independent pixels. These alternatives let you defi ne a dimension using relative scales that account for different screen resolutions and densities to simplify scaling on different hardware
The following XML snippet shows how to specify dimension values for a large font size and a standard border:
<dimen name=”standard_border”>5dp</dimen> <dimen name=”large_font_size”>16sp</dimen>
Styles and Themes
Style resources let your applications maintain a consistent look and feel by enabling you to specify the attribute values used by Views. The most common use of themes and styles is to store the colors and fonts for an application
To create a style, use a style tag that includes a name attribute and contains one or more item tags. Each item tag should include a name attribute used to specify the attribute (such as font size or color) being defined. The tag itself should then contain the value, as shown in the following skeleton code.
<?xml version=”1.0” encoding=”utf-8”?> <resources> <style name=”base_text”> <item name=”android:textSize”>14sp</item> <item name=”android:textColor”>#111</item> </style> </resources>
Styles support inheritance using the parent attribute on the style tag, making it easy to create simple variations:
<?xml version=”1.0” encoding=”utf-8”?> <resources> <style name=”small_text” parent=”base_text”> <item name=”android:textSize”>8sp</item> </style> </resources>
Drawable resources include bitmaps and NinePatches (stretchable PNG images). They also include complex composite Drawables, such as LevelListDrawables and StateListDrawables, that can be defi ned in XML
All Drawables are stored as individual files in the res/drawable folder. Note that it’s good practice to store bitmap image assets in the appropriate drawable -ldpi, -mdpi, -hdpi, and -xhdpi folders, as described earlier in this chapter. The resource identifier for a Drawable resource is the lowercase fi le name without its extension.
Layout resources enable you to decouple your presentation layer from your business logic by designing UI layouts in XML rather than constructing them in code.
You can use layouts to defi ne the UI for any visual component, including Activities, Fragments, and Widgets. Once defi ned in XML, the layout must be “infl ated” into the user interface. Within an Activity this is done using setContentView (usually within the onCreate method), whereas Fragment Views are infl ated using the inflate method from the Inflator object passed in to the Fragment’s onCreateView handler.
<?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> </LinearLayout>
Defining animations as external resources enables you to reuse the same sequence in multiple places and provides you with the opportunity to present different animations based on device hardware or orientation
Create menu resources to design your menu layouts in XML, rather than constructing them in code
You can use menu resources to defi ne both Activity and context menus within your applications, and provide the same options you would have when constructing your menus in code. When defi ned in XML, a menu is infl ated within your application via the inflate method of the MenuInflator Service, usually within the onCreateOptionsMenu method
<?xml version=”1.0” encoding=”utf-8”?> <menu xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:id=”@+id/menu_refresh” android:title=”@string/refresh_mi” /> <item android:id=”@+id/menu_settings” android:title=”@string/settings_mi” /> </menu>