Available Resource Types

This page describes the different types of resources that you can externalize from your code and package with your application.

For more details on how to use resources in your application, please see the Resources and Internationalization documentation.

Simple Values

All simple resource values can be expressed as a string, using various formats to unambiguously indicate the type of resource being created. For this reason, these values can be defined both as standard resources (under res/values/), as well as direct values supplied for mappings in styles and themes, and attributes in XML files such as layouts.

Color Values

A color value specifies an RGB value with an alpha channel, which can be used in various places such as specifying a solid color for a Drawable or the color to use for text. A color value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:

If you want to retrieve the color represented by a resource ID, you can call the Resources.getColor() method.

Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <color> tags.

Resource source file location: res/values/colors.xml (File name is arbitrary.)

Compiled resource datatype: Resource pointer to a Java int.

Resource reference name:

Syntax

<color name=color_name>#color_value</color>
<color>
Value is a color, using web-style syntax, as describe above. Has only one attribute:
  • name - The name used in referring to this color.

Example XML Declaration

The following code declares two colors, the first fully opaque, and the second translucent.

<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
</resources>

Example Code Use

Example Java code

// Retrieve a color value.
int color = getResources.getColor(R.color.opaque_red);

Example XML code

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAlign="center"
          android:textColor="@color/translucent_red"
          android:text="Some Text"/>

Strings and Styled Text

Strings, with optional simple formatting, can be stored and retrieved as resources. You can add formatting to your string by using three standard HTML tags: <b>, <i>, and <u>. To guarantee getting an unstyled string only (the raw text) call the toString() method of the retrieved CharSequence object. Methods that accept string resources should be able to process these styling tags.

If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.

Note: If you use an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other kind of enclosing quotes:

<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This won't work!</string>
<string name="bad_example_2">XML encodings won&apos;t work either!</string>

Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <string> tags.

Resource source file location: res/values/strings.xml (File name is arbitrary.)

Compiled resource datatype: Resource pointer to a Java CharSequence.

Resource reference name:

Syntax

<string name=string_name>string_value</string>
<string>
Value is a string, with optional styling tags. Has only one attribute:
  • name - The name used in referring to this string.

Example XML Declaration

The following declares two strings: the first — simple text with no formatting (resulting in a CharSequence that is simply a String object) — the second includes formatting information in the string (resulting in a CharSequence that is a complex data structure). If you are using the custom editor for string files in Eclipse, the HTML formatting tags will automatically be escaped and you will need to use Context.getString() and fromHtml(String) to retreive the resource and then convert it to formatted text.

<resources>
   <string name="simple_welcome_message">Welcome!</string>
   <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>

Example Code Use

Example Java code

// Assign a styled string resource to a TextView
// on the current screen.
CharSequence str = getString(R.string.styled_welcome_message);
TextView tv = (TextView)findViewByID(R.id.text);
tv.setText(str);

Example XML code

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAlign="center"
          android:text="@string/simple_welcome_message"/> 

Using Styled Text as a Format String

Sometimes you may want to create a styled text resource that is also used as a format string. This cannot be done directly because there is no way of passing the styled text as the format string argument of String.format() without stripping out the style information. The workaround is to store the style tags as escaped HTML tags, and then convert the escaped HTML string into a styled text after formatting has taken place.

To use styled text as a format string, do the following.

  1. Store your styled text resource as an escaped string, so that the HTML tags in your text resource are not interpreted as if they were XML tags:
    <resources>
      <string name="search_results_resultsTextFormat">%1$d results for &lt;b>&amp;quot;%2$s&amp;quot;&lt;/b></string>
    </resources>
    

    In this example the format string has two arguments: %1$d is a decimal number, %2$s is a string.

  2. Make sure any String arguments are properly escaped if they might contain '<' or '&' characters. The htmlEncode(String) method will do this:
    String escapedTitle = TextUtil.htmlEncode(title);
    
  3. Use String.format() to format the HTML text, then use fromHtml(String) to convert the HTML text into styled text:
    String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
    String resultsText = String.format(resultsTextFormat, count, escapedTitle);
    CharSequence styledResults = Html.fromHtml(resultsText);
    
None