page.title=More Resource Types parent.title=Resource Types parent.link=available-resources.html @jd:body
This page defines more types of resources you can externalize, including:
A boolean value defined in XML.
Note: A bool is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine bool resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.bool.bool_name
@[package:]bool/bool_name
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="bool_name" >[true | false]</bool> </resources>
res/values-small/bools.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="screen_small">true</bool> <bool name="adjust_view_bounds">true</bool> </resources>
This application code retrieves the boolean:
Resources res = {@link android.content.Context#getResources()}; boolean screenIsSmall = res.{@link android.content.res.Resources#getBoolean(int) getBoolean}(R.bool.screen_small);
This layout XML uses the boolean for an attribute:
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:src="@drawable/logo" android:adjustViewBounds="@bool/adjust_view_bounds" />
A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, {@code android:drawable="@color/green"}).
The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:
Note: A color is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine color resources with other simple resources in the one XML file,
under one {@code
res/values/colors.xml
R.color.color_name
@[package:]color/color_name
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="color_name" >hex_color</color> </resources>
res/values/colors.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <color name="translucent_red">#80ff0000</color> </resources>
This application code retrieves the color resource:
Resources res = {@link android.content.Context#getResources()}; int color = res.{@link android.content.res.Resources#getColor(int) getColor}(R.color.opaque_red);
This layout XML applies the color to an attribute:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/translucent_red" android:text="Hello"/>
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:
Note: A dimension is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine dimension resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.dimen.dimension_name
@[package:]dimen/dimension_name
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="dimension_name" >dimension</dimen> </resources>
res/values/dimens.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="ball_radius">30dp</dimen> <dimen name="font_size">16sp</dimen> </resources>
This application code retrieves a dimension:
Resources res = {@link android.content.Context#getResources()}; float fontSize = res.{@link android.content.res.Resources#getDimension(int) getDimension}(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/>
A unique resource ID defined in XML. Using the name you provide in the {@code
Note: An ID is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine ID resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.id.name
@[package:]id/name
<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="id_name" /> </resources>
XML file saved at res/values/ids.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="button_ok" /> <item type="id" name="dialog_exit" /> </resources>
Then, this layout snippet uses the "button_ok" ID for a Button widget:
<Button android:id="@id/button_ok" style="@style/button_style" />
Notice that the {@code android:id} value does not include the plus sign in the ID reference, because the ID already exists, as defined in the {@code ids.xml} example above. (When you specify an ID to an XML resource using the plus sign—in the format {@code android:id="@+id/name"}—it means that the "name" ID does not exist and should be created.)
As another example, the following code snippet uses the "dialog_exit" ID as a unique identifier for a dialog:
{@link android.app.Activity#showDialog(int) showDialog}(R.id.dialog_exit);
In the same application, the "dialog_exit" ID is compared when creating a dialog:
protected Dialog {@link android.app.Activity#onCreateDialog(int)}(int id) { Dialog dialog; switch(id) { case R.id.dialog_exit: ... break; default: dialog = null; } return dialog; }
An integer defined in XML.
Note: An integer is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine integer resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.integer.integer_name
@[package:]integer/integer_name
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="integer_name" >integer</integer> </resources>
XML file saved at res/values/integers.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="max_speed">75</integer> <integer name="min_speed">5</integer> </resources>
This application code retrieves an integer:
Resources res = {@link android.content.Context#getResources()}; int maxSpeed = res.{@link android.content.res.Resources#getInteger(int) getInteger}(R.integer.max_speed);
An array of integers defined in XML.
Note: An integer array is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine integer array resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.array.integer_array_name
@[package:]array.integer_array_name
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="integer_array_name"> <item >integer</item> </integer-array> </resources>
res/values/integers.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="bits"> <item>4</item> <item>8</item> <item>16</item> <item>32</item> </integer-array> </resources>
This application code retrieves the integer array:
Resources res = {@link android.content.Context#getResources()}; int[] bits = res.{@link android.content.res.Resources#getIntArray(int) getIntArray}(R.array.bits);
A {@link android.content.res.TypedArray} defined in XML. You can use this to create an array of other resources, such as drawables. Note that the array is not required to be homogeneous, so you can create an array of mixed resource types, but you must be aware of what and where the data types are in the array so that you can properly obtain each item with the {@link android.content.res.TypedArray}'s {@code get...()} methods.
Note: A typed array is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). As
such, you can combine typed array resources with other simple resources in the one XML file,
under one {@code
res/values/filename.xml
R.array.array_name
@[package:]array.array_name
<?xml version="1.0" encoding="utf-8"?> <resources> <array name="integer_array_name"> <item>resource</item> </array> </resources>
res/values/arrays.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <array name="icons"> <item>@drawable/home</item> <item>@drawable/settings</item> <item>@drawable/logout</item> </array> <array name="colors"> <item>#FFFF0000</item> <item>#FF00FF00</item> <item>#FF0000FF</item> </array> </resources>
This application code retrieves each array and then obtains the first entry in each array:
Resources res = {@link android.content.Context#getResources()}; TypedArray icons = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.icons); Drawable drawable = icons.{@link android.content.res.TypedArray#getDrawable(int) getDrawable}(0); TypedArray colors = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.colors); int color = colors.{@link android.content.res.TypedArray#getColor(int,int) getColor}(0,0);