Supported simple values include strings, colors, dimensions, and string or integer arrays. All simple values are stored within XML files in the res/values folder.
Within each XML file you indicate the type of value being stored using tags, as shown in the sample XML file in Listing 3-1.
a l3 Chapter2_Sample_Todo_List > |'3 src ? & Android 2.0 i> 29 gen [Generated Java Files]
assets j res a £3- drawable
[xj main.xml a values
[Xj strings.urn I q AndroidP.1anlfest.xml J| default.properties
LISTING 3-1: Simple values XML
LISTING 3-1: Simple values XML
Available for download on Wrox.com
<?xml version="1.0" encoding="utf-8"?> <resources>
<string name="app_name">To Do List</string> <color name=Mapp_backgroundM>#FF0000FF</color> <dimen name="default_border">5px</dimen> <array name="string_array"> <item>Item 1</item> <item>Item 2</item> <item>Item 3</item> </array>
<array name="integer_array"> <item>3</item> <item>2</item> <item>1</item> </array> </resources>
This example includes all the simple value types. By convention, resources are separated into different files for each type; for example, res/values/strings.xml would contain only string resources.
The following sections detail the options for defining simple resources.
Externalizing your strings helps maintain consistency within your application and makes it much easier to create localized versions.
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 described above. To apply styling to a format string you have to escape the HTML tags when creating your resource, as shown in the following.
<string name="stop_message"><b>Stop</b>. %1$s</string>
Within your code, use the Html.fromHtml method to convert this back into a styled character sequence.
String rString = getString(R.string.stop_message);
String fString = String.format(rString, "Collaborate and listen."); CharSequence styledString = Html.fromHtml(fString);
Use the <color> tag to define a new color resource. Specify the color value using a # symbol followed by the (optional) alpha channel, 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.
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 identifier describing the scale of your dimension:
> in (physical inches)
> pt (physical points)
> mm (physical millimeters)
> dp (density-independent pixels relative to a 160-dpi screen)
> sp (scale-independent pixels)
These alternatives let you define a dimension not only in absolute terms, but also 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>
Was this article helpful?
Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.