M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions

View File

@ -0,0 +1,341 @@
page.title=Accessing Resources
parent.title=Application Resources
parent.link=index.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>Quickview</h2>
<ul>
<li>Resources can be referenced from code using integers from {@code R.java}, such as
{@code R.drawable.myimage}</li>
<li>Resources can be referenced from resources using a special XML syntax, such as {@code
&#64;drawable/myimage}</li>
<li>You can also access your app resources with methods in
{@link android.content.res.Resources}</li>
</ul>
<h2>Key classes</h2>
<ol>
<li>{@link android.content.res.Resources}</li>
</ol>
<h2>In this document</h2>
<ol>
<li><a href="#ResourcesFromCode">Accessing Resources from Code</a></li>
<li><a href="#ResourcesFromXml">Accessing Resources from XML</a>
<ol>
<li><a href="#ReferencesToThemeAttributes">Referencing style attributes</a></li>
</ol>
</li>
<li><a href="#PlatformResources">Accessing Platform Resources</a></li>
</ol>
<h2>See also</h2>
<ol>
<li><a href="providing-resources.html">Providing Resources</a></li>
<li><a href="available-resources.html">Resource Types</a></li>
</ol>
</div>
</div>
<p>Once you provide a resource in your application (discussed in <a
href="providing-resources.html">Providing Resources</a>), you can apply it by
referencing its resource ID. All resource IDs are defined in your project's {@code R} class, which
the {@code aapt} tool automatically generates.</p>
<p>When your application is compiled, {@code aapt} generates the {@code R} class, which contains
resource IDs for all the resources in your {@code
res/} directory. For each type of resource, there is an {@code R} subclass (for example,
{@code R.drawable} for all drawable resources) and for each resource of that type, there is a static
integer (for example, {@code R.drawable.icon}). This integer is the resource ID that you can use
to retrieve your resource.</p>
<p>Although the {@code R} class is where resource IDs are specified, you should never need to
look there to discover a resource ID. A resource ID is always composed of:</p>
<ul>
<li>The <em>resource type</em>: Each resource is grouped into a "type," such as {@code
string}, {@code drawable}, and {@code layout}. For more about the different types, see <a
href="available-resources.html">Resource Types</a>.
</li>
<li>The <em>resource name</em>, which is either: the filename,
excluding the extension; or the value in the XML {@code android:name} attribute, if the
resource is a simple value (such as a string).</li>
</ul>
<p>There are two ways you can access a resource:</p>
<ul>
<li><strong>In code:</strong> Using an static integer from a sub-class of your {@code R}
class, such as:
<pre class="classic no-pretty-print">R.string.hello</pre>
<p>{@code string} is the resource type and {@code hello} is the resource name. There are many
Android APIs that can access your resources when you provide a resource ID in this format. See
<a href="#ResourcesFromCode">Accessing Resources in Code</a>.</p>
</li>
<li><strong>In XML:</strong> Using a special XML syntax that also corresponds to
the resource ID defined in your {@code R} class, such as:
<pre class="classic no-pretty-print">&#64;string/hello</pre>
<p>{@code string} is the resource type and {@code hello} is the resource name. You can use this
syntax in an XML resource any place where a value is expected that you provide in a resource. See <a
href="#ResourcesFromXml">Accessing Resources from XML</a>.</p>
</li>
</ul>
<h2 id="ResourcesFromCode">Accessing Resources in Code </h2>
<p>You can use a resource in code by passing the resource ID as a method parameter. For
example, you can set an {@link android.widget.ImageView} to use the {@code res/drawable/myimage.png}
resource using {@link android.widget.ImageView#setImageResource(int) setImageResource()}:</p>
<pre>
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(<strong>R.drawable.myimage</strong>);
</pre>
<p>You can also retrieve individual resources using methods in {@link
android.content.res.Resources}, which you can get an instance of
with {@link android.content.Context#getResources()}.</p>
<div class="sidebox-wrapper">
<div class="sidebox">
<h2>Access to Original Files</h2>
<p>While uncommon, you might need access your original files and directories. If you do, then
saving your files in {@code res/} won't work for you, because the only way to read a resource from
{@code res/} is with the resource ID. Instead, you can save your resources in the
{@code assets/} directory.</p>
<p>Files saved in the {@code assets/} directory are <em>not</em> given a resource
ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can
query files in the {@code assets/} directory like a normal file system and read raw data using
{@link android.content.res.AssetManager}.</p>
<p>However, if all you require is the ability to read raw data (such as a video or audio file),
then save the file in the {@code res/raw/} directory and read a stream of bytes using {@link
android.content.res.Resources#openRawResource(int) openRawResource()}.</p>
</div>
</div>
<h3>Syntax</h3>
<p>Here's the syntax to reference a resource in code:</p>
<pre class="classic no-pretty-print">
[<em>&lt;package_name&gt;</em>.]R.<em>&lt;resource_type&gt;</em>.<em>&lt;resource_name&gt;</em>
</pre>
<ul>
<li><em>{@code &lt;package_name&gt;}</em> is the name of the package in which the resource is located (not
required when referencing resources from your own package).</li>
<li><em>{@code &lt;resource_type&gt;}</em> is the {@code R} subclass for the resource type.</li>
<li><em>{@code &lt;resource_name&gt;}</em> is either the resource filename
without the extension or the {@code android:name} attribute value in the XML element (for simple
values).</li>
</ul>
<p>See <a href="available-resources.html">Resource Types</a> for
more information about each resource type and how to reference them.</p>
<h3>Use cases</h3>
<p>There are many methods that accept a resource ID parameter and you can retrieve resources using
methods in {@link android.content.res.Resources}. You can get an instance of {@link
android.content.res.Resources} with {@link android.content.Context#getResources
Context.getResources()}.</p>
<p>Here are some examples of accessing resources in code:</p>
<pre>
// Load a background for the current screen from a drawable resource
{@link android.app.Activity#getWindow()}.{@link
android.view.Window#setBackgroundDrawableResource(int)
setBackgroundDrawableResource}(<strong>R.drawable.my_background_image</strong>) ;
// Set the Activity title by getting a string from the Resources object, because
// this method requires a CharSequence rather than a resource ID
{@link android.app.Activity#getWindow()}.{@link android.view.Window#setTitle(CharSequence)
setTitle}(getResources().{@link android.content.res.Resources#getText(int)
getText}(<strong>R.string.main_title</strong>));
// Load a custom layout for the current screen
{@link android.app.Activity#setContentView(int)
setContentView}(<strong>R.layout.main_screen</strong>);
// Set a slide in animation by getting an Animation from the Resources object
mFlipper.{@link android.widget.ViewAnimator#setInAnimation(Animation)
setInAnimation}(AnimationUtils.loadAnimation(this,
<strong>R.anim.hyperspace_in</strong>));
// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(<strong>R.id.msg</strong>);
msgTextView.{@link android.widget.TextView#setText(int)
setText}(<strong>R.string.hello_message</strong>);
</pre>
<p class="caution"><strong>Caution:</strong> You should never modify the {@code
R.java} file by hand&mdash;it is generated by the {@code aapt} tool when your project is
compiled. Any changes are overridden next time you compile.</p>
<h2 id="ResourcesFromXml">Accessing Resources from XML</h2>
<p>You can define values for some XML attributes and elements using a
reference to an existing resource. You will often do this when creating layout files, to
supply strings and images for your widgets.</p>
<p>For example, if you add a {@link android.widget.Button} to your layout, you should use
a <a href="string-resource.html">string resource</a> for the button text:</p>
<pre>
&lt;Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="<strong>@string/submit</strong>" /&gt;
</pre>
<h3>Syntax</h3>
<p>Here is the syntax to reference a resource in an XML resource:</p>
<pre class="classic no-pretty-print">
&#64;[<em>&lt;package_name&gt;</em>:]<em>&lt;resource_type&gt;</em>/<em>&lt;resource_name&gt;</em>
</pre>
<ul>
<li>{@code &lt;package_name&gt;} is the name of the package in which the resource is located (not
required when referencing resources from the same package)</li>
<li>{@code &lt;resource_type&gt;} is the
{@code R} subclass for the resource type</li>
<li>{@code &lt;resource_name&gt;} is either the resource filename
without the extension or the {@code android:name} attribute value in the XML element (for simple
values).</li>
</ul>
<p>See <a href="available-resources.html">Resource Types</a> for
more information about each resource type and how to reference them.</p>
<h3>Use cases</h3>
<p>In some cases you must use a resource for a value in XML (for example, to apply a drawable image
to a widget), but you can also use a resource in XML any place that accepts a simple value. For
example, if you have the following resource file that includes a <a
href="more-resources.html#Color">color resource</a> and a <a
href="string-resource.html">string resource</a>:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;color name="opaque_red">#f00&lt;/color>
&lt;string name="hello">Hello!&lt;/string>
&lt;/resources>
</pre>
<p>You can use these resources in the following layout file to set the text color and
text string:</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;
android:textColor=&quot;<strong>&#64;color/opaque_red</strong>&quot;
android:text=&quot;<strong>&#64;string/hello</strong>&quot; /&gt;
</pre>
<p>In this case you don't need to specify the package name in the resource reference because the
resources are from your own package. To
reference a system resource, you would need to include the package name. For example:</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;
android:textColor=&quot;<strong>&#64;android:color/secondary_text_dark</strong>&quot;
android:text=&quot;&#64;string/hello&quot; /&gt;
</pre>
<p class="note"><strong>Note:</strong> You should use string resources at all times, so that your
application can be localized for other languages. For information about creating alternative
resources (such as localized strings), see <a
href="providing-resources.html#AlternativeResources">Providing Alternative
Resources</a>.</p>
<p>You can even use resources in XML to create aliases. For example, you can create a
drawable resource that is an alias for another drawable resource:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/other_drawable" />
</pre>
<p>This sounds redundant, but can be very useful when using alternative resource. Read more about
<a href="providing-resources.html#AliasResources">Creating alias resources</a>.</p>
<h3 id="ReferencesToThemeAttributes">Referencing style attributes</h3>
<p>A style attribute resource allows you to reference the value
of an attribute in the currently-applied theme. Referencing a style attribute allows you to
customize the look of UI elements by styling them to match standard variations supplied by the
current theme, instead of supplying a hard-coded value. Referencing a style attribute
essentially says, "use the style that is defined by this attribute, in the current theme."</p>
<p>To reference a style attribute, the name syntax is almost identical to the normal resource
format, but instead of the at-symbol ({@code &#64;}), use a question-mark ({@code ?}), and the
resource type portion is optional. For instance:</p>
<pre class="classic">
?[<em>&lt;package_name&gt;</em>:][<em>&lt;resource_type&gt;</em>/]<em>&lt;resource_name&gt;</em>
</pre>
<p>For example, here's how you can reference an attribute to set the text color to match the
"primary" text color of the system theme:</p>
<pre>
&lt;EditText id=&quot;text&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:textColor=&quot;<strong>?android:textColorSecondary</strong>&quot;
android:text=&quot;&#64;string/hello_world&quot; /&gt;
</pre>
<p>Here, the {@code android:textColor} attribute specifies the name of a style attribute
in the current theme. Android now uses the value applied to the {@code android:textColorSecondary}
style attribute as the value for {@code android:textColor} in this widget. Because the system
resource tool knows that an attribute resource is expected in this context,
you do not need to explicitly state the type (which would be
<code>?android:attr/textColorSecondary</code>)&mdash;you can exclude the {@code attr} type.</p>
<h2 id="PlatformResources">Accessing Platform Resources</h2>
<p>Android contains a number of standard resources, such as styles, themes, and layouts. To
access these resource, qualify your resource reference with the
<code>android</code> package name. For example, Android provides a layout resource you can use for
list items in a {@link android.widget.ListAdapter}:</p>
<pre>
{@link android.app.ListActivity#setListAdapter(ListAdapter)
setListAdapter}(new {@link
android.widget.ArrayAdapter}&lt;String&gt;(this, <strong>android.R.layout.simple_list_item_1</strong>, myarray));
</pre>
<p>In this example, {@link android.R.layout#simple_list_item_1} is a layout resource defined by the
platform for items in a {@link android.widget.ListView}. You can use this instead of creating
your own layout for list items. (For more about using {@link android.widget.ListView}, see the
<a href="{@docRoot}resources/tutorials/views/hello-listview.html">List View Tutorial</a>.)</p>

View File

@ -0,0 +1,568 @@
page.title=Animation Resources
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D
Graphics</a></li>
</ol>
</div>
</div>
<p>An animation resource can define one of two types of animations:</p>
<dl>
<dt><a href="#Tween">Tween Animation</a></dt>
<dd>Creates an animation by performing a series of transformations on a single image.
An {@link android.view.animation.Animation}.</dd>
<dt><a href="#Frame">Frame Animation</a></dt>
<dd>Creates an animation by showing a sequence of images in order.
An {@link android.graphics.drawable.AnimationDrawable}.</dd>
</dl>
<h2 id="Tween">Tween Animation</h2>
<p>An animation defined in XML that performs transitions such as rotating,
fading, moving, and stretching on a graphic.
</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/anim/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to an {@link android.view.animation.Animation}.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.anim.<em>filename</em></code><br/>
In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#set-element">set</a> xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/<em>interpolator_resource</em>"
android:shareInterpolator=["true" | "false"] &gt;
&lt;<a href="#alpha-element">alpha</a>
android:fromAlpha="<em>float</em>"
android:toAlpha="<em>float</em>" /&gt;
&lt;<a href="#scale-element">scale</a>
android:fromXScale="<em>float</em>"
android:toXScale="<em>float</em>"
android:fromYScale="<em>float</em>"
android:toYScale="<em>float</em>"
android:pivotX="<em>float</em>"
android:pivotY="<em>float</em>" /&gt;
&lt;<a href="#translate-element">translate</a>
android:fromX="<em>float</em>"
android:toX="<em>float</em>"
android:fromY="<em>float</em>"
android:toY="<em>float</em>" /&gt;
&lt;<a href="#rotate-element">rotate</a>
android:fromDegrees="<em>float</em>"
android:toDegrees="<em>float</em>"
android:pivotX="<em>float</em>"
android:pivotY="<em>float</em>" /&gt;
&lt;<a href="#set-element">set</a>&gt;
...
&lt;/set&gt;
&lt;/set&gt;
</pre>
<p>The file must have a single root element: either an
<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
<code>&lt;rotate&gt;</code>, or <code>&lt;set&gt;</code> element that holds
a group (or groups) of other animation elements (even nested <code>&lt;set&gt;</code> elements).
</p>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="set-element"><code>&lt;set&gt;</code></dt>
<dd>A container that holds other animation elements
(<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
<code>&lt;rotate&gt;</code>) or other <code>&lt;set&gt;</code> elements. Represents an {@link
android.view.animation.AnimationSet}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:interpolator</code></dt>
<dd><em>Interpolator resource</em>.
An {@link android.view.animation.Interpolator} to apply on the animation.
The value must be a reference to a resource that specifies an interpolator
(not an interpolator class name). There are default interpolator
resources available from the platform or you can create your own interpolator resource.
See the discussion below for more about <a href="#Interpolators">Interpolators</a>.</dd>
<dt><code>android:shareInterpolator</code></dt>
<dd><em>Boolean</em>. "true" if you want to share the same interpolator among all child
elements.</dd>
</dl>
</dd>
<dt id="alpha-element"><code>&lt;alpha&gt;</code></dt>
<dd>A fade-in or fade-out animation. Represents an {@link
android.view.animation.AlphaAnimation}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:fromAlpha</code></dt>
<dd><em>Float</em>. Starting opacity offset, where 0.0 is transparent and 1.0
is opaque.</dd>
<dt><code>android:toAlpha</code></dt>
<dd><em>Float</em>. Ending opacity offset, where 0.0 is transparent and 1.0
is opaque.</dd>
</dl>
<p>For more attributes supported by <code>&lt;alpha&gt;</code>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
inherrited by this element).</p>
</dd>
<dt id="scale-element"><code>&lt;scale&gt;</code></dt>
<dd>A resizing animation. You can specify the center point of the image from which it grows
outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values
are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link
android.view.animation.ScaleAnimation}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:fromXScale</code></dt>
<dd><em>Float</em>. Starting X size offset, where 1.0 is no change.</dd>
<dt><code>android:toXScale</code></dt>
<dd><em>Float</em>. Ending X size offset, where 1.0 is no change.</dd>
<dt><code>android:fromYScale</code></dt>
<dd><em>Float</em>. Starting Y size offset, where 1.0 is no change.</dd>
<dt><code>android:toYScale</code></dt>
<dd><em>Float</em>. Ending Y size offset, where 1.0 is no change.</dd>
<dt><code>android:pivotX</code></dt>
<dd><em>Float</em>. The X coordinate to remain fixed when the object is scaled.</dd>
<dt><code>android:pivotY</code></dt>
<dd><em>Float</em>. The Y coordinate to remain fixed when the object is scaled.</dd>
</dl>
<p>For more attributes supported by <code>&lt;scale&gt;</code>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
inherrited by this element).</p>
</dd>
<dt id="translate-element"><code>&lt;translate&gt;</code></dt>
<dd>A vertical and/or horizontal motion. Supports the following attributes in any of
the following three formats: values from -100 to 100 ending with "%", indicating a percentage
relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its
parent; a float value with no suffix, indicating an absolute value. Represents a {@link
android.view.animation.TranslateAnimation}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:fromXDelta</code></dt>
<dd><em>Float or percentage</em>. Starting X offset. Expressed either: in pixels relative
to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
<dt><code>android:toXDelta</code></dt>
<dd><em>Float or percentage</em>. Ending X offset. Expressed either: in pixels relative
to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
<dt><code>android:fromYDelta</code></dt>
<dd><em>Float or percentage</em>. Starting Y offset. Expressed either: in pixels relative
to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
<dt><code>android:toYDelta</code></dt>
<dd><em>Float or percentage</em>. Ending Y offset. Expressed either: in pixels relative
to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
</dl>
<p>For more attributes supported by <code>&lt;translate&gt;</code>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
inherrited by this element).</p>
</dd>
<dt id="rotate-element"><code>&lt;rotate&gt;</code></dt>
<dd>A rotation animation. Represents a {@link android.view.animation.RotateAnimation}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:fromDegrees</code></dt>
<dd><em>Float</em>. Starting angular position, in degrees.</dd>
<dt><code>android:toDegrees</code></dt>
<dd><em>Float</em>. Ending angular position, in degrees.</dd>
<dt><code>android:pivotX</code></dt>
<dd><em>Float or percentage</em>. The X coordinate of the center of rotation. Expressed
either: in pixels relative to the object's left edge (such as {@code "5"}), in percentage relative
to the object's left edge (such as {@code "5%"}), or in percentage relative to the parent
container's left edge (such as {@code "5%p"}).</dd>
<dt><code>android:pivotY</code></dt>
<dd><em>Float or percentage</em>. The Y coordinate of the center of rotation. Expressed
either: in pixels relative to the object's top edge (such as {@code "5"}), in percentage relative
to the object's top edge (such as {@code "5%"}), or in percentage relative to the parent
container's top edge (such as {@code "5%p"}).</dd>
</dl>
<p>For more attributes supported by <code>&lt;rotate&gt;</code>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
inherrited by this element).</p>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>
<pp>XML file saved at <code>res/anim/hyperspace_jump.xml</code>:</p>
<pre>
&lt;set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
&lt;scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
&lt;set
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="700">
&lt;scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
&lt;rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
&lt;/set>
&lt;/set>
</pre>
<p>This application code will apply the animation to an {@link android.widget.ImageView} and
start the animation:</p>
<pre>
ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump);
image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump);
</pre>
</dd> <!-- end example -->
<dt>see also:</dt>
<dd>
<ul>
<li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D
Graphics: Tween Animation</a></li>
</ul>
</dd>
</dl>
<h3 id="Interpolators">Interpolators</h3>
<p>An interpolator is an animation modifier defined in XML that affects the rate of change in an
animation. This allows your existing animation effects to be accelerated, decelerated, repeated,
bounced, etc.</p>
<p>An interpolator is applied to an animation element with the {@code android:interpolator}
attribute, the value of which is a reference to an interpolator resource.</p>
<p>All interpolators available in Android are subclasses of the {@link
android.view.animation.Interpolator} class. For each interpolator class, Android
includes a public resource you can reference in order to apply the interpolator to an animation
using the the {@code android:interpolator} attribute.
The following table specifies the resource to use for each interpolator:</p>
<table>
<tr><th>Interpolator class</th><th>Resource ID</th></tr>
<tr>
<td>{@link android.view.animation.AccelerateDecelerateInterpolator}</td>
<td>{@code @android:anim/accelerate_decelerate_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.AccelerateInterpolator}</td>
<td>{@code @android:anim/accelerate_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.AnticipateInterpolator}</td>
<td>{@code @android:anim/anticipate_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.AnticipateOvershootInterpolator}</td>
<td>{@code @android:anim/anticipate_overshoot_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.BounceInterpolator}</td>
<td>{@code @android:anim/bounce_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.CycleInterpolator}</td>
<td>{@code @android:anim/cycle_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.DecelerateInterpolator}</td>
<td>{@code @android:anim/decelerate_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.LinearInterpolator}</td>
<td>{@code @android:anim/linear_interpolator}</td>
</tr>
<tr>
<td>{@link android.view.animation.OvershootInterpolator}</td>
<td>{@code @android:anim/overshoot_interpolator}</td>
</tr>
</table>
<p>Here's how you can apply one of these with the {@code android:interpolator} attribute:</p>
<pre>
&lt;set android:interpolator="@android:anim/accelerate_interpolator"&gt;
...
&lt;/set&gt;
</pre>
<h4>Custom interpolators</h4>
<p>If you're not satisfied with the interpolators provided by the platform (listed in the
table above), you can create a custom interpolator resource with modified attributes.
For example, you can adjust the rate of
acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of
cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to
create your own interpolator resource in an XML file.
</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/anim/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to the corresponding interpolator object.</dd>
<dt>resource reference:</dt>
<dd>
In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<em>InterpolatorName</em> xmlns:android="http://schemas.android.com/apk/res/android"
android:<em>attribute_name</em>="<em>value</em>"
/>
</pre>
<p>If you don't apply any attributes, then your interpolator will function exactly the same as
those provided by the platform (listed in the table above).</p>
</dd>
<dt>elements:</dt>
<dd>Notice that each {@link android.view.animation.Interpolator} implementation, when
defined in XML, begins its name in lowercase.</p>
<dl class="tag-list">
<dt><code>&lt;accelerateDecelerateInterpolator&gt;</code></dt>
<dd>The rate of change starts and ends slowly but accelerates through the
middle. <p>No attributes.</p></dd>
<dt><code>&lt;accelerateInterpolator&gt;</code></dt>
<dd>The rate of change starts out slowly, then accelerates.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:factor</code></dt>
<dd><em>Float</em>. The acceleration rate (default is 1).</dd>
</dl>
</dd>
<dt><code>&lt;anticipateInterpolator&gt;</code></dt>
<dd>The change starts backward then flings forward.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:tension</code></dt>
<dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
</dl>
</dd>
<dt><code>&lt;anticipateOvershootInterpolator&gt;</code></dt>
<dd>The change starts backward, flings forward and overshoots the target value, then
settles at the final value.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:tension</code></dt>
<dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
<dt><code>android:extraTension</code></dt>
<dd><em>Float</em>. The amount by which to multiply the tension (default is
1.5).</dd>
</dl>
</dd>
<dt><code>&lt;bounceInterpolator&gt;</code></dt>
<dd>The change bounces at the end. <p>No attributes</p></dd>
<dt><code>&lt;cycleInterpolator&gt;</code></dt>
<dd>Repeats the animation for a specified number of cycles. The rate of change follows a
sinusoidal pattern.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:cycles</code></dt>
<dd><em>Integer</em>. The number of cycles (default is 1).</dd>
</dl>
</dd>
<dt><code>&lt;decelerateInterpolator&gt;</code></dt>
<dd>The rate of change starts out quickly, then decelerates.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:factor</code></dt>
<dd><em>Float</em>. The deceleration rate (default is 1).</dd>
</dl>
</dd>
<dt><code>&lt;linearInterpolator&gt;</code></dt>
<dd>The rate of change is constant. <p>No attributes.</p></dd>
<dt><code>&lt;overshootInterpolator&gt;</code></dt>
<dd>The change flings forward and overshoots the last value, then comes back.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:tension</code></dt>
<dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
</dl>
</dd>
</dl>
<dt>example:</dt>
<dd>
<p>XML file saved at <code>res/anim/my_overshoot_interpolator.xml</code>:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:tension="7.0"
/>
</pre>
<p>This animation XML will apply the interpolator:</p>
<pre>
&lt;scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/my_overshoot_interpolator"
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700" />
</pre>
</dd>
</dl>
<h2 id="Frame">Frame Animation</h2>
<p>An animation defined in XML that shows a sequence of images in order (like a film).
</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/drawable/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to an {@link android.graphics.drawable.AnimationDrawable}.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.drawable.<em>filename</em></code><br/>
In XML: <code>@[<em>package</em>:]drawable.<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#animation-list-element">animation-list</a> xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
&lt;<a href="#item-element">item</a>
android:drawable="@[package:]drawable/<em>drawable_resource_name</em>"
android:duration="<em>integer</em>" />
&lt;/animation-list>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="animation-list-element"><code>&lt;animation-list&gt;</code></dt>
<dd><strong>Required</strong>. This must be the root element. Contains one or more
<code>&lt;item&gt;</code> elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:oneshot</code></dt>
<dd><em>Boolean</em>. "true" if you want to perform the animation once; "false" to loop the
animation.</dd>
</dl>
</dd>
<dt id="item-element"><code>&lt;item&gt;</code></dt>
<dd>A single frame of animation. Must be a child of a <code>&lt;animation-list&gt;</code> element.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:drawable</code></dt>
<dd><em>Drawable resource</em>. The drawable to use for this frame.</dd>
<dt><code>android:duration</code></dt>
<dd><em>Integer</em>. The duration to show this frame, in milliseconds.</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>
<dl>
<dt>XML file saved at <code>res/anim/rocket.xml</code>:</dt>
<dd>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
&lt;item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
&lt;item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
&lt;item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
&lt;/animation-list>
</pre>
</dd>
<dt>This application code will set the animation as the background for a View,
then play the animation:</dt>
<dd>
<pre>
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()};
rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()};
</pre>
</dd>
</dl>
</dd> <!-- end example -->
</dl>

View File

@ -0,0 +1,59 @@
page.title=Resource Types
parent.title=Application Resources
parent.link=index.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="providing-resources.html">Providing Resources</a></li>
<li><a href="accessing-resources.html">Accessing Resources</a></li>
</ol>
</div>
</div>
<p>Each of the documents in this section describe the usage, format and syntax for a certain type
of application resource that you can provide in your resources directory ({@code res/}).</p>
<p>Here's a brief summary of each resource type:</p>
<dl>
<dt><a href="{@docRoot}guide/topics/resources/animation-resource.html">Animation Resources</a></dt>
<dd>Define pre-determined animations.<br/>
Tween animations are saved in {@code res/anim/} and accessed from the {@code R.anim} class.<br/>
Frame animations are saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/color-list-resource.html">Color State List Resource</a></dt>
<dd>Define a color resources that changes based on the View state.<br/>
Saved in {@code res/color/} and accessed from the {@code R.color} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a></dt>
<dd>Define various graphics with bitmaps or XML.<br/>
Saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/layout-resource.html">Layout Resource</a></dt>
<dd>Define the layout for your application UI.<br/>
Saved in {@code res/layout/} and accessed from the {@code R.layout} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/menu-resource.html">Menu Resource</a></dt>
<dd>Define the contents of your application menus.<br/>
Saved in {@code res/menu/} and accessed from the {@code R.menu} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/string-resource.html">String Resources</a></dt>
<dd>Define strings, string arrays, and plurals (and include string formatting and styling).<br/>
Saved in {@code res/values/} and accessed from the {@code R.string}, {@code R.array},
and {@code R.plurals} classes.</dd>
<dt><a href="{@docRoot}guide/topics/resources/style-resource.html">Style Resource</a></dt>
<dd>Define the look and format for UI elements.<br/>
Saved in {@code res/values/} and accessed from the {@code R.style} class.</dd>
<dt><a href="{@docRoot}guide/topics/resources/more-resources.html">More Resource Types</a></dt>
<dd>Define values such as booleans, integers, dimensions, colors, and other arrays.<br/>
Saved in {@code res/values/} but each accessed from unique {@code R} sub-classes (such as {@code
R.bool}, {@code R.integer}, {@code R.dimen}, etc.).</dd>
</dl>

View File

@ -0,0 +1,164 @@
page.title=Color State List Resource
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="more-resources.html#Color">Color (simple value)</a></li>
</ol>
</div>
</div>
<p>A {@link android.content.res.ColorStateList} is an object you can define in XML
that you can apply as a color, but will actually change colors, depending on the state of
the {@link android.view.View} object to which it is applied. For example, a {@link
android.widget.Button} widget can exist in one of several different states (pressed, focused,
or niether) and, using a color state list, you can provide a different color during each state.</p>
<p>You can describe the state list in an XML file. Each color is defined in an {@code
&lt;item>} element inside a single {@code &lt;selector>} element. Each {@code &lt;item>}
uses various attributes to describe the state in which it should be used.</p>
<p>During each state change, the state list is traversed top to bottom and the first item that
matches the current state will be used&mdash;the selection is <em>not</em> based on the "best
match," but simply the first item that meets the minimum criteria of the state.</p>
<p class="note"><strong>Note:</strong> If you want to provide a static color resource, use a
simple <a href="more-resources.html#Color">Color</a> value.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/color/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link android.content.res.ColorStateList}.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.color.<em>filename</em></code><br/>
In XML: <code>@[<em>package</em>:]color/<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#selector-element">selector</a> xmlns:android="http://schemas.android.com/apk/res/android" >
&lt;<a href="#item-element">item</a>
android:color="<em>hex_color</em>"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
&lt;/selector>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="selector-element"><code>&lt;selector&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root element. Contains one or more {@code
&lt;item>} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>xmlns:android</code></dt>
<dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be
<code>"http://schemas.android.com/apk/res/android"</code>.
</dl>
</dd>
<dt id="item-element"><code>&lt;item&gt;</code></dt>
<dd>Defines a color to use during certain states, as described by its attributes. Must be a
child of a <code>&lt;selector&gt;</code> element.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:color</code></dt>
<dd><em>Hexadeximal color</em>. <strong>Required</strong>. The color is specified with an
RGB value and optional alpha channel.
<p>The value always begins with a pound (#) character and then followed by the
Alpha-Red-Green-Blue information in one of the following formats:</p>
<ul>
<li>#<em>RGB</em></li>
<li>#<em>ARGB</em></li>
<li>#<em>RRGGBB</em></li>
<li>#<em>AARRGGBB</em></li>
</ul></dd>
<dt><code>android:state_pressed</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is pressed (such as when a button
is touched/clicked); "false" if this item should be used in the default, non-pressed state.</dd>
<dt><code>android:state_focused</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is focused (such as when a button
is highlighted using the trackball/d-pad); "false" if this item should be used in the default,
non-focused state.</dd>
<dt><code>android:state_selected</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is selected (such as when a
tab is opened); "false" if this item should be used when the object is not selected.</dd>
<dt><code>android:state_checkable</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is checkable; "false" if this
item should be used when the object is not checkable. (Only useful if the object can
transition between a checkable and non-checkable widget.)</dd>
<dt><code>android:state_checked</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is checked; "false" if it
should be used when the object is un-checked.</dd>
<dt><code>android:state_enabled</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the object is enabled (capable of
receiving touch/click events); "false" if it should be used when the object is disabled.</dd>
<dt><code>android:state_window_focused</code></dt>
<dd><em>Boolean</em>. "true" if this item should be used when the application window has focus (the
application is in the foreground), "false" if this item should be used when the application
window does not have focus (for example, if the notification shade is pulled down or a dialog appears).</dd>
</dl>
<p class="note"><strong>Note:</strong> Remember that the first item in the state list that
matches the current state of the object will be applied. So if the first item in the list contains
none of the state attributes above, then it will be applied every time, which is why your
default value should always be last (as demonstrated in the following example).</p>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/color/button_text.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;selector xmlns:android="http://schemas.android.com/apk/res/android">
&lt;item android:state_pressed="true"
android:color="#ffff0000"/> &lt;!-- pressed --&gt;
&lt;item android:state_focused="true"
android:color="#ff0000ff"/> &lt;!-- focused --&gt;
&lt;item android:color="#ff000000"/> &lt;!-- default --&gt;
&lt;/selector>
</pre>
<p>This layout XML will apply the color list to a View:</p>
<pre>
&lt;Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
</pre>
</dd> <!-- end example -->
<dt>see also:</dt>
<dd>
<ul>
<li><a href="more-resources.html#Color">Color (simple value)</a></li>
<li>{@link android.content.res.ColorStateList}</li>
<li><a href="drawable-resource.html#StateList">State List Drawable</a></li>
</ul>
</dd>
</dl>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,105 @@
page.title=Application Resources
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>Topics</h2>
<ol>
<li><a href="providing-resources.html">Providing Resources</a></li>
<li><a href="accessing-resources.html">Accessing Resources</a></li>
<li><a href="runtime-changes.html">Handling Runtime Changes</a></li>
<li><a href="localization.html">Localization</a></li>
</ol>
<h2>Reference</h2>
<ol>
<li><a href="available-resources.html">Resource Types</a></li>
</ol>
</div>
</div>
<p>You should always externalize resources such as images and strings from your application
code, so that you can maintain them independently. Externalizing your
resources also allows you to provide alternative resources that support specific device
configurations such as different languages or screen sizes, which becomes increasingly
important as more Android-powered devices become available with different configurations. In order
to provide compatibility with different configurations, you must organize resources in your
project's {@code res/} directory, using various sub-directories that group resources by type and
configuration.</p>
<div class="figure" style="width:421px">
<img src="{@docRoot}images/resources/resource_devices_diagram1.png" height="137" alt="" />
<p class="img-caption">
<strong>Figure 1.</strong> Two different devices, both using default
resources.</p>
</div>
<div class="figure" style="width:421px">
<img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" />
<p class="img-caption">
<strong>Figure 2.</strong> Two different devices, one using alternative
resources.</p>
</div>
<p>For any type of resource, you can specify <em>default</em> and multiple
<em>alternative</em> resources for your application:</p>
<ul>
<li>Default resources are those that should be used regardless of
the device configuration or when there are no alternative resources that match the current
configuration.</li>
<li>Alternative resources are those that you've designed for use with a specific
configuration. To specify that a group of resources are for a specific configuration,
append an appropriate configuration qualifier to the directory name.</li>
</ul>
<p>For example, while your default UI
layout is saved in the {@code res/layout/} directory, you might specify a different UI layout to
be used when the screen is in landscape orientation, by saving it in the {@code res/layout-land/}
directory. Android automatically applies the appropriate resources by matching the
device's current configuration to your resource directory names.</p>
<p>Figure 1 demonstrates how a collection of default resources from an application are applied
to two different devices when there are no alternative resources available. Figure 2 shows
the same application with a set of alternative resources that qualify for one of the device
configurations, thus, the two devices uses different resources.</p>
<p>The information above is just an introduction to how application resources work on Android.
The following documents provide a complete guide to how you can organize your application resources,
specify alternative resources, access them in your application, and more:</p>
<dl>
<dt><strong><a href="providing-resources.html">Providing Resources</a></strong></dt>
<dd>What kinds of resources you can provide in your app, where to save them, and how to create
alternative resources for specific device configurations.</dd>
<dt><strong><a href="accessing-resources.html">Accessing Resources</a></strong></dt>
<dd>How to use the resources you've provided, either by referencing them from your application
code or from other XML resources.</dd>
<dt><strong><a href="runtime-changes.html">Handling Runtime Changes</a></strong></dt>
<dd>How to manage configuration changes that occur while your Activity is running.</dd>
<dt><strong><a href="localization.html">Localization</a></strong></dt>
<dd>A bottom-up guide to localizing your application using alternative resources. While this is
just one specific use of alternative resources, it is very important in order to reach more
users.</dd>
<dt><strong><a href="available-resources.html">Resource Types</a></strong></dt>
<dd>A reference of various resource types you can provide, describing their XML elements,
attributes, and syntax. For example, this reference shows you how to create a resource for
application menus, drawables, animations, and more.</dd>
</dl>
<!--
<h2>Raw Assets</h2>
<p>An alternative to saving files in {@code res/} is to save files in the {@code
assets/} directory. This should only be necessary if you need direct access to original files and
directories by name. Files saved in the {@code assets/} directory will not be given a resource
ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can
query data in the {@code assets/} directory like an ordinary file system, search through the
directory and
read raw data using {@link android.content.res.AssetManager}. For example, this can be more useful
when dealing with textures for a game. However, if you only need to read raw data from a file
(such as a video or audio file), then you should save files into the {@code res/raw/} directory and
then read a stream of bytes using {@link android.content.res.Resources#openRawResource(int)}. This
is uncommon, but if you need direct access to original files in {@code assets/}, refer to the {@link
android.content.res.AssetManager} documentation.</p>
-->

View File

@ -0,0 +1,282 @@
page.title=Layout Resource
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li>
</ol>
</div>
</div>
<p>A layout resource defines the architecture for the UI in an Activity or a component of a UI.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/layout/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link android.view.View} (or subclass) resource.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.layout.<em>filename</em></code><br/>
In XML: <code>@[<em>package</em>:]layout/<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#viewgroup-element"><em>ViewGroup</em></a> xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
android:layout_height=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
android:layout_width=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
[<em>ViewGroup-specific attributes</em>] &gt;
&lt;<a href="#view-element"><em>View</em></a>
android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
android:layout_height=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
android:layout_width=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
[<em>View-specific attributes</em>] &gt;
&lt;<a href="#requestfocus-element">requestFocus</a>/&gt;
&lt;/<em>View</em>&gt;
&lt;<a href="#viewgroup-element"><em>ViewGroup</em></a> &gt;
&lt;<a href="#view-element"><em>View</em></a> /&gt;
&lt;/<em>ViewGroup</em>&gt;
&lt;<a href="#include-element">include</a> layout="@layout/<i>layout_resource</i>"/&gt;
&lt;/<em>ViewGroup</em>&gt;
</pre>
<p class="note"><strong>Note:</strong> The root element can be either a
{@link android.view.ViewGroup}, a {@link android.view.View}, or a <a
href="#merge-element">{@code &lt;merge&gt;}</a> element, but there must be only
one root element and it must contain the {@code xmlns:android} attribute with the {@code android}
namespace as shown.</p>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="viewgroup-element"><code>&lt;ViewGroup&gt;</code></dt>
<dd>A container for other {@link android.view.View} elements. There are many
different kinds of {@link android.view.ViewGroup} objects and each one lets you
specify the layout of the child elements in different ways. Different kinds of
{@link android.view.ViewGroup} objects include {@link android.widget.LinearLayout},
{@link android.widget.RelativeLayout}, and {@link android.widget.FrameLayout}.
<p>You should not assume that any derivation of {@link android.view.ViewGroup}
will accept nested {@link android.view.View}s. Some {@link android.view.ViewGroup}s
are implementations of the {@link android.widget.AdapterView} class, which determines
its children only from an {@link android.widget.Adapter}.</p>
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:id</code></dt>
<dd><em>Resource ID</em>. A unique resource name for the element, which you can
use to obtain a reference to the {@link android.view.ViewGroup} from your application. See more
about the <a href="#idvalue">value for {@code android:id}</a> below.
</dd>
<dt><code>android:layout_height</code></dt>
<dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the group, as a
dimension value (or <a
href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
</dd>
<dt><code>android:layout_width</code></dt>
<dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the group, as a
dimension value (or <a
href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
</dd>
</dl>
<p>More attributes are supported by the {@link android.view.ViewGroup}
base class, and many more are supported by each implementation of
{@link android.view.ViewGroup}. For a reference of all available attributes,
see the corresponding reference documentation for the {@link android.view.ViewGroup} class
(for example, the <a
href="{@docRoot}reference/android/widget/LinearLayout.html#lattrs">LinearLayout XML
attributes</a>).</p>
</dd>
<dt id="view-element"><code>&lt;View&gt;</code></dt>
<dd>An individual UI component, generally referred to as a "widget". Different
kinds of {@link android.view.View} objects include {@link android.widget.TextView},
{@link android.widget.Button}, and {@link android.widget.CheckBox}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:id</code></dt>
<dd><em>Resource ID</em>. A unique resource name for the element, which you can use to
obtain a reference to the {@link android.view.View} from your application. See more about
the <a href="#idvalue">value for {@code android:id}</a> below.
</dd>
<dt><code>android:layout_height</code></dt>
<dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the element, as
a dimension value (or <a
href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
</dd>
<dt><code>android:layout_width</code></dt>
<dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the element, as
a dimension value (or <a
href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
</dd>
</dl>
<p>More attributes are supported by the {@link android.view.View}
base class, and many more are supported by each implementation of
{@link android.view.View}. Read <a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring
Layout</a> for more information. For a reference of all available attributes,
see the corresponding reference documentation (for example, the <a
href="{@docRoot}reference/android/widget/TextView.html#lattrs">TextView XML attributes</a>).</p>
</dd>
<dt id="requestfocus-element"><code>&lt;requestFocus&gt;</code></dt>
<dd>Any element representing a {@link android.view.View} object can include this empty element,
which gives it's parent initial focus on the screen. You can have only one of these
elements per file.</dd>
<dt id="include-element"><code>&lt;include&gt;</code></dt>
<dd>Includes a layout file into this layout.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>layout</code></dt>
<dd><em>Layout resource</em>. <strong>Required</strong>. Reference to a layout
resource.</dd>
<dt><code>android:id</code></dt>
<dd><em>Resource ID</em>. Overrides the ID given to the root view in the included layout.
</dd>
<dt><code>android:layout_height</code></dt>
<dd><em>Dimension or keyword</em>. Overrides the height given to the root view in the
included layout. Only effective if <code>android:layout_width</code> is also declared.
</dd>
<dt><code>android:layout_width</code></dt>
<dd><em>Dimension or keyword</em>. Overrides the width given to the root view in the
included layout. Only effective if <code>android:layout_height</code> is also declared.
</dd>
</dl>
<p>You can include any other layout attributes in the <code>&lt;include&gt;</code> that are
supported by the root element in the included layout and they will override those defined in the
root element.</p>
<p class="caution"><strong>Caution:</strong> If you want to override the layout dimensions,
you must override both <code>android:layout_height</code> and
<code>android:layout_width</code>&mdash;you cannot override only the height or only the width.
If you override only one, it will not take effect. (Other layout properties, such as weight,
are still inherited from the source layout.)</p>
<p>Another way to include a layout is to use {@link android.view.ViewStub}. It is a lightweight
View that consumes no layout space until you explicitly inflate it, at which point, it includes a
layout file defined by its {@code android:layout} attribute. For more information about using {@link
android.view.ViewStub}, read <a href="{@docRoot}resources/articles/layout-tricks-stubs.html">Layout
Tricks: ViewStubs</a>.</p>
</dd>
<dt id="merge-element"><code>&lt;merge&gt;</code></dt>
<dd>An alternative root element that is not drawn in the layout hierarchy. Using this as the
root element is useful when you know that this layout will be placed into a layout
that already contains the appropriate parent View to contain the children of the
<code>&lt;merge&gt;</code> element. This is particularly useful when you plan to include this layout
in another layout file using <a href="#include-element"><code>&lt;include&gt;</code></a> and
this layout doesn't require a different {@link android.view.ViewGroup} container. For more
information about merging layouts, read <a
href="{@docRoot}resources/articles/layout-tricks-merge.html">Layout
Tricks: Merging</a>.</dd>
</dl>
<h4 id="idvalue">Value for <code>android:id</code></h4>
<p>For the ID value, you should usually use this syntax form: <code>"@+id/<em>name</em>"</code>. The
plus symbol, {@code +}, indicates that this is a new resource ID and the <code>aapt</code> tool will
create a new resource integer in the {@code R.java} class, if it doesn't already exist. For
example:</p>
<pre>
&lt;TextView android:id="@+id/nameTextbox"/>
</pre>
<p>The <code>nameTextbox</code> name is now a resource ID attached to this element. You can then
refer to the {@link android.widget.TextView} to which the ID is associated in Java:</p>
<pre>
findViewById(R.id.nameTextbox);
</pre>
<p>This code returns the {@link android.widget.TextView} object.</p>
<p>However, if you have already defined an <a
href="{@docRoot}guide/topics/resources/drawable-resource.html#Id">ID resource</a> (and it is not
already used), then you can apply that ID to a {@link android.view.View} element by excluding the
plus symbol in the <code>android:id</code> value.</p>
<h4 id="layoutvalues">Value for <code>android:layout_height</code> and
<code>android:layout_width</code>:</h4>
<p>The height and width value can be expressed using any of the
<a href="more-resources.html#Dimension">dimension
units</a> supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:</p>
<table><tr><th>Value</th><th>Description</th></tr>
<tr>
<td><code>match_parent</code></td>
<td>Sets the dimension to match that of the parent element. Added in API Level 8 to
deprecate <code>fill_parent</code>.</td>
</tr>
<tr>
<td><code>fill_parent</code></td>
<td>Sets the dimension to match that of the parent element.</td>
</tr><tr>
<td><code>wrap_content</code></td>
<td>Sets the dimension only to the size required to fit the content of this element.</td>
</tr>
</table>
<h4>Custom View elements</h4>
<p>You can create your own custom {@link android.view.View} and {@link android.view.ViewGroup}
elements and apply them to your layout the same as a standard layout
element. You can also specify the attributes supported in the XML element. To learn more,
read <a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>.
</p>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/layout/main_activity.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
&lt;TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
&lt;Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
&lt;/LinearLayout>
</pre>
<p>This application code will load the layout for an {@link android.app.Activity}, in the
{@link android.app.Activity#onCreate(Bundle) onCreate()} method:</dt>
<dd>
<pre>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView.(R.layout.main_activity);
}
</pre>
</dd> <!-- end example -->
<dt>see also:</dt>
<dd>
<ul>
<li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li>
<li>{@link android.view.View}</li>
<li>{@link android.view.ViewGroup}</li>
</ul>
</dd>
</dl>

View File

@ -0,0 +1,647 @@
page.title=Localization
parent.title=Application Resources
parent.link=index.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>Localization quickview</h2>
<ul>
<li>Android lets you create different resource sets for different locales.</li>
<li>When your application runs, Android will load the resource set
that match the device's locale.</li>
<li>If locale-specific resources are not available, Android falls back to
defaults.</li>
<li>The emulator has features for testing localized apps. </li>
</ul>
<h2>In this document</h2>
<ol>
<li><a href="#resource-switching">Overview: Resource-Switching in Android</a>
</li>
<ol><li><a href="#defaults-r-important">Why Default Resources Are Important</a></li></ol>
<li><a href="#using-framework">Using Resources for Localization</a>
<ol>
<li><a href="#creating-defaults">How to Create Default Resources</a></li>
<li><a href="#creating-alternatives">How to Create Alternative Resources</a></li>
<li><a href="#resource-precedence">Which Resources Take Precedence?</a></li>
<li><a href="#referring-to-resources">Referring to Resources in Java</a></li>
</ol>
</li>
<li><a href="#strategies">Localization Strategies</a></li>
<li><a href="#testing">Testing Localized Applications</a></li>
<ol>
<li><a href="#device">Testing on a Device</a></li>
<li><a href="#emulator">Testing on an Emulator</a></li>
<li><a href="#test-for-default">Testing for Default Resources</a></li>
</ol>
<li><a href="#publishing">Publishing</a></li>
<li><a href="#checklist">Localization Checklists</a></li>
<ol>
<li><a href="#planning-checklist">Planning and Design Checklist</a></li>
<li><a href="#content-checklist">Content Checklist</a></li>
<li><a href="#testing-checklist">Testing and Publishing Checklist</a></li>
</ol>
</ol>
<h2>See also</h2>
<ol>
<li><a
href="{@docRoot}resources/tutorials/localization/index.html">Hello, L10N Tutorial</a></li>
<li><a href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resources</a></li>
<li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li>
<li><a href="{@docRoot}reference/android/app/Activity.html#ActivityLifecycle">Activity Lifecycle</a></li>
</ol>
</div>
</div>
<p>Android will run on many devices in many regions. To reach the most users,
your application should handle text, audio files, numbers, currency, and
graphics in ways appropriate to the locales where your application will be used.
</p>
<p>This document describes best practices for localizing Android
applications. The principles apply whether you are developing your application
using ADT with Eclipse, Ant-based tools, or any other IDE. </p>
<p>You should already have a working knowledge of Java and be familiar with
Android resource loading, the declaration of user interface elements in XML,
development considerations such as Activity lifecycle, and general principles of
internationalization and localization. </p>
<p>It is good practice to use the Android resource framework to separate the
localized aspects of your application as much as possible from the core Java
functionality:</p>
<ul>
<li>You can put most or all of the <em>contents</em> of your application's
user interface into resource files, as described in this document and in <a
href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resources</a>.</li>
<li>The <em>behavior</em> of the user interface, on the other hand, is driven
by your Java code.
For example, if users input data that needs to be formatted or sorted
differently depending on locale, then you would use Java to handle the data
programmatically. This document does not cover how to localize your Java code.
</li>
</ul>
<p>The <a
href="{@docRoot}resources/tutorials/localization/index.html">Hello, L10N
</a> tutorial takes you through the steps of creating a simple localized
application that uses locale-specific resources in the way described in this
document. </p>
<h2 id="resource-switching">Overview: Resource-Switching in Android</h2>
<p>Resources are text strings, layouts, sounds, graphics, and any other static
data that your Android application needs. An application can include multiple
sets of resources, each customized for a different device configuration. When a
user runs the application, Android automatically selects and loads the
resources that best match the device.</p>
<p>(This document focuses on localization and locale. For a complete description
of resource-switching and all the types of configurations that you can
specify &#8212; screen orientation, touchscreen type, and so on &#8212; see <a
href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing
Alternative Resources</a>.)</p>
<table border="0" cellspacing="0" cellpadding="0">
<tr border="0">
<td width="180" style="border: 0pt none ;"><p class="special-note">
<strong>When you write your application:</strong>
<br><br>
You create a set of default resources, plus alternatives to be used in
different locales.</p></td>
<td style="border: 0pt none; padding:0">
<p style="border:0; padding:0"><img src="../../../images/resources/right-arrow.png" alt="right-arrow"
width="51" height="17"></p></td>
<td width="180" style="border: 0pt none ;"><p class="special-note">
<strong>When a user runs your application:</strong>
<br><br>The Android system selects which resources to load, based on the
device's locale.</p></td>
</tr>
</table>
<p>When you write your application, you create default and alternative resources
for your application to use. To create resources, you place files within
specially named subdirectories of the project's <code>res/</code> directory.
</p>
<h3 id="defaults-r-important">Why Default Resources Are Important</h3>
<p>Whenever the application runs in a locale for which you have not provided
locale-specific text, Android will load the default strings from
<code>res/values/strings.xml</code>. If this default file is absent, or if it
is missing a string that your application needs, then your application will not run
and will show an error.
The example below illustrates what can happen when the default text file is incomplete. </p>
<p><em>Example:</em>
<p>An application's Java code refers to just two strings, <code>text_a</code> and
<code>text_b</code>. This application includes a localized resource file
(<code>res/values-en/strings.xml</code>) that defines <code>text_a</code> and
<code>text_b</code> in English. This application also includes a default
resource file (<code>res/values/strings.xml</code>) that includes a
definition for <code>text_a</code>, but not for <code>text_b</code>:
<ul>
<li>This application might compile without a problem. An IDE such as Eclipse
will not highlight any errors if a resource is missing.</li>
<li>When this application is launched on a device with locale set to English,
the application might run without a problem, because
<code>res/values-en/strings.xml</code> contains both of the needed text
strings.</li>
<li>However, <strong>the user will see an error message and a Force Close
button</strong> when this application is launched on a device set to a
language other than English. The application will not load.</li>
</ul>
<p>To prevent this situation, make sure that a <code>res/values/strings.xml</code>
file exists and that it defines every needed string. The situation applies to
all types of resources, not just strings: You
need to create a set of default resource files containing all
the resources that your application calls upon &#8212; layouts, drawables,
animations, etc. For information about testing, see <a href="#test-for-default">
Testing for Default Resources</a>.</p>
<h2 id="using-framework">Using Resources for Localization</h2>
<h3 id="creating-defaults">How to Create Default Resources</h3>
<p>Put the application's default text in
a file with the following location and name:</p>
<p><code>&nbsp;&nbsp;&nbsp;&nbsp;res/values/strings.xml</code> (required directory)</p>
<p>The text strings in <code>res/values/strings.xml</code> should use the
default language, which is the language that you expect most of your application's users to
speak. </p>
<p>The default resource set must also include any default drawables and layouts,
and can include other types of resources such as animations.
<br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;res/drawable/</code>(required directory holding at least
one graphic file, for the application's icon in the Market)<br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;res/layout/</code> (required directory holding an XML
file that defines the default layout)<br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;res/anim/</code> (required if you have any
<code>res/anim-<em>&lt;qualifiers&gt;</em></code> folders)<br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;res/xml/</code> (required if you have any
<code>res/xml-<em>&lt;qualifiers&gt;</em></code> folders)<br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;res/raw/</code> (required if you have any
<code>res/raw-<em>&lt;qualifiers&gt;</em></code> folders)
</p>
<p class="note"><strong>Tip:</strong> In your code, examine each reference to
an Android resource. Make sure that a default resource is defined for each
one. Also make sure that the default string file is complete: A <em>
localized</em> string file can contain a subset of the strings, but the
<em>default</em> string file must contain them all.
</p>
<h3 id="creating-alternatives">How to Create Alternative Resources</h3>
<p>A large part of localizing an application is providing alternative text for
different languages. In some cases you will also provide alternative graphics,
sounds, layouts, and other locale-specific resources. </p>
<p>An application can specify many <code>res/<em>&lt;qualifiers&gt;</em>/</code>
directories, each with different qualifiers. To create an alternative resource for
a different locale, you use a qualifier that specifies a language or a
language-region combination. (The name of a resource directory must conform
to the naming scheme described in
<a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing
Alternative Resources</a>,
or else it will not compile.)</p>
<p><em>Example:</em></p>
<p>Suppose that your application's default language is English. Suppose also
that you want to localize all the text in your application to French, and most
of the text in your application (everything except the application's title) to
Japanese. In this case, you could create three alternative <code>strings.xml</code>
files, each stored in a locale-specific resource directory:</p>
<ol>
<li><code>res/values/strings.xml</code><br>
Contains English text for all the strings that the application uses,
including text for a string named <code>title</code>.</li>
<li><code>res/values-fr/strings.xml</code><br>
Contain French text for all the strings, including <code>title</code>.</li>
<li><code>res/values-ja/strings.xml</code><br>
Contain Japanese text for all the strings <em>except</em>
<code>title</code>.<br>
<code></code></li>
</ol>
<p>If your Java code refers to <code>R.string.title</code>, here is what will
happen at runtime:</p>
<ul>
<li>If the device is set to any language other than French, Android will load
<code>title</code> from the <code>res/values/strings.xml</code> file.</li>
<li>If the device is set to French, Android will load <code>title</code> from
the <code>res/values-fr/strings.xml</code> file.</li>
</ul>
<p>Notice that if the device is set to Japanese, Android will look for
<code>title</code> in the <code>res/values-ja/strings.xml</code> file. But
because no such string is included in that file, Android will fall back to the
default, and will load <code>title</code> in English from the
<code>res/values/strings.xml</code> file. </p>
<h3 id="resource-precedence">Which Resources Take Precedence?</h3>
<p> If multiple resource files match a device's configuration, Android follows a
set of rules in deciding which file to use. Among the qualifiers that can be
specified in a resource directory name, <strong>locale almost always takes
precedence</strong>. </p>
<p><em>Example:</em></p>
<p>Assume that an application includes a default set of graphics and two other
sets of graphics, each optimized for a different device setup:</p>
<ul>
<li><code>res/drawable/</code><br>
Contains
default graphics.</li>
<li><code>res/drawable-small-land-stylus/</code><br>
Contains graphics optimized for use with a device that expects input from a
stylus and has a QVGA low-density screen in landscape orientation.</li>
<li><code>res/drawable-ja/</code> <br>
Contains graphics optimized for use with Japanese.</li>
</ul>
<p>If the application runs on a device that is configured to use Japanese,
Android will load graphics from <code>res/drawable-ja/</code>, even if the
device happens to be one that expects input from a stylus and has a QVGA
low-density screen in landscape orientation.</p>
<p class="note"><strong>Exception:</strong> The only qualifiers that take
precedence over locale in the selection process are MCC and MNC (mobile country
code and mobile network code). </p>
<p><em>Example:</em></p>
<p>Assume that you have the following situation:</p>
<ul>
<li>The application code calls for <code>R.string.text_a</code></li>
<li>Two relevant resource files are available:
<ul>
<li><code>res/values-mcc404/strings.xml</code>, which includes
<code>text_a</code> in the application's default language, in this case
English.</li>
<li><code>res/values-hi/strings.xml</code>, which includes
<code>text_a</code> in Hindi.</li>
</ul>
</li>
<li>The application is running on a device that has the following
configuration:
<ul>
<li>The SIM card is connected to a mobile network in India (MCC 404).</li>
<li>The language is set to Hindi (<code>hi</code>).</li>
</ul>
</li>
</ul>
<p>Android will load <code>text_a</code> from
<code>res/values-mcc404/strings.xml</code> (in English), even if the device is
configured for Hindi. That is because in the resource-selection process, Android
will prefer an MCC match over a language match. </p>
<p>The selection process is not always as straightforward as these examples
suggest. Please read <a
href="{@docRoot}guide/topics/resources/providing-resources.html#BestMatch">How Android Finds
the Best-matching Resource</a> for a more nuanced description of the
process. All the qualifiers are described and listed in order of
precedence in <a
href="{@docRoot}guide/topics/resources/providing-resources.html#table2">Table 2 of Providing
Alternative Resources</a>.</p>
<h3 id="referring-to-resources">Referring to Resources in Java</h3>
<p>In your application's Java code, you refer to resources using the syntax
<code>R.<em>resource_type</em>.<em>resource_name</em></code> or
<code>android.R.<em>resource_type</em>.<em>resource_name</em></code><em>.</em>
For more about this, see <a
href="{@docRoot}guide/topics/resources/accessing-resources.html">Accessing Resources</a>.</p>
<h2 id="strategies">Localization Strategies</h2>
<h4 id="failing2">Design your application to work in any locale</h4>
<p>You cannot assume anything about the device on which a user will
run your application. The device might have hardware that you were not
anticipating, or it might be set to a locale that you did not plan for or that
you cannot test. Design your application so that it will function normally or fail gracefully no
matter what device it runs on.</p>
<p class="note"><strong>Important:</strong> Make sure that your application
includes a full set of default resources.</p> <p>Make sure to include
<code>res/drawable/</code> and a <code>res/values/</code> folders (without any
additional modifiers in the folder names) that contain all the images and text
that your application will need. </p>
<p>If an application is missing even one default resource, it will not run on a
device that is set to an unsupported locale. For example, the
<code>res/values/strings.xml</code> default file might lack one string that
the application needs: When the application runs in an unsupported locale and
attempts to load <code>res/values/strings.xml</code>, the user will see an
error message and a Force Close button. An IDE such as Eclipse will not
highlight this kind of error, and you will not see the problem when you
test the application on a device or emulator that is set to a supported locale.</p>
<p>For more information, see <a href="#test-for-default">Testing for Default Resources</a>.</p>
<h4>Design a flexible layout</h4>
<p> If you need to rearrange your layout to fit a certain language (for example
German with its long words), you can create an alternative layout for that
language (for example <code>res/layout-de/main.xml</code>). However, doing this
can make your application harder to maintain. It is better to create a single
layout that is more flexible.</p>
<p>Another typical situation is a language that requires something different in
its layout. For example, you might have a contact form that should include two
name fields when the application runs in Japanese, but three name fields when
the application runs in some other language. You could handle this in either of
two ways:</p>
<ul>
<li>Create one layout with a field that you can programmatically enable or
disable, based on the language, or</li>
<li>Have the main layout include another layout that includes the changeable
field. The second layout can have different configurations for different
languages.</li>
</ul>
<h4>Avoid creating more resource files and text strings than you need</h4>
<p>You probably do not need to create a locale-specific
alternative for every resource in your application. For example, the layout
defined in the <code>res/layout/main.xml</code> file might work in any locale,
in which case there would be no need to create any alternative layout files.
</p>
<p>Also, you might not need to create alternative text for every
string. For example, assume the following:</p>
<ul>
<li>Your application's default language is American
English. Every string that the application uses is defined, using American
English spellings, in <code>res/values/strings.xml</code>. </li>
<li>For a few important phrases, you want to provide
British English spelling. You want these alternative strings to be used when your
application runs on a device in the United Kingdom. </li>
</ul>
<p>To do this, you could create a small file called
<code>res/values-en-rGB/strings.xml</code> that includes only the strings that
should be different when the application runs in the U.K. For all the rest of
the strings, the application will fall back to the defaults and use what is
defined in <code>res/values/strings.xml</code>.</p>
<h4>Use the Android Context object for manual locale lookup</h4>
<p>You can look up the locale using the {@link android.content.Context} object
that Android makes available:</p>
<pre>String locale = context.getResources().getConfiguration().locale.getDisplayName();</pre>
<h2 id="testing">Testing Localized Applications</h2>
<h3 id="device">Testing on a Device</h3>
<p>Keep in mind that the device you are testing may be significantly different from
the devices available to consumers in other geographies. The locales available
on your device may differ from those available on other devices. Also, the
resolution and density of the device screen may differ, which could affect
the display of strings and drawables in your UI.</p>
<p>To change the locale on a device, use the Settings application (Home &gt;
Menu &gt; Settings &gt; Locale &amp; text &gt; Select locale). </p>
<h3 id="emulator">Testing on an Emulator</h3>
<p>For details about using the emulator, see See <a
href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a>.</p>
<h4>Creating and using a custom locale</h4>
<p>A &quot;custom&quot; locale is a language/region combination that the Android
system image does not explicitly support. (For a list of supported locales in
Android platforms see the Version Notes in the <a
href="{@docRoot}sdk/index.html">SDK</a> tab). You can test
how your application will run in a custom locale by creating a custom locale in
the emulator. There are two ways to do this:</p>
<ul>
<li>Use the Custom Locale application, which is accessible from the
Application tab. (After you create a custom locale, switch to it by
pressing and holding the locale name.)</li>
<li>Change to a custom locale from the adb shell, as described below.</li>
</ul>
<p>When you set the emulator to a locale that is not available in the Android
system image, the system itself will display in its default language. Your
application, however, should localize properly.</p>
<h4>Changing the emulator locale from the adb shell</h4>
<p>To change the locale in the emulator by using the adb shell. </p>
<ol>
<li>Pick the locale you want to test and determine its language and region codes, for
example <code>fr</code> for French and <code>CA</code> for Canada.<br>
</li>
<li>Launch an emulator.</li>
<li>From a command-line shell on the host computer, run the following
command:<br>
<code>adb shell</code><br>
or if you have a device attached, specify that you want the emulator by adding
the <code>-e</code> option:<br>
<code>adb -e shell</code></li>
<li>At the adb shell prompt (<code>#</code>), run this command: <br>
<code>setprop persist.sys.language [<em>language code</em>];setprop
persist.sys.country [<em>country code</em>];stop;sleep 5;start <br>
</code>Replace bracketed sections with the appropriate codes from Step
1.</li>
</ol>
<p>For instance, to test in Canadian French:</p>
<p><code>setprop persist.sys.language fr;setprop persist.sys.country
CA;stop;sleep 5;start </code></p>
<p>This will cause the emulator to restart. (It will look like a full reboot,
but it is not.) Once the Home screen appears again, re-launch your application (for
example, click the Run icon in Eclipse), and the application will launch with
the new locale. </p>
<h3 id="test-for-default">Testing for Default Resources</h3>
<p>Here's how to test whether an application includes every string resource that it needs: </p>
<ol><li>Set the emulator or device to a language that your application does not
support. For example, if the application has French strings in
<code>res/values-fr/</code> but does not have any Spanish strings in
<code>res/values-es/</code>, then set the emulator's locale to Spanish.
(You can use the Custom Locale application to set the emulator to an
unsupported locale.)</li>
<li>Run the application.</li>
<li>If the application shows an error message and a Force Close button, it might
be looking for a string that is not available. Make sure that your
<code>res/values/strings.xml</code> file includes a definition for
every string that the application uses.</li>
</ol>
</p>
<p>If the test is successful, repeat it for other types of
configurations. For example, if the application has a layout file called
<code>res/layout-land/main.xml</code> but does not contain a file called
<code>res/layout-port/main.xml</code>, then set the emulator or device to
portrait orientation and see if the application will run.
<h2 id="publishing">Publishing Localized Applications</h2>
<p>The Android Market is
the main application distribution system for Android devices. To publish a
localized application, you need to sign your application, version it, and go
through all the other steps described in <a
href="{@docRoot}guide/publishing/preparing.html">Preparing to Publish</a>. </p>
<p>If you split your application in several .apk files, each targeted to a
different locale, follow these guidelines:</p>
<ul>
<li>Sign each .apk file with the same certificate. For more about this, see <a
href="{@docRoot}guide/publishing/app-signing.html#strategies">Signing
Strategies</a>. </li>
<li>Give each .apk file a different application name. Currently it is
impossible to put two applications into the Android Market that have exactly the
same name.</li>
<li>Include a complete set of default resources in each .apk file.</li>
</ul>
<h2 id="checklist">Localization Checklists</h2>
<p>These checklists summarize the process of localizing an Android application.
Not everything on these lists will apply to every application.</p>
<h3 id="planning-checklist">Planning and Design Checklist</h3>
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Choose a localization strategy. Which countries and which languages will
your application support? What is your application's default country and
language? How will your application behave when it does not have specific
resources available for a given locale?</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td><p>Identify everything in your application that will need to be
localized: </p>
<ul>
<li>Consider specific details of your application &#8212; text, images,
sounds, music, numbers, money, dates and times. You might not need to localize
everything. For example, you don't need to localize text that the user never
sees, or images that are culturally neutral, or icons that convey the same
meaning in every locale. </li>
<li>Consider broad themes. For example, if you hope to sell your
application in two very culturally different markets, you might want to design
your UI and present your application in an entirely different way for each
locale.</li>
</ul></td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td><p>Design your Java code to externalize resources wherever possible:</p>
<ul>
<li>Use <code>R.string</code> and <code>strings.xml</code> files instead
of hard-coded strings or string constants. </li>
<li>Use <code>R.drawable</code> and <code>R.layout</code> instead of
hard-coded drawables or layouts. </li>
</ul></td>
</tr>
</table>
<h3 id="content-checklist">Content Checklist</h3>
<table border="0" cellspacing="0" cellpadding="5" width="100%">
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Create a full set of default resources in <code>res/values/</code> and
other <code>res/</code> folders, as described in <a
href="#creating-defaults">Creating Default Resources</a>.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Obtain reliable translations of the static text, including menu text,
button names, error messages, and help text. Place the translated strings in
<code>res/values-<em>&lt;qualifiers&gt;</em>/strings.xml</code> files. </td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Make sure that your application correctly formats dynamic text (for
example numbers and dates) for each supported locale. Make sure that your
application handles word breaks, punctuation, and alphabetical sorting correctly
for each supported language.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>If necessary, create locale-specific versions of your graphics and
layout, and place them in <code>res/drawable-<em>&lt;qualifiers&gt;</em>/</code> and
<code>res/layout-<em>&lt;qualifiers&gt;</em>/</code> folders.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Create any other localized content that your application requires; for
example, create recordings of sound files for each language, as needed.</td>
</tr>
</table>
<h3 id="testing-checklist">Testing and Publishing Checklist</h3>
<table border="0" cellspacing="0" cellpadding="5" width="100%">
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Test your application for each supported locale. If possible, have a
person who is native to each locale test your application and give you
feedback.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Test the default resources by loading a locale that is not available on
the device or emulator. For instructions, see <a href="#test-for-default">
Testing for Default Resources</a>. </td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Test the localized strings in both landscape and portrait display modes.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Sign your application and create your final build or builds.</td>
</tr>
<tr>
<td valign="top" align="center"><img src="../../../images/resources/arrow.png" alt="arrow" width="26"
border="0"></td>
<td>Upload your .apk file or files to Market, selecting the appropriate
languages as
you upload. (For more details, see <a
href="{@docRoot}guide/publishing/publishing.html">Publishing Your
Applications</a>.)</td>
</tr>
</table>

View File

@ -0,0 +1,207 @@
page.title=Menu Resource
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a></li>
</ol>
</div>
</div>
<p>A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that
can be inflated with {@link android.view.MenuInflater}.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/menu/<em>filename</em>.xml</code><br/>
The filename will be used as the resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.menu.<em>filename</em></code><br/>
In XML: <code>@[<em>package</em>:]menu.<em>filename</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#menu-element">menu</a> xmlns:android="http://schemas.android.com/apk/res/android">
&lt;<a href="#item-element">item</a> android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
android:orderInCategory="<em>integer</em>"
android:title="<em>string</em>"
android:titleCondensed="<em>string</em>"
android:icon="@[package:]drawable/<em>drawable_resource_name</em>"
android:alphabeticShortcut="<em>string</em>"
android:numericShortcut="<em>string</em>"
android:checkable=["true" | "false"]
android:visible=["visible" | "invisible" | "gone"]
android:enabled=["enabled" | "disabled"] /&gt;
&lt;<a href="#group-element">group</a> android:id="@[+][<em>package</em>:]id/<em>resource name</em>"
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
android:orderInCategory="<em>integer</em>"
android:checkableBehavior=["none" | "all" | "single"]
android:visible=["visible" | "invisible" | "gone"]
android:enabled=["enabled" | "disabled"] &gt;
&lt;<a href="#item-element">item</a> /&gt;
&lt;/group&gt;
&lt;<a href="#item-element">item</a> &gt;
&lt;<a href="#menu-element">menu</a>&gt;
&lt;<a href="#item-element">item</a> /&gt;
&lt;/menu&gt;
&lt;/item&gt;
&lt;/menu&gt;
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="menu-element"><code>&lt;menu&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node. Contains <code>&lt;item></code> and/or
<code>&lt;group></code> elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>xmlns:android</code></dt>
<dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be
<code>"http://schemas.android.com/apk/res/android"</code>.
</dl>
</dd>
<dt id="group-element"><code>&lt;group&gt;</code></dt>
<dd>A menu group (to create a collection of items that share traits, such as whether they are
visible, enabled, or checkable). Contains one or more <code>&lt;item&gt;</code> elements. Must be a
child of a <code>&lt;menu&gt;</code> element.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:id</code></dt>
<dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item, use the form:
<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new ID.</dd>
<dt><code>android:menuCategory</code></dt>
<dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
constants, which define the group's priority. Valid values:
<table>
<tr><th>Value</th><th>Description</th></tr>
<tr><td><code>container</code></td><td>For groups that are part of a
container.</td></tr>
<tr><td><code>system</code></td><td>For groups that are provided by the
system.</td></tr>
<tr><td><code>secondary</code></td><td>For groups that are user-supplied secondary
(infrequently used) options.</td></tr>
<tr><td><code>alternative</code></td><td>For groups that are alternative actions
on the data that is currently displayed.</td></tr>
</table>
</dd>
<dt><code>android:orderInCategory</code></dt>
<dd><em>Integer</em>. The default order of the items within the category.</dd>
<dt><code>android:checkableBehavior</code></dt>
<dd><em>Keyword</em>. The type of checkable behavior for the group. Valid values:
<table>
<tr><th>Value</th><th>Description</th></tr>
<tr><td><code>none</code></td><td>Not checkable</td></tr>
<tr><td><code>all</code></td><td>All items can be checked (use checkboxes)</td></tr>
<tr><td><code>single</code></td><td>Only one item can be checked (use radio buttons)</td></tr>
</table>
</dd>
<dt><code>android:visible</code></dt>
<dd><em>Boolean</em>. "true" if the group is visible.</dd>
<dt><code>android:enabled</code></dt>
<dd><em>Boolean</em>. "true" if the group is enabled.</dd>
</dl>
</dd>
<dt id="item-element"><code>&lt;item&gt;</code></dt>
<dd>A menu item. May contain a <code>&lt;menu&gt;</code> element (for a Sub
Menu). Must be a child of a <code>&lt;menu&gt;</code> or <code>&lt;group&gt;</code> element.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:id</code></dt>
<dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item, use the form:
<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new ID.</dd>
<dt><code>android:menuCategory</code></dt>
<dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
constants, which define the item's priority. Valid values:
<table>
<tr><th>Value</th><th>Description</th></tr>
<tr><td><code>container</code></td><td>For items that are part of a
container.</td></tr>
<tr><td><code>system</code></td><td>For items that are provided by the
system.</td></tr>
<tr><td><code>secondary</code></td><td>For items that are user-supplied secondary
(infrequently used) options.</td></tr>
<tr><td><code>alternative</code></td><td>For items that are alternative actions
on the data that is currently displayed.</td></tr>
</table>
</dd>
<dt><code>android:orderInCategory</code></dt>
<dd><em>Integer</em>. The order of "importance" of the item, within a group.</dd>
<dt><code>android:title</code></dt>
<dd><em>String</em>. The menu title.</dd>
<dt><code>android:titleCondensed</code></dt>
<dd><em>String</em>. A condensed title, for situations in which the normal title is
too long.</dd>
<dt><code>android:icon</code></dt>
<dd><em>Drawable resource</em>. An image to be used as the menu item icon.</dd>
<dt><code>android:alphabeticShortcut</code></dt>
<dd><em>Char</em>. A character for the alphabetic shortcut key.</dd>
<dt><code>android:numericShortcut</code></dt>
<dd><em>Integer</em>. A number for the numeric shortcut key.</dd>
<dt><code>android:checkable</code></dt>
<dd><em>Boolean</em>. "true" if the item is checkable.</dd>
<dt><code>android:checked</code></dt>
<dd><em>Boolean</em>. "true" if the item is checked by default.</dd>
<dt><code>android:visible</code></dt>
<dd><em>Boolean</em>. "true" if the item is visible by default.</dd>
<dt><code>android:enabled</code></dt>
<dd><em>Boolean</em>. "true" if the item is enabled by default.</dd>
</dl>
</dd>
</dl>
</dd>
<dt>example:</dt>
<dd>XML file saved at <code>res/menu/example_menu.xml</code>:
<pre>
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android">
&lt;item android:id="@+id/item1"
android:title="@string/item1"
android:icon="@drawable/group_item1_icon" />
&lt;group android:id="@+id/group">
&lt;item android:id="@+id/group_item1"
android:title="@string/group_item1"
android:icon="@drawable/group_item1_icon" />
&lt;item android:id="@+id/group_item2"
android:title="G@string/group_item2"
android:icon="@drawable/group_item2_icon" />
&lt;/group>
&lt;item android:id="@+id/submenu"
android:title="@string/submenu_title" >
&lt;menu>
&lt;item android:id="@+id/submenu_item1"
android:title="@string/submenu_item1" />
&lt;/menu>
&lt;/item>
&lt;/menu>
</pre>
<p>This application code will inflate the menu from the {@link
android.app.Activity#onCreateOptionsMenu(Menu)} callback:</p>
<pre>
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.example_menu, menu);
return true;
}
</pre>
</dd> <!-- end example -->
</dl>

View File

@ -0,0 +1,803 @@
page.title=More Resource Types
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<p>This page defines more types of resources you can externalize, including:</p>
<dl>
<dt><a href="#Bool">Bool</a></dt>
<dd>XML resource that carries a boolean value.</dd>
<dt><a href="#Color">Color</a></dt>
<dd>XML resource that carries a color value (a hexadecimal color).</dd>
<dt><a href="#Dimension">Dimension</a></dt>
<dd>XML resource that carries a dimension value (with a unit of measure).</dd>
<dt><a href="#Id">ID</a></dt>
<dd>XML resource that provides a unique identifier for application resources and
components.</dd>
<dt><a href="#Integer">Integer</a></dt>
<dd>XML resource that carries an integer value.</dd>
<dt><a href="#IntegerArray">Integer Array</a></dt>
<dd>XML resource that provides an array of integers.</dd>
<dt><a href="#TypedArray">Typed Array</a></dt>
<dd>XML resource that provides a {@link android.content.res.TypedArray} (which you can use
for an array of drawables).</dd>
</dl>
<h2 id="Bool">Bool</h2>
<p>A boolean value defined in XML.</p>
<p class="note"><strong>Note:</strong> 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 &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;bool>} element's {@code name} will be used as the resource
ID.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.bool.<em>bool_name</em></code><br/>
In XML: <code>@[<em>package</em>:]bool/<em>bool_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#bool-resources-element">resources</a>&gt;
&lt;<a href="#bool-element">bool</a>
name="<em>bool_name</em>"
&gt;[true | false]&lt;/bool>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="bool-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="bool-element"><code>&lt;bool&gt;</code></dt>
<dd>A boolean value: {@code true} or {@code false}.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the bool value. This will be used as the resource ID.</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values-small/bools.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources&gt;
&lt;bool name="screen_small">true&lt;/bool>
&lt;bool name="adjust_view_bounds">true&lt;/bool>
&lt;/resources>
</pre>
<p>This application code retrieves the boolean:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
boolean screenIsSmall = res.{@link android.content.res.Resources#getBoolean(int) getBoolean}(R.bool.screen_small);
</pre>
<p>This layout XML uses the boolean for an attribute:</p>
<pre>
&lt;ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/logo"
android:adjustViewBounds="@bool/adjust_view_bounds" />
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="Color">Color</h2>
<p>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"}).</p>
<p>The value always begins with a pound (#) character and then followed by the
Alpha-Red-Green-Blue information in one of the following formats:</p>
<ul>
<li>#<em>RGB</em></li>
<li>#<em>ARGB</em></li>
<li>#<em>RRGGBB</em></li>
<li>#<em>AARRGGBB</em></li>
</ul>
<p class="note"><strong>Note:</strong> 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 &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/colors.xml</code><br/>
The filename is arbitrary. The {@code &lt;color>} element's {@code name} will be used as the
resource ID.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.color.<em>color_name</em></code><br/>
In XML: <code>@[<em>package</em>:]color/<em>color_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#color-resources-element">resources</a>>
&lt;<a href="#color-element">color</a>
name="<em>color_name</em>"
&gt;<em>hex_color</em>&lt;/color>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="color-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="color-element"><code>&lt;color&gt;</code></dt>
<dd>A color expressed in hexadecimal, as described above.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the color. This will be used as the resource ID.
</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/colors.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;color name="opaque_red">#f00&lt;/color>
&lt;color name="translucent_red">#80ff0000&lt;/color>
&lt;/resources>
</pre>
<p>This application code retrieves the color resource:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
int color = res.{@link android.content.res.Resources#getColor(int) getColor}(R.color.opaque_red);
</pre>
<p>This layout XML applies the color to an attribute:</p>
<pre>
&lt;TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/translucent_red"
android:text="Hello"/>
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="Dimension">Dimension</h2>
<p>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:</p>
<dl>
<dt>{@code dp}</dt>
<dd>Density-independent Pixels - an abstract unit that is based on the physical density of the
screen. These units are relative to a 160 dpi (dots per inch) screen, so <em>{@code 160dp} is
always one inch</em> regardless of the screen density. The ratio of dp-to-pixel will change with the
screen density, but not necessarily in direct proportion. You should use these units when specifying
view dimensions in your layout, so the UI properly scales to render at the same actual size on
different screens. (The compiler accepts both "dip" and "dp", though "dp" is more consistent with
"sp".)</dd>
<dt>{@code sp}</dt>
<dd>Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font
size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference.</dd>
<dt>{@code pt}</dt>
<dd>Points - 1/72 of an inch based on the physical size of the screen.</dd>
<dt>{@code px}</dt>
<dd>Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because
the actual representation can vary across devices; each devices may have a different number of pixels
per inch and may have more or fewer total pixels available on the screen.</dd>
<dt>{@code mm}</dt>
<dd>Millimeters - based on the physical size of the screen.</dd>
<dt>{@code in}</dt>
<dd>Inches - based on the physical size of the screen.</dd>
</dl>
<p class="note"><strong>Note:</strong> 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 &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;dimen>} element's {@code name} will be used as the
resource ID.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.dimen.<em>dimension_name</em></code><br/>
In XML: <code>@[<em>package</em>:]dimen/<em>dimension_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#dimen-resources-element">resources</a>>
&lt;<a href="#dimen-element">dimen</a>
name="<em>dimension_name</em>"
&gt;<em>dimension</em>&lt;/dimen&gt;
&lt;/resources&gt;
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="dimen-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="dimen-element"><code>&lt;dimen&gt;</code></dt>
<dd>A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in),
as described above.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the dimension. This will be used as the resource ID.
</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/dimens.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;dimen name="textview_height">25dp&lt;/dimen>
&lt;dimen name="textview_width">150dp&lt;/dimen>
&lt;dimen name="ball_radius">30dp&lt;/dimen>
&lt;dimen name="font_size">16sp&lt;/dimen>
&lt;/resources>
</pre>
<p>This application code retrieves a dimension:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
float fontSize = res.{@link android.content.res.Resources#getDimension(int) getDimension}(R.dimen.font_size);
</pre>
<p>This layout XML applies dimensions to attributes:</p>
<pre>
&lt;TextView
android:layout_height="@dimen/textview_height"
android:layout_width="@dimen/textview_width"
android:textSize="@dimen/font_size"/>
</pre>
</dl>
</dd> <!-- end example -->
</dl>
<h2 id="Id">ID</h2>
<p>A unique resource ID defined in XML. Using the name you provide in the {@code &lt;item&gt;}
element, the Android developer tools create a unique integer in your project's {@code
R.java} class, which you can use as an
identifier for an application resources (for example, a {@link android.view.View} in your UI layout)
or a unique integer for use in your application code (for example, as an ID for a dialog or a
result code).</p>
<p class="note"><strong>Note:</strong> 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 &lt;resources&gt;} element. Also, remember that an ID resources does not reference
an actual resource item; it is simply a unique ID that you can attach to other resources or use
as a unique integer in your application.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename.xml</em></code><br/>
The filename is arbitrary.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.id.<em>name</em></code><br/>
In XML: <code>@[<em>package</em>:]id/<em>name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#id-resources-element">resources</a>&gt;
&lt;<a href="#id-item-element">item</a>
type="id"
name="<em>id_name</em>" /&gt;
&lt;/resources&gt;
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="id-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="id-item-element"><code>&lt;item&gt;</code></dt>
<dd>Defines a unique ID. Takes no value, only attributes.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>type</code></dt>
<dd>Must be "id".</dd>
<dt><code>name</code></dt>
<dd><em>String</em>. A unique name for the ID.</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>
<p>XML file saved at <code>res/values/ids.xml</code>:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;item type="id" name="button_ok" /&gt;
&lt;item type="id" name="dialog_exit" /&gt;
&lt;/resources>
</pre>
<p>Then, this layout snippet uses the "button_ok" ID for a Button widget:</p>
<pre>
&lt;Button android:id="<b>@id/button_ok</b>"
style="@style/button_style" /&gt;
</pre>
<p>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&mdash;in the format {@code
android:id="@+id/name"}&mdash;it means that the "name" ID does not exist and should be created.)</p>
<p>As another example, the following code snippet uses the "dialog_exit" ID as a unique identifier
for a dialog:</p>
<pre>
{@link android.app.Activity#showDialog(int) showDialog}(<b>R.id.dialog_exit</b>);
</pre>
<p>In the same application, the "dialog_exit" ID is compared when creating a dialog:</p>
<pre>
protected Dialog {@link android.app.Activity#onCreateDialog(int)}(int id) {
Dialog dialog;
switch(id) {
case <b>R.id.dialog_exit</b>:
...
break;
default:
dialog = null;
}
return dialog;
}
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="Integer">Integer</h2>
<p>An integer defined in XML.</p>
<p class="note"><strong>Note:</strong> 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 &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename.xml</em></code><br/>
The filename is arbitrary. The {@code &lt;integer>} element's {@code name} will be used as the
resource ID.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.integer.<em>integer_name</em></code><br/>
In XML: <code>@[<em>package</em>:]integer/<em>integer_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#integer-resources-element">resources</a>>
&lt;<a href="#integer-element">integer</a>
name="<em>integer_name</em>"
&gt;<em>integer</em>&lt;/integer&gt;
&lt;/resources&gt;
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="integer-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="integer-element"><code>&lt;integer&gt;</code></dt>
<dd>An integer.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the integer. This will be used as the resource ID.
</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>
<p>XML file saved at <code>res/values/integers.xml</code>:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;integer name="max_speed">75&lt;/integer>
&lt;integer name="min_speed">5&lt;/integer>
&lt;/resources>
</pre>
<p>This application code retrieves an integer:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
int maxSpeed = res.{@link android.content.res.Resources#getInteger(int) getInteger}(R.integer.max_speed);
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="IntegerArray">Integer Array</h2>
<p>An array of integers defined in XML.</p>
<p class="note"><strong>Note:</strong> 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 &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;integer-array>} element's {@code name} will be used as the
resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to an array of integers.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.array.<em>string_array_name</em></code><br/>
In XML: <code>@[<em>package</em>:]array.<em>integer_array_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#integer-array-resources-element">resources</a>>
&lt;<a href="#integer-array-element">integer-array</a>
name="<em>integer_array_name</em>">
&lt;<a href="#integer-array-item-element">item</a>
&gt;<em>integer</em>&lt;/item&gt;
&lt;/integer-array>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="integer-array-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="integer-array-element"><code>&lt;string-array&gt;</code></dt>
<dd>Defines an array of integers. Contains one or more child {@code &lt;item>} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:name</code></dt>
<dd><em>String</em>. A name for the array. This name will be used as the resource
ID to reference the array.</dd>
</dl>
</dd>
<dt id="integer-array-item-element"><code>&lt;item&gt;</code></dt>
<dd>An integer. The value can be a referenced to another
integer resource. Must be a child of a {@code &lt;integer-array&gt;} element.
<p>No attributes.</p>
</dd>
</dl>
</dd> <!-- end elements -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/integers.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;integer-array name="bits">
&lt;item>4&lt;/item>
&lt;item>8&lt;/item>
&lt;item>16&lt;/item>
&lt;item>32&lt;/item>
&lt;/integer-array>
&lt;/resources>
</pre>
<p>This application code retrieves the integer array:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
int[] bits = res.{@link android.content.res.Resources#getIntArray(int) getIntArray}(R.array.bits);
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="TypedArray">Typed Array</h2>
<p>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.</p>
<p class="note"><strong>Note:</strong> 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 &lt;resources&gt;} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;array>} element's {@code name} will be used as the
resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link android.content.res.TypedArray}.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.array.<em>array_name</em></code><br/>
In XML: <code>@[<em>package</em>:]array.<em>array_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#array-resources-element">resources</a>>
&lt;<a href="#array-element">array</a>
name="<em>integer_array_name</em>">
&lt;<a href="#array-item-element">item</a>&gt;<em>resource</em>&lt;/item&gt;
&lt;/array>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="array-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="array-element"><code>&lt;array&gt;</code></dt>
<dd>Defines an array. Contains one or more child {@code &lt;item>} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>android:name</code></dt>
<dd><em>String</em>. A name for the array. This name will be used as the resource
ID to reference the array.</dd>
</dl>
</dd>
<dt id="array-item-element"><code>&lt;item&gt;</code></dt>
<dd>A generic resource. The value can be a reference to a resource or a simple data type.
Must be a child of an {@code &lt;array&gt;} element.
<p>No attributes.</p>
</dd>
</dl>
</dd> <!-- end elements -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/arrays.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;array name="icons">
&lt;item>@drawable/home&lt;/item>
&lt;item>@drawable/settings&lt;/item>
&lt;item>@drawable/logout&lt;/item>
&lt;/array>
&lt;array name="colors">
&lt;item>#FFFF0000&lt;/item>
&lt;item>#FF00FF00&lt;/item>
&lt;item>#FF0000FF&lt;/item>
&lt;/array>
&lt;/resources>
</pre>
<p>This application code retrieves each array and then obtains the first entry in each array:</p>
<pre>
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.icons);
int color = colors.{@link android.content.res.TypedArray#getColor(int,int) getColor}(0,0);
</pre>
</dd> <!-- end example -->
</dl>
<!-- TODO
<h2>Styleable Attribute</h2>
<dl class="xml">
<dt>syntax:</dt>
<dd>
<pre class="stx">
</pre>
</dd>
<dt>file location:</dt>
<dd><code>res/</code></dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd>
<dt>resource reference:</dt>
<dd>Java: <code>R.</code><br/>
XML:
</dd>
<dt>elements and attributes:</dt>
<dd>
<dl class="attr">
<dt><code></code></dt>
<dd></dd>
<dt><code></code></dt>
<dd>Valid attributes:
<dl>
<dt><code></code></dt>
<dd>
</dd>
<dt><code></code></dt>
<dd>
</dd>
</dl>
</dd>
</dl>
</dd>
<dt>example:</dt>
<dd>
<dl>
<dt>XML file saved at <code>res/</code>:</dt>
<dd>
<pre>
</pre>
</dd>
<dt>Java code :</dt>
<dd>
<pre>
</pre>
</dd>
</dl>
</dd>
<dt>see also:</dt>
<dd>
<ul>
<li></li>
</ul>
</dd>
</dl>
-->

View File

@ -0,0 +1,992 @@
page.title=Providing Resources
parent.title=Application Resources
parent.link=index.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>Quickview</h2>
<ul>
<li>Different types of resources belong in different subdirectories of {@code res/}</li>
<li>Alternative resources provide configuration-specific resource files</li>
<li>Always include default resources so your app does not depend on specific
device configurations</li>
</ul>
<h2>In this document</h2>
<ol>
<li><a href="#ResourceTypes">Grouping Resource Types</a></li>
<li><a href="#AlternativeResources">Providing Alternative Resources</a>
<ol>
<li><a href="#QualifierRules">Qualifier name rules</a></li>
<li><a href="#AliasResources">Creating alias resources</a></li>
</ol>
</li>
<li><a href="#Compatibility">Providing the Best Device Compatibility with Resources</a>
<ol>
<li><a href="#ScreenCompatibility">Providing screen resource compatibility for Android
1.5</a></li>
</ol>
</li>
<li><a href="#BestMatch">How Android Finds the Best-matching Resource</a></li>
<li><a href="#KnownIssues">Known Issues</a></li>
</ol>
<h2>See also</h2>
<ol>
<li><a href="accessing-resources.html">Accessing Resources</a></li>
<li><a href="available-resources.html">Resource Types</a></li>
<li><a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
Screens</a></li>
</ol>
</div>
</div>
<p>You should always externalize application resources such as images and strings from your
code, so that you can maintain them independently. You should also provide alternative resources for
specific device configurations, by grouping them in specially-named resource directories. At
runtime, Android uses uses the appropriate resource based on the current configuration. For
example, you might want to provide a different UI layout depending on the screen size or different
strings depending on the language setting.</p>
<p>Once you externalize your application resources, you can access them
using resource IDs that are generated in your project's {@code R} class. How to use
resources in your application is discussed in <a href="accessing-resources.html">Accessing
Resources</a>. This document shows you how to group your resources in your Android project and
provide alternative resources for specific device configurations.</p>
<h2 id="ResourceTypes">Grouping Resource Types</h2>
<p>You should place each type of resource in a specific subdirectory of your project's
{@code res/} directory. For example, here's the file hierarchy for a simple project:</p>
<pre class="classic no-pretty-print">
MyProject/
src/ <span style="color:black">
MyActivity.java </span>
res/
drawable/ <span style="color:black">
icon.png </span>
layout/ <span style="color:black">
main.xml
info.xml</span>
values/ <span style="color:black">
strings.xml </span>
</pre>
<p>As you can see in this example, the {@code res/} directory contains all the resources (in
subdirectories): an image resource, two layout resources, and a string resource file. The resource
directory names are important and are described in table 1.</p>
<p class="table-caption" id="table1"><strong>Table 1.</strong> Resource directories
supported inside project {@code res/} directory.</p>
<table>
<tr>
<th scope="col">Directory</th>
<th scope="col">Resource Type</th>
</tr>
<tr>
<td><code>anim/</code></td>
<td>XML files that define tween animations. See <a
href="animation-resource.html">Animation Resources</a>.</td>
</tr>
<tr>
<td><code>color/</code></td>
<td>XML files that define a state list of colors. See <a href="color-list-resource.html">Color
State List Resource</a></td>
</tr>
<tr>
<td><code>drawable/</code></td>
<td><p>Bitmap files ({@code .png}, {@code .9.png}, {@code .jpg}, {@code .gif}) or XML files that
are compiled into the following drawable resource subtypes:</p>
<ul>
<li>Bitmap files</li>
<li>Nine-Patches (re-sizable bitmaps)</li>
<li>State lists</li>
<li>Shapes</li>
<li>Animation drawables</li>
<li>Other drawables</li>
</ul>
<p>See <a href="drawable-resource.html">Drawable Resources</a>.</p>
</td>
</tr>
<tr>
<td><code>layout/</code></td>
<td>XML files that define a user interface layout.
See <a href="layout-resource.html">Layout Resource</a>.</td>
</tr>
<tr>
<td><code>menu/</code></td>
<td>XML files that define application menus, such as an Options Menu, Context Menu, or Sub
Menu. See <a href="menu-resource.html">Menu Resource</a>.</td>
</tr>
<tr>
<td><code>raw/</code></td>
<td><p>Arbitrary files to save in their raw form. To open these resources with a raw
{@link java.io.InputStream}, call {@link android.content.res.Resources#openRawResource(int)
Resources.openRawResource()} with the resource ID, which is {@code R.raw.<em>filename</em>}.</p>
<p>However, if you need access to original file names and file hierarchy, you might consider
saving some resources in the {@code
assets/} directory (instead of {@code res/raw/}). Files in {@code assets/} are not given a
resource ID, so you can read them only using {@link android.content.res.AssetManager}.</p></td>
</tr>
<tr>
<td><code>values/</code></td>
<td><p>XML files that contain simple values, such as strings, integers, and colors.</p>
<p>Whereas XML resource files in other {@code res/} subdirectories define a single resource
based on the XML filename, files in the {@code values/} directory describe multiple resources.
For a file in this directory, each child of the {@code &lt;resources&gt;} element defines a single
resource. For example, a {@code &lt;string&gt;} element creates an
{@code R.string} resource and a {@code &lt;color&gt;} element creates an {@code R.color}
resource.</p>
<p>Because each resource is defined with its own XML element, you can name the file
whatever you want and place different resource types in one file. However, for clarity, you might
want to place unique resource types in different files. For example, here are some filename
conventions for resources you can create in this directory:</p>
<ul>
<li>arrays.xml for resource arrays (<a
href="more-resources.html#TypedArray">typed arrays</a>).</li>
<li>colors.xml for <a
href="more-resources.html#Color">color values</a></li>
<li>dimens.xml for <a
href="more-resources.html#Dimension">dimension values</a>.</li>
<li>strings.xml for <a href="string-resource.html">string
values</a>.</li>
<li>styles.xml for <a href="style-resource.html">styles</a>.</li>
</ul>
<p>See <a href="string-resource.html">String Resources</a>,
<a href="style-resource.html">Style Resource</a>, and
<a href="more-resources.html">More Resource Types</a>.</p>
</td>
</tr>
<tr>
<td><code>xml/</code></td>
<td>Arbitrary XML files that can be read at runtime by calling {@link
android.content.res.Resources#getXml(int) Resources.getXML()}. Various XML configuration files
must be saved here, such as a <a
href="{@docRoot}guide/topics/search/searchable-config.html">searchable configuration</a>.
<!-- or preferences configuration. --></td>
</tr>
</table>
<p class="caution"><strong>Caution:</strong> Never save resource files directly inside the
{@code res/} directory&mdash;it will cause a compiler error.</p>
<p>For more information about certain types of resources, see the <a
href="available-resources.html">Resource Types</a> documentation.</p>
<p>The resources that you save in the subdirectories defined in table 1 are your "default"
resources. That is, these resources define the default design and content for your application.
However, different types of Android-powered devices might call for different types of resources.
For example, if a device has a larger than normal screen, then you should provide
different layout resources that take advantage of the extra screen space. Or, if a device has a
different language setting, then you should provide different string resources that translate the
text in your user interface. To provide these different resources for different device
configurations, you need to provide alternative resources, in addition to your default
resources.</p>
<h2 id="AlternativeResources">Providing Alternative Resources</h2>
<div class="figure" style="width:421px">
<img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" />
<p class="img-caption">
<strong>Figure 1.</strong> Two different devices, one using alternative resources.</p>
</div>
<p>Almost every application should provide alternative resources to support specific device
configurations. For instance, you should include alternative drawable resources for different
screen densities and alternative string resources for different languages. At runtime, Android
detects the current device configuration and loads the appropriate
resources for your application.</p>
<p>To specify configuration-specific alternatives for a set of resources:</p>
<ol>
<li>Create a new directory in {@code res/} named in the form {@code
<em>&lt;resources_name&gt;</em>-<em>&lt;config_qualifier&gt;</em>}.
<ul>
<li><em>{@code &lt;resources_name&gt;}</em> is the directory name of the corresponding default
resources (defined in table 1).</li>
<li><em>{@code &lt;qualifier&gt;}</em> is a name that specifies an individual configuration
for which these resources are to be used (defined in table 2).</li>
</ul>
<p>You can append more than one <em>{@code &lt;qualifier&gt;}</em>. Separate each
one with a dash.</p>
</li>
<li>Save the respective alternative resources in this new directory. The resource files must be
named exactly the same as the default resource files.</li>
</ol>
<p>For example, here are some default and alternative resources:</p>
<pre class="classic no-pretty-print">
res/
drawable/ <span style="color:black">
icon.png
background.png </span>
drawable-hdpi/ <span style="color:black">
icon.png
background.png </span>
</pre>
<p>The {@code hdpi} qualifier indicates that the resources in that directory are for devices with a
high-density screen. The images in each of these drawable directories are sized for a specific
screen density, but the filenames are exactly
the same. This way, the resource ID that you use to reference the {@code icon.png} or {@code
background.png} image is always the same, but Android selects the
version of each resource that best matches the current device, by comparing the device
configuration information with the qualifiers in the alternative resource directory name.</p>
<p>Android supports several configuration qualifiers and you can
add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2
lists the valid configuration qualifiers, in order of precedence&mdash;if you use multiple
qualifiers for one resource directory, they must be added to the directory name in the order they
are listed in the table.</p>
<p class="note"><strong>Note:</strong> Some configuration qualifiers were added after Android 1.0,
so not
all versions of Android support all the qualifiers listed in table 2. New qualifiers
indicate the version in which they were added. To avoid any issues, always include a set of default
resources for resources that your application uses. For more information, see the section about <a
href="#Compatibility">Providing the Best Device Compatibility with Resources</a>.</p>
<p class="table-caption" id="table2"><strong>Table 2.</strong> Configuration qualifier
names.</p>
<table>
<tr>
<th>Qualifier</th>
<th>Values</th>
<th>Description</th>
</tr>
<tr id="MccQualifier">
<td>MCC and MNC</td>
<td>Examples:<br/>
<code>mcc310</code><br/>
<code><nobr>mcc310-mnc004</nobr></code><br/>
<code>mcc208-mnc00</code><br/>
etc.
</td>
<td>
<p>The mobile country code (MCC), optionally followed by mobile network code (MNC)
from the SIM card in the device. For example, <code>mcc310</code> is U.S. on any carrier,
<code>mcc310-mnc004</code> is U.S. on Verizon, and <code>mcc208-mnc00</code> is France on
Orange.</p>
<p>If the device uses a radio connection (GSM phone), the MCC comes
from the SIM, and the MNC comes from the network to which the
device is connected.</p>
<p>You can also use the MCC alone (for example, to include country-specific legal
resources in your application). If you need to specify based on the language only, then use the
<em>language and region</em> qualifier instead (discussed next). If you decide to use the MCC and
MNC qualifier, you should do so with care and test that it works as expected.</p>
<p>Also see the configuration fields {@link
android.content.res.Configuration#mcc}, and {@link
android.content.res.Configuration#mnc}, which indicate the current mobile country code
and mobile network code, respectively.</p>
</td>
</tr>
<tr id="LocaleQualifier">
<td>Language and region</td>
<td>Examples:<br/>
<code>en</code><br/>
<code>fr</code><br/>
<code>en-rUS</code><br/>
<code>fr-rFR</code><br/>
<code>fr-rCA</code><br/>
etc.
</td>
<td><p>The language is defined by a two-letter <a
href="http://www.loc.gov/standards/iso639-2/php/code_list.php">ISO
639-1</a> language code, optionally followed by a two letter
<a
href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO
3166-1-alpha-2</a> region code (preceded by lowercase &quot;{@code r}&quot;).
</p><p>
The codes are <em>not</em> case-sensitive; the {@code r} prefix is used to
distinguish the region portion.
You cannot specify a region alone.</p>
<p>This can change during the life
of your application if the user changes his or her language in the system settings. See <a
href="runtime-changes.html">Handling Runtime Changes</a> for information about
how this can affect your application during runtime.</p>
<p>See <a href="localization.html">Localization</a> for a complete guide to localizing
your application for other languages.</p>
<p>Also see the {@link android.content.res.Configuration#locale} configuration field, which
indicates the current locale.</p>
</td>
</tr>
<tr id="ScreenSizeQualifier">
<td>Screen size</td>
<td>
<code>small</code><br/>
<code>normal</code><br/>
<code>large</code><br/>
<code>xlarge</code>
</td>
<td>
<ul class="nolist">
<li>{@code small}: Screens based on the space available on a
low-density QVGA screen. Considering a portrait HVGA display, this has
the same available width but less height&mdash;it is 3:4 vs. HVGA's
2:3 aspect ratio. Examples are QVGA low density and VGA high
density.</li>
<li>{@code normal}: Screens based on the traditional
medium-density HVGA screen. A screen is considered to be normal if it is
at least this size (independent of density) and not larger. Examples
of such screens a WQVGA low density, HVGA medium density, WVGA
high density.</li>
<li>{@code large}: Screens based on the space available on a
medium-density VGA screen. Such a screen has significantly more
available space in both width and height than an HVGA display.
Examples are VGA and WVGA medium density screens.</li>
<li>{@code xlarge}: Screens that are considerably larger than the traditional
medium-density HVGA screen. In most cases, devices with extra large screens would be too
large to carry in a pocket and would most likely be tablet-style devices. <em>Added in API Level
9.</em></li>
</ul>
<p><em>Added in API Level 4.</em></p>
<p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
Screens</a> for more information.</p>
<p>Also see the {@link android.content.res.Configuration#screenLayout} configuration field,
which indicates whether the screen is small, normal,
or large.</p>
</td>
</tr>
<tr id="ScreenAspectQualifier">
<td>Screen aspect</td>
<td>
<code>long</code><br/>
<code>notlong</code>
</td>
<td>
<ul class="nolist">
<li>{@code long}: Long screens, such as WQVGA, WVGA, FWVGA</li>
<li>{@code notlong}: Not long screens, such as QVGA, HVGA, and VGA</li>
</ul>
<p><em>Added in API Level 4.</em></p>
<p>This is based purely on the aspect ratio of the screen (a "long" screen is wider). This
is not related to the screen orientation.</p>
<p>Also see the {@link android.content.res.Configuration#screenLayout} configuration field,
which indicates whether the screen is long.</p>
</td>
</tr>
<tr id="OrientationQualifier">
<td>Screen orientation</td>
<td>
<code>port</code><br/>
<code>land</code> <!-- <br/>
<code>square</code> -->
</td>
<td>
<ul class="nolist">
<li>{@code port}: Device is in portrait orientation (vertical)</li>
<li>{@code land}: Device is in landscape orientation (horizontal)</li>
<!-- Square mode is currently not used. -->
</ul>
<p>This can change during the life of your application if the user rotates the
screen. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about
how this affects your application during runtime.</p>
<p>Also see the {@link android.content.res.Configuration#orientation} configuration field,
which indicates the current device orientation.</p>
</td>
</tr>
<tr id="DockQualifier">
<td>Dock mode</td>
<td>
<code>car</code><br/>
<code>desk</code>
</td>
<td>
<ul class="nolist">
<li>{@code car}: Device is in a car dock</li>
<li>{@code desk}: Device is in a desk dock</li>
</ul>
<p><em>Added in API Level 8.</em></p>
<p>This can change during the life of your application if the user places the device in a
dock. You can enable or disable this mode using {@link
android.app.UiModeManager}. See <a href="runtime-changes.html">Handling Runtime Changes</a> for
information about how this affects your application during runtime.</p>
</td>
</tr>
<tr id="NightQualifier">
<td>Night mode</td>
<td>
<code>night</code><br/>
<code>notnight</code>
</td>
<td>
<ul class="nolist">
<li>{@code night}: Night time</li>
<li>{@code notnight}: Day time</li>
</ul>
<p><em>Added in API Level 8.</em></p>
<p>This can change during the life of your application if night mode is left in
auto mode (default), in which case the mode changes based on the time of day. You can enable
or disable this mode using {@link android.app.UiModeManager}. See <a
href="runtime-changes.html">Handling Runtime Changes</a> for information about how this affects your
application during runtime.</p>
</td>
</tr>
<tr id="DensityQualifier">
<td>Screen pixel density (dpi)</td>
<td>
<code>ldpi</code><br/>
<code>mdpi</code><br/>
<code>hdpi</code><br/>
<code>xhdpi</code><br/>
<code>nodpi</code>
</td>
<td>
<ul class="nolist">
<li>{@code ldpi}: Low-density screens; approximately 120dpi.</li>
<li>{@code mdpi}: Medium-density (on traditional HVGA) screens; approximately
160dpi.</li>
<li>{@code hdpi}: High-density screens; approximately 240dpi.</li>
<li>{@code xhdpi}: Extra high-density screens; approximately 320dpi. <em>Added in API
Level 8</em></li>
<li>{@code nodpi}: This can be used for bitmap resources that you do not want to be scaled
to match the device density.</li>
</ul>
<p><em>Added in API Level 4.</em></p>
<p>There is thus a 3:4:6 scaling ratio between the three densities, so a 9x9 bitmap
in ldpi is 12x12 in mdpi and 18x18 in hdpi.</p>
<p>When Android selects which resource files to use,
it handles screen density differently than the other qualifiers.
In step 1 of <a href="#BestMatch">How Android finds the best
matching directory</a> (below), screen density is always considered to
be a match. In step 4, if the qualifier being considered is screen
density, Android selects the best final match at that point,
without any need to move on to step 5.
</p>
<p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
Screens</a> for more information about how to handle screen sizes and how Android might scale
your bitmaps.</p>
</td>
</tr>
<tr id="TouchscreenQualifier">
<td>Touchscreen type</td>
<td>
<code>notouch</code><br/>
<code>stylus</code><br/>
<code>finger</code>
</td>
<td>
<ul class="nolist">
<li>{@code notouch}: Device does not have a touchscreen.</li>
<li>{@code stylus}: Device has a resistive touchscreen that's suited for use with a
stylus.</li>
<li>{@code finger}: Device has a touchscreen.</li>
</ul>
<p>Also see the {@link android.content.res.Configuration#touchscreen} configuration field,
which indicates the type of touchscreen on the device.</p>
</td>
</tr>
<tr id="KeyboardAvailQualifier">
<td>Keyboard availability</td>
<td>
<code>keysexposed</code><br/>
<code>keyssoft</code>
</td>
<td>
<ul class="nolist">
<li>{@code keysexposed}: Device has a keyboard available. If the device has a
software keyboard enabled (which is likely), this may be used even when the hardware keyboard is
<em>not</em> exposed to the user, even if the device has no hardware keyboard. If no software
keyboard is provided or it's disabled, then this is only used when a hardware keyboard is
exposed.</li>
<li>{@code keyshidden}: Device has a hardware keyboard available but it is
hidden <em>and</em> the device does <em>not</em> have a software keyboard enabled.</li>
<li>{@code keyssoft}: Device has a software keyboard enabled, whether it's
visible or not.</li>
</ul>
<p>If you provide <code>keysexposed</code> resources, but not <code>keyssoft</code>
resources, the system uses the <code>keysexposed</code> resources regardless of whether a
keyboard is visible, as long as the system has a software keyboard enabled.</p>
<p>This can change during the life of your application if the user opens a hardware
keyboard. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about how
this affects your application during runtime.</p>
<p>Also see the configuration fields {@link
android.content.res.Configuration#hardKeyboardHidden} and {@link
android.content.res.Configuration#keyboardHidden}, which indicate the visibility of a hardware
keyboard and and the visibility of any kind of keyboard (including software), respectively.</p>
</td>
</tr>
<tr id="ImeQualifier">
<td>Primary text input method</td>
<td>
<code>nokeys</code><br/>
<code>qwerty</code><br/>
<code>12key</code>
</td>
<td>
<ul class="nolist">
<li>{@code nokeys}: Device has no hardware keys for text input.</li>
<li>{@code qwerty}: Device has a hardware qwerty keyboard, whether it's visible to the
user
or not.</li>
<li>{@code 12key}: Device has a hardware 12-key keyboard, whether it's visible to the user
or not.</li>
</ul>
<p>Also see the {@link android.content.res.Configuration#keyboard} configuration field,
which indicates the primary text input method available.</p>
</td>
</tr>
<tr id="NavAvailQualifier">
<td>Navigation key availability</td>
<td>
<code>navexposed</code><br/>
<code>navhidden</code>
</td>
<td>
<ul class="nolist">
<li>{@code navexposed}: Navigation keys are available to the user.</li>
<li>{@code navhidden}: Navigation keys are not available (such as behind a closed
lid).</li>
</ul>
<p>This can change during the life of your application if the user reveals the navigation
keys. See <a href="runtime-changes.html">Handling Runtime Changes</a> for
information about how this affects your application during runtime.</p>
<p>Also see the {@link android.content.res.Configuration#navigationHidden} configuration
field, which indicates whether navigation keys are hidden.</p>
</td>
</tr>
<tr id="TouchQualifier">
<td>Primary non-touch navigation method</td>
<td>
<code>nonav</code><br/>
<code>dpad</code><br/>
<code>trackball</code><br/>
<code>wheel</code>
</td>
<td>
<ul class="nolist">
<li>{@code nonav}: Device has no navigation facility other than using the
touchscreen.</li>
<li>{@code dpad}: Device has a directional-pad (d-pad) for navigation.</li>
<li>{@code trackball}: Device has a trackball for navigation.</li>
<li>{@code wheel}: Device has a directional wheel(s) for navigation (uncommon).</li>
</ul>
<p>Also see the {@link android.content.res.Configuration#navigation} configuration field,
which indicates the type of navigation method available.</p>
</td>
</tr>
<!-- DEPRECATED
<tr>
<td>Screen dimensions</td>
<td>Examples:<br/>
<code>320x240</code><br/>
<code>640x480</code><br/>
etc.
</td>
<td>
<p>The larger dimension must be specified first. <strong>This configuration is deprecated
and should not be used</strong>. Instead use "screen size," "wider/taller screens," and "screen
orientation" described above.</p>
</td>
</tr>
-->
<tr id="VersionQualifier">
<td>System Version (API Level)</td>
<td>Examples:<br/>
<code>v3</code><br/>
<code>v4</code><br/>
<code>v7</code><br/>
etc.</td>
<td>
<p>The API Level supported by the device. For example, <code>v1</code> for API Level
1 (devices with Android 1.0 or higher) and <code>v4</code> for API Level 4 (devices with Android
1.6 or higher). See the <a
href="{@docRoot}guide/appendix/api-levels.html">Android API Levels</a> document for more information
about these values.</p>
<p class="caution"><strong>Caution:</strong> Android 1.5 and 1.6 only match resources
with this qualifier when it exactly matches the system version. See the section below about <a
href="#KnownIssues">Known Issues</a> for more information.</p>
</td>
</tr>
</table>
<h3 id="QualifierRules">Qualifier name rules</h3>
<p>Here are some rules about using configuration qualifier names:</p>
<ul>
<li>You can specify multiple qualifiers for a single set of resources, separated by dashes. For
example, <code>drawable-en-rUS-land</code> applies to US-English devices in landscape
orientation.</li>
<li>The qualifiers must be in the order listed in <a href="#table2">table 2</a>. For
example:
<ul>
<li>Wrong: <code>drawable-hdpi-port/</code></li>
<li>Correct: <code>drawable-port-hdpi/</code></li>
</ul>
</li>
<li>Alternative resource directories cannot be nested. For example, you cannot have
<code>res/drawable/drawable-en/</code>.</li>
<li>Values are case-insensitive. The resource compiler converts directory names
to lower case before processing to avoid problems on case-insensitive
file systems. Any capitalization in the names is only to benefit readability.</li>
<li>Only one value for each qualifier type is supported. For example, if you want to use
the same drawable files for Spain and France, you <em>cannot</em> have a directory named
<code>drawable-rES-rFR/</code>. Instead you need two resource directories, such as
<code>drawable-rES/</code> and <code>drawable-rFR/</code>, which contain the appropriate files.
However, you are not required to actually duplicate the same files in both locations. Instead, you
can create an alias to a resource. See <a href="#AliasResources">Creating
alias resources</a> below.</li>
</ul>
<p>After you save alternative resources into directories named with
these qualifiers, Android automatically applies the resources in your application based on the
current device configuration. Each time a resource is requested, Android checks for alternative
resource directories that contain the requested resource file, then <a href="#BestMatch">finds the
best-matching resource</a> (discussed below). If there are no alternative resources that match
a particular device configuration, then Android uses the corresponding default resources (the
set of resources for a particular resource type that does not include a configuration
qualifier).</p>
<h3 id="AliasResources">Creating alias resources</h3>
<p>When you have a resource that you'd like to use for more than one device
configuration (but do not want to provide as a default resource), you do not need to put the same
resource in more than one alternative resource directory. Instead, you can (in some cases) create an
alternative
resource that acts as an alias for a resource saved in your default resource directory.</p>
<p class="note"><strong>Note:</strong> Not all resources offer a mechanism by which you can
create an alias to another resource. In particular, animation, menu, raw, and other unspecified
resources in the {@code xml/} directory do not offer this feature.</p>
<p>For example, imagine you have an application icon, {@code icon.png}, and need unique version of
it for different locales. However, two locales, English-Canadian and French-Canadian, need to
use the same version. You might assume that you need to copy the same image
into the resource directory for both English-Canadian and French-Canadian, but it's
not true. Instead, you can save the image that's used for both as {@code icon_ca.png} (any
name other than {@code icon.png}) and put
it in the default {@code res/drawable/} directory. Then create an {@code icon.xml} file in {@code
res/drawable-en-rCA/} and {@code res/drawable-fr-rCA/} that refers to the {@code icon_ca.png}
resource using the {@code &lt;bitmap&gt;} element. This allows you to store just one version of the
PNG file and two small XML files that point to it. (An example XML file is shown below.)</p>
<h4>Drawable</h4>
<p>To create an alias to an existing drawable, use the {@code &lt;bitmap&gt;} element.
For example:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon_ca" />
</pre>
<p>If you save this file as {@code icon.xml} (in an alternative resource directory, such as
{@code res/drawable-en-rCA/}), it is compiled into a resource that you
can reference as {@code R.drawable.icon}, but is actually an alias for the {@code
R.drawable.icon_ca} resource (which is saved in {@code res/drawable/}).</p>
<h4>Layout</h4>
<p>To create an alias to an existing layout, use the {@code &lt;include&gt;}
element, wrapped in a {@code &lt;merge&gt;}. For example:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;merge>
&lt;include layout="@layout/main_ltr"/>
&lt;/merge>
</pre>
<p>If you save this file as {@code main.xml}, it is compiled into a resource you can reference
as {@code R.layout.main}, but is actually an alias for the {@code R.layout.main_ltr}
resource.</p>
<h4>Strings and other simple values</h4>
<p>To create an alias to an existing string, simply use the resource ID of the desired
string as the value for the new string. For example:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;string name="hello">Hello&lt;/string>
&lt;string name="hi">@string/hello&lt;/string>
&lt;/resources>
</pre>
<p>The {@code R.string.hi} resource is now an alias for the {@code R.string.hello}.</p>
<p> <a href="{@docRoot}guide/topics/resources/more-resources.html">Other simple values</a> work the
same way. For example, a color:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;color name="yellow">#f00&lt;/color>
&lt;color name="highlight">@color/red&lt;/color>
&lt;/resources>
</pre>
<h2 id="Compatibility">Providing the Best Device Compatibility with Resources</h2>
<p>In order for your application to support multiple device configurations, it's very important that
you always provide default resources for each type of resource that your application uses.</p>
<p>For example, if your application supports several languages, always include a {@code
values/} directory (in which your strings are saved) <em>without</em> a <a
href="#LocaleQualifier">language and region qualifier</a>. If you instead put all your string files
in directories that have a language and region qualifier, then your application will crash when run
on a device set to a language that your strings do not support. But, as long as you provide default
{@code values/} resources, then your application will run properly (even if the user doesn't
understand that language&mdash;it's better than crashing).</p>
<p>Likewise, if you provide different layout resources based on the screen orientation, you should
pick one orientation as your default. For example, instead of providing layout resources in {@code
layout-land/} for landscape and {@code layout-port/} for portrait, leave one as the default, such as
{@code layout/} for landscape and {@code layout-port/} for portrait.</p>
<p>Providing default resources is important not only because your application might run on a
configuration you had not anticipated, but also because new versions of Android sometimes add
configuration qualifiers that older versions do not support. If you use a new resource qualifier,
but maintain code compatibility with older versions of Android, then when an older version of
Android runs your application, it will crash if you do not provide default resources, because it
cannot use the resources named with the new qualifier. For example, if your <a
href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
minSdkVersion}</a> is set to 4, and you qualify all of your drawable resources using <a
href="#NightQualifier">night mode</a> ({@code night} or {@code notnight}, which were added in API
Level 8), then an API Level 4 device cannot access your drawable resources and will crash. In this
case, you probably want {@code notnight} to be your default resources, so you should exclude that
qualifier so your drawable resources are in either {@code drawable/} or {@code drawable-night/}.</p>
<p>So, in order to provide the best device compatibility, always provide default
resources for the resources your application needs to perform properly. Then create alternative
resources for specific device configurations using the configuration qualifiers.</p>
<p>There is one exception to this rule: If your application's <a
href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is 4 or
greater, you <em>do not</em> need default drawable resources when you provide alternative drawable
resources with the <a href="#DensityQualifier">screen density</a> qualifier. Even without default
drawable resources, Android can find the best match among the alternative screen densities and scale
the bitmaps as necessary. However, for the best experience on all types of devices, you should
provide alternative drawables for all three types of density. If your <a
href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is
<em>less than</em> 4 (Android 1.5 or lower), be aware that the screen size, density, and aspect
qualifiers are not supported on Android 1.5 or lower, so you might need to perform additional
compatibility for these versions.</p>
<h3 id="ScreenCompatibility">Providing screen resource compatibility for Android 1.5</h3>
<p>Android 1.5 (and lower) does not support the following configuration qualifers:</p>
<dl>
<dt><a href="#DensityQualifier">Density</a></dt>
<dd>{@code ldpi}, {@code mdpi}, {@code ldpi}, and {@code nodpi}</dd>
<dt><a href="#ScreenSizeQualifier">Screen size</a></dt>
<dd>{@code small}, {@code normal}, and {@code large}</dd>
<dt><a href="#ScreenAspectQualifier">Screen aspect</a></dt>
<dd>{@code long} and {@code notlong}</dd>
</dl>
<p>These configuration qualifiers were introduced in Android 1.6, so Android 1.5 (API Level 3) and
lower does not support them. If you use these configuration qualifiers and do not provide
corresponding default resources, then an Android 1.5 device might use any one of the resource
directories named with the above screen configuration qualifiers, because it ignores these
qualifiers and uses whichever otherwise-matching drawable resource it finds first.</p>
<p>For example, if your application supports Android 1.5 and includes drawable resources for
each density type ({@code drawable-ldpi/}, {@code drawable-mdpi/}, and {@code drawable-ldpi/}),
and does <em>not</em> include default drawable resources ({@code drawable/}), then
an Android 1.5 will use drawables from any one of the alternative resource directories, which
can result in a user interface that's less than ideal.<p>
<p>So, to provide compatibility with Android 1.5 (and lower) when using the screen configuration
qualifiers:</p>
<ol>
<li>Provide default resources that are for medium-density, normal, and notlong screens.
<p>Because all Android 1.5 devices have medium-density, normal, not-long screens, you can
place these kinds of resources in the corresponding default resource directory. For example, put all
medium density drawable resources in {@code drawable/} (instead of {@code drawable-mdpi/}),
put {@code normal} size resources in the corresponding default resource directory, and {@code
notlong} resources in the corresponding default resource directory.</p>
</li>
<li>Ensure that your <a href="{@docRoot}sdk/tools-notes.html">SDK Tools</a> version
is r6 or greater.
<p>You need SDK Tools, Revision 6 (or greater), because it includes a new packaging tool that
automatically applies an appropriate <a href="#VersionQualifier">version qualifier</a> to any
resource directory named with a qualifier that does not exist in Android 1.0. For example, because
the density qualifier was introduced in Android 1.6 (API Level 4), when the packaging tool
encounters a resource directory using the density qualifier, it adds {@code v4} to the directory
name to ensure that older versions do not use those resources (only API Level 4 and higher support
that qualifier). Thus, by putting your medium-density resources in a directory <em>without</em> the
{@code mdpi} qualifier, they are still accessible by Android 1.5, and any device that supports the
density qualifer and has a medium-density screen also uses the default resources (which are mdpi)
because they are the best match for the device (instead of using the {@code ldpi} or {@code hdpi}
resources).</p>
</li>
</ol>
<p class="note"><strong>Note:</strong> Later versions of Android, such as API Level 8,
introduce other configuration qualifiers that older version do not support. To provide the best
compatibility, you should always include a set of default resources for each type of resource
that your application uses, as discussed above to provide the best device compatibility.</p>
<h2 id="BestMatch">How Android Finds the Best-matching Resource</h2>
<p>When you request a resource for which you provide alternatives, Android selects which
alternative resource to use at runtime, depending on the current device configuration. To
demonstrate how Android selects an alternative resource, assume the following drawable directories
each contain different versions of the same images:</p>
<pre class="classic no-pretty-print">
drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
</pre>
<p>And assume the following is the device configuration:</p>
<p style="margin-left:1em;">
Locale = <code>en-GB</code> <br/>
Screen orientation = <code>port</code> <br/>
Screen pixel density = <code>hdpi</code> <br/>
Touchscreen type = <code>notouch</code> <br/>
Primary text input method = <code>12key</code>
</p>
<p>By comparing the device configuration to the available alternative resources, Android selects
drawables from {@code drawable-en-port}. It arrives at this decision using the following logic:</p>
<div class="figure" style="width:280px">
<img src="{@docRoot}images/resources/res-selection-flowchart.png" alt="" height="590" />
<p class="img-caption"><strong>Figure 2.</strong> Flowchart of how Android finds the
best-matching resource.</p>
</div>
<ol>
<li>Eliminate resource files that contradict the device configuration.
<p>The <code>drawable-fr-rCA/</code> directory is eliminated, because it
contradicts the <code>en-GB</code> locale.</p>
<pre class="classic no-pretty-print">
drawable/
drawable-en/
<strike>drawable-fr-rCA/</strike>
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
</pre>
<p class="note"><strong>Exception:</strong> Screen pixel density is the one qualifier that is not
eliminated due to a contradiction. Even though the screen density of the device is hdpi,
<code>drawable-port-ldpi/</code> is not eliminated because every screen density is
considered to be a match at this point. More information is available in the <a
href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
Screens</a> document.</p></li>
<li>Pick the (next) highest-precedence qualifier in the list (<a href="#table2">table 2</a>).
(Start with MCC, then move down.) </li>
<li>Do any of the resource directories include this qualifier? </li>
<ul>
<li>If No, return to step 2 and look at the next qualifier. (In the example,
the answer is &quot;no&quot; until the language qualifier is reached.)</li>
<li>If Yes, continue to step 4.</li>
</ul>
</li>
<li>Eliminate resource directories that do not include this qualifier. In the example, the system
eliminates all the directories that do not include a language qualifier:</li>
<pre class="classic no-pretty-print">
<strike>drawable/</strike>
drawable-en/
drawable-en-port/
drawable-en-notouch-12key/
<strike>drawable-port-ldpi/</strike>
<strike>drawable-port-notouch-12key/</strike>
</pre>
<p class="note"><strong>Exception:</strong> If the qualifier in question is screen pixel density,
Android selects the option that most closely matches the device screen density.
In general, Android prefers scaling down a larger original image to scaling up a smaller
original image. See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
Screens</a>.</p>
</li>
<li>Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen
orientation is the next qualifier for which there are any matches.
So, resources that do not specify a screen orientation are eliminated:
<pre class="classic no-pretty-print">
<strike>drawable-en/</strike>
drawable-en-port/
<strike>drawable-en-notouch-12key/</strike>
</pre>
<p>The remaining directory is {@code drawable-en-port}.</p>
</li>
</ol>
<p>Though this procedure is executed for each resource requested, the system further optimizes
some aspects. One such optimization is that once the device configuration is known, it might
eliminate alternative resources that can never match. For example, if the configuration
language is English ("en"), then any resource directory that has a language qualifier set to
something other than English is never included in the pool of resources checked (though a
resource directory <em>without</em> the language qualifier is still included).</p>
<p class="note"><strong>Note:</strong> The <em>precedence</em> of the qualifier (in <a
href="#table2">table 2</a>) is more important
than the number of qualifiers that exactly match the device. For example, in step 4 above, the last
choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen
type, and input method), while <code>drawable-en</code> has only one parameter that matches
(language). However, language has a higher precedence than these other qualifiers, so
<code>drawable-port-notouch-12key</code> is out.</p>
<p>To learn more about how to use resources in your application, continue to <a
href="accessing-resources.html">Accessing Resources</a>.</p>
<h2 id="KnownIssues">Known Issues</h2>
<h3>Android 1.5 and 1.6: Version qualifier performs exact match, instead of best match</h3>
<p>The correct behavior is for the system to match resources marked with a <a
href="#VersionQualifier">version qualifier</a> equal
to or less than the system version on the device, but on Android 1.5 and 1.6, (API Level 3 and 4),
there is a bug that causes the system to match resources marked with the version qualifier
only when it exactly matches the version on the device.</p>
<p><b>The workaround:</b> To provide version-specific resources, abide by this behavior. However,
because this bug is fixed in versions of Android available after 1.6, if
you need to differentiate resources between Android 1.5, 1.6, and later versions, then you only need
to apply the version qualifier to the 1.6 resources and one to match all later versions. Thus, this
is effectively a non-issue.</p>
<p>For example, if you want drawable resources that are different on each Android 1.5, 1.6,
and 2.0.1 (and later), create three drawable directories: {@code drawable/} (for 1.5 and lower),
{@code drawable-v4} (for 1.6), and {@code drawable-v6} (for 2.0.1 and later&mdash;version 2.0, v5,
is no longer available).</p>

View File

@ -0,0 +1,8 @@
page.title=Application Resources
@jd:body
<script type="text/javascript">
window.location = toRoot + "guide/topics/resources/index.html";
</script>
<p><strong>This document has moved. Please see <a href="index.html">Application Resources</a>.</strong></p>

View File

@ -0,0 +1,247 @@
page.title=Handling Runtime Changes
parent.title=Application Resources
parent.link=index.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>In this document</h2>
<ol>
<li><a href="#RetainingAnObject">Retaining an Object During a Configuration Change</a></li>
<li><a href="#HandlingTheChange">Handling the Configuration Change Yourself</a>
</ol>
<h2>See also</h2>
<ol>
<li><a href="providing-resources.html">Providing Resources</a></li>
<li><a href="accessing-resources.html">Accessing Resources</a></li>
<li><a href="{@docRoot}resources/articles/faster-screen-orientation-change.html">Faster Screen
Orientation Change</a></li>
</ol>
</div>
</div>
<p>Some device configurations can change during runtime
(such as screen orientation, keyboard availability, and language). When such a change occurs,
Android restarts the running
Activity ({@link android.app.Activity#onDestroy()} is called, followed by {@link
android.app.Activity#onCreate(Bundle) onCreate()}). The restart behavior is designed to help your
application adapt to new configurations by automatically reloading your application with
alternative resources.</p>
<p>To properly handle a restart, it is important that your Activity restores its previous
state through the normal <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Activity
lifecycle</a>, in which Android calls
{@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} before it destroys
your Activity so that you can save data about the application state. You can then restore the state
during {@link android.app.Activity#onCreate(Bundle) onCreate()} or {@link
android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()}. To test
that your application restarts itself with the application state intact, you should
invoke configuration changes (such as changing the screen orientation) while performing various
tasks in your application.</p>
<p>Your application should be able to restart at any time without loss of user data or
state in order to handle events such as when the user receives an incoming phone call and then
returns to your application (read about the
<a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Activity lifecycle</a>).</p>
<p>However, you might encounter a situation in which restarting your application and
restoring significant amounts of data can be costly and create a poor user experience. In such a
situation, you have two options:</p>
<ol type="a">
<li><a href="#RetainingAnObject">Retain an object during a configuration change</a>
<p>Allow your Activity to restart when a configuration changes, but carry a stateful
{@link java.lang.Object} to the new instance of your Activity.</p>
</li>
<li><a href="#HandlingTheChange">Handle the configuration change yourself</a>
<p>Prevent the system from restarting your Activity during certain configuration
changes and receive a callback when the configurations do change, so that you can manually update
your Activity as necessary.</p>
</li>
</ol>
<h2 id="RetainingAnObject">Retaining an Object During a Configuration Change</h2>
<p>If restarting your Activity requires that you recover large sets of data, re-establish a
network connection, or perform other intensive operations, then a full restart due to a
configuration change might
be an unpleasant user experience. Also, it may not be possible for you to completely
maintain your Activity state with the {@link android.os.Bundle} that the system saves for you during
the Activity lifecycle&mdash;it is not designed to carry large objects (such as bitmaps) and the
data within it must be serialized then deserialized, which can consume a lot of memory and make the
configuration change slow. In such a situation, you can alleviate the burden of reinitializing
your Activity by retaining a stateful Object when your Activity is restarted due to a configuration
change.</p>
<p>To retain an Object during a runtime configuration change:</p>
<ol>
<li>Override the {@link android.app.Activity#onRetainNonConfigurationInstance()} method to return
the Object you would like to retain.</li>
<li>When your Activity is created again, call {@link
android.app.Activity#getLastNonConfigurationInstance()} to recover your Object.</li>
</ol>
<p>Android calls {@link android.app.Activity#onRetainNonConfigurationInstance()} between {@link
android.app.Activity#onStop()} and {@link
android.app.Activity#onDestroy()} when it shuts down your Activity due to a configuration
change. In your implementation of {@link
android.app.Activity#onRetainNonConfigurationInstance()}, you can return any {@link
java.lang.Object} that you need in order to efficiently restore your state after the configuration
change.</p>
<p>A scenario in which this can be valuable is if your application loads a lot of data from the
web. If the user changes the orientation of the device and the Activity restarts, your application
must re-fetch the data, which could be slow. What you can do instead is implement
{@link android.app.Activity#onRetainNonConfigurationInstance()} to return an object carrying your
data and then retrieve the data when your Activity starts again with {@link
android.app.Activity#getLastNonConfigurationInstance()}. For example:</p>
<pre>
&#64;Override
public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}
</pre>
<p class="caution"><strong>Caution:</strong> While you can return any object, you
should never pass an object that is tied to the {@link android.app.Activity}, such as a {@link
android.graphics.drawable.Drawable}, an {@link android.widget.Adapter}, a {@link android.view.View}
or any other object that's associated with a {@link android.content.Context}. If you do, it will
leak all the Views and resources of the original Activity instance. (To leak the resources
means that your application maintains a hold on them and they cannot be garbage-collected, so
lots of memory can be lost.)</p>
<p>Then retrieve the {@code data} when your Activity starts again:</p>
<pre>
&#64;Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data = loadMyData();
}
...
}
</pre>
<p>In this case, {@link android.app.Activity#getLastNonConfigurationInstance()} retrieves
the data saved by {@link android.app.Activity#onRetainNonConfigurationInstance()}. If {@code data}
is null (which happens when the
Activity starts due to any reason other than a configuration change) then the data object is loaded
from the original source.</p>
<h2 id="HandlingTheChange">Handling the Configuration Change Yourself</h2>
<p>If your application doesn't need to update resources during a specific configuration
change <em>and</em> you have a performance limitation that requires you to
avoid the Activity restart, then you can declare that your Activity handles the configuration change
itself, which prevents the system from restarting your Activity.</p>
<p class="note"><strong>Note:</strong> Handling the configuration change yourself can make it much
more difficult to use alternative resources, because the system does not automatically apply them
for you. This technique should be considered a last resort and is not recommended for most
applications.</p>
<p>To declare that your Activity handles a configuration change, edit the appropriate <a
href="{@docRoot}guide/topics/manifest/activity-element.html">{@code &lt;activity&gt;}</a> element
in your manifest file to include the <a
href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
android:configChanges}</a> attribute with a string value that represents the configuration that you
want to handle. Possible values are listed in the documentation for
the <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
android:configChanges}</a> attribute (the most commonly used values are {@code orientation} to
handle when the screen orientation changes and {@code keyboardHidden} to handle when the
keyboard availability changes). You can declare multiple configuration values in the attribute
by separating them with a pipe character ("|").</p>
<p>For example, the following manifest snippet declares an Activity that handles both the
screen orientation change and keyboard availability change:</p>
<pre>
&lt;activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
</pre>
<p>Now when one of these configurations change, {@code MyActivity} is not restarted.
Instead, the Activity receives a call to {@link
android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. This method
is passed a {@link android.content.res.Configuration} object that specifies
the new device configuration. By reading fields in the {@link android.content.res.Configuration},
you can determine the new configuration and make appropriate changes by updating
the resources used in your interface. At the
time this method is called, your Activity's {@link android.content.res.Resources} object is updated
to return resources based on the new configuration, so you can easily
reset elements of your UI without the system restarting your Activity.</p>
<p>For example, the following {@link
android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} implementation
checks the availability of a hardware keyboard and the current device orientation:</p>
<pre>
&#64;Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
</pre>
<p>The {@link android.content.res.Configuration} object represents all of the current
configurations, not just the ones that have changed. Most of the time, you won't care exactly how
the configuration has changed and can simply re-assign all your resources that provide alternatives
to the configuration that you're handling. For example, because the {@link
android.content.res.Resources} object is now updated, you can reset
any {@link android.widget.ImageView}s with {@link android.widget.ImageView#setImageResource(int)}
and the appropriate resource for the new configuration is used (as described in <a
href="providing-resources.html#AlternateResources">Providing Resources</a>).</p>
<p>Notice that the values from the {@link
android.content.res.Configuration} fields are integers that are matched to specific constants
from the {@link android.content.res.Configuration} class. For documentation about which constants
to use with each field, refer to the appropriate field in the {@link
android.content.res.Configuration} reference.</p>
<p class="note"><strong>Remember:</strong> When you declare your Activity to handle a configuration
change, you are responsible for resetting any elements for which you provide alternatives. If you
declare your Activity to handle the orientation change and have images that should change
between landscape and portrait, you must re-assign each resource to each element during {@link
android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.</p>
<p>If you don't need to update your application based on these configuration
changes, you can instead <em>not</em> implement {@link
android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. In
which case, all of the resources used before the configuration change are still used
and you've only avoided the restart of your Activity. However, your application should always be
able to shutdown and restart with its previous state intact. Not only because
there are other configuration changes that you cannot prevent from restarting your application but
also in order to handle events such as when the user receives an incoming phone call and then
returns to your application.</p>
<p>For more about which configuration changes you can handle in your Activity, see the <a
href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
android:configChanges}</a> documentation and the {@link android.content.res.Configuration}
class.</p>

View File

@ -0,0 +1,444 @@
page.title=String Resources
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<p>A string resource provides text strings for your application
with optional text styling and formatting. There are three types of resources that can provide
your application with strings:</p>
<dl>
<dt><a href="#String">String</a></dt>
<dd>XML resource that provides a single string.</dd>
<dt><a href="#StringArray">String Array</a></dt>
<dd>XML resource that provides an array of strings.</dd>
<dt><a href="#Plurals">Plurals</a></dt>
<dd>XML resource that carries different strings for different pluralizations
of the same word or phrase.</dd>
</dl>
<p>All strings are capable of applying some styling markup and formatting arguments. For
information about styling and formatting strings, see the section about <a
href="#FormattingAndStyling">Formatting and Styling</a>.</p>
<h2 id="String">String</h2>
<p>A single string that can be referenced from the application or from other resource files (such
as an XML layout).</p>
<p class="note"><strong>Note:</strong> A string is a simple resource that is referenced
using the value provided in the {@code name} attribute (not the name of the XML file). So, you can
combine string resources with other simple resources in the one XML file,
under one {@code &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;string>} element's {@code name} will be used as the
resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to a {@link java.lang.String}.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.string.<em>string_name</em></code><br/>
In XML:<code>@string/<em>string_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#string-resources-element">resources</a>>
&lt;<a href="#string-element">string</a>
name="<em>string_name</em>"
&gt;<em>text_string</em>&lt;/string&gt;
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="string-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="string-element"><code>&lt;string&gt;</code></dt>
<dd>A string, which can include styling tags. Beware that you must escape apostrophes and
quotation marks. For more information about how to properly style and format your strings see <a
href="#FormattingAndStyling">Formatting and Styling</a>, below.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the string. This name will be used as the resource
ID.</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/strings.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;string name="hello">Hello!&lt;/string>
&lt;/resources>
</pre>
<p>This layout XML applies a string to a View:</p>
<pre>
&lt;TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<strong>android:text="@string/hello"</strong> />
</pre>
<p>This application code retrieves a string:</p>
<pre>
String string = {@link android.content.Context#getString(int) getString}(R.string.hello);
</pre>
<p>You can use either {@link android.content.Context#getString(int)} or
{@link android.content.Context#getText(int)} to retieve a string. {@link
android.content.Context#getText(int)} will retain any rich text styling applied to the string.</p>
</dd> <!-- end example -->
</dl>
<h2 id="StringArray">String Array</h2>
<p>An array of strings that can be referenced from the application.</p>
<p class="note"><strong>Note:</strong> A string 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 string array resources with other simple resources in the one XML file,
under one {@code &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;string-array>} element's {@code name} will be used as the
resource ID.</dd>
<dt>compiled resource datatype:</dt>
<dd>Resource pointer to an array of {@link java.lang.String}s.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.array.<em>string_array_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#string-array-resources-element">resources</a>>
&lt;<a href="#string-array-element">string-array</a>
name="<em>string_array_name</em>">
&lt;<a href="#string-array-item-element">item</a>
&gt;<em>text_string</em>&lt;/item&gt;
&lt;/string-array>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="string-array-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="string-array-element"><code>&lt;string-array&gt;</code></dt>
<dd>Defines an array of strings. Contains one or more {@code &lt;item>} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the array. This name will be used as the resource
ID to reference the array.</dd>
</dl>
</dd>
<dt id="string-array-item-element"><code>&lt;item&gt;</code></dt>
<dd>A string, which can include styling tags. The value can be a referenced to another
string resource. Must be a child of a {@code &lt;string-array&gt;} element. Beware that you
must escape apostrophes and
quotation marks. See <a href="#FormattingAndStyling">Formatting and Styling</a>, below, for
information about to properly style and format your strings.
<p>No attributes.</p>
</dd>
</dl>
</dd> <!-- end elements -->
<dt>example:</dt>
<dd>XML file saved at <code>res/values/strings.xml</code>:
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;string-array name="planets_array">
&lt;item>Mercury&lt;/item>
&lt;item>Venus&lt;/item>
&lt;item>Earth&lt;/item>
&lt;item>Mars&lt;/item>
&lt;/string-array>
&lt;/resources>
</pre>
<p>This application code retrieves a string array:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
String[] planets = res.{@link android.content.res.Resources#getStringArray(int)
getStringArray}(R.array.planets_array);
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="Plurals">Plurals</h2>
<p>A pair of strings that each provide a different plural form of the same word or phrase,
which you can collectively reference from the application. When you request the plurals
resource using a method such as {@link android.content.res.Resources#getQuantityString(int,int)
getQuantityString()}, you must pass a "count", which will determine the plural form you
require and return that string to you.</p>
<p class="note"><strong>Note:</strong> A plurals collection 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 plurals resources with other simple resources in the one
XML file, under one {@code &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The {@code &lt;plurals>} element's {@code name} will be used as the
resource ID.</dd>
<dt>resource reference:</dt>
<dd>
In Java: <code>R.plurals.<em>plural_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#plurals-resources-element">resources</a>>
&lt;<a href="#plurals-element">plurals</a>
name="<em>plural_name</em>">
&lt;<a href="#plurals-item-element">item</a>
quantity=["one" | "other"]
&gt;<em>text_string</em>&lt;/item>
&lt;/plurals>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="plurals-resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="plurals-element"><code>&lt;plurals&gt;</code></dt>
<dd>A collection of strings, of which, one string is provided depending on the amount of
something. Contains one or more {@code &lt;item>} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. A name for the pair of strings. This name will be used as the
resource ID.</dd>
</dl>
</dd>
<dt id="plurals-item-element"><code>&lt;item&gt;</code></dt>
<dd>A plural or singular string. The value can be a referenced to another
string resource. Must be a child of a {@code &lt;plurals&gt;} element. Beware that you must
escape apostrophes and quotation marks. See <a href="#FormattingAndStyling">Formatting and
Styling</a>, below, for information about to properly style and format your strings.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>quantity</code></dt>
<dd><em>Keyword</em>. A value indicating the case in which this string should be used. Valid
values:
<table>
<tr><th>Value</th><th>Description</th></tr>
<tr>
<td>{@code one}</td><td>When there is one (a singular string).</td>
</tr>
<tr>
<td>{@code other}</td><td>When the quantity is anything other than one (a plural
string, but also used when the count is zero).</td>
</tr>
</table>
</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements -->
<dt>example:</dt>
<dd>XML file saved at {@code res/values/strings.xml}:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;plurals name="numberOfSongsAvailable">
&lt;item quantity="one">One song found.&lt;/item>
&lt;item quantity="other">%d songs found.&lt;/item>
&lt;/plurals>
&lt;/resources>
</pre>
<p>Java code:</p>
<pre>
int count = getNumberOfsongsAvailable();
Resources res = {@link android.content.Context#getResources()};
String songsFound = res.{@link android.content.res.Resources#getQuantityString(int,int)
getQuantityString}(R.plurals.numberOfSongsAvailable, count);
</pre>
</dd> <!-- end example -->
</dl>
<h2 id="FormattingAndStyling">Formatting and Styling</h2>
<p>Here are a few important things you should know about how to properly
format and style your string resources.</p>
<h3>Escaping apostrophes and quotes</h3>
<p>If you have an apostrophe or a quote in your string, you must either escape it or enclose the
whole string in the other type of enclosing quotes. For example, here are some stings that
do and don't work:</p>
<pre>
&lt;string name="good_example">"This'll work"&lt;/string>
&lt;string name="good_example_2">This\'ll also work&lt;/string>
&lt;string name="bad_example">This doesn't work&lt;/string>
&lt;string name="bad_example_2">XML encodings don&amp;apos;t work&lt;/string>
</pre>
<h3>Formatting strings</h3>
<p>If you need to format your strings using <a
href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">{@code String.format(String, Object...)}</a>,
then you can do so by putting
your format arguments in the string resource. For example, with the following resource:</p>
<pre>
&lt;string name="welcome_messages">Hello, %1$s! You have %2$d new messages.&lt;/string>
</pre>
<p>In this example, the format string has two arguments: {@code %1$s} is a string and {@code %2$d}
is a decimal number. You can format the string with arguements from your application like this:</p>
<pre>
Resources res = {@link android.content.Context#getResources()};
String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount);
</pre>
<h3>Styling with HTML markup</h3>
<p>You can add styling to your strings with HTML markup. For example:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;string name="welcome">Welcome to &lt;b>Android&lt;/b>!&lt;/string>
&lt;/resources>
</pre>
<p>Supported HTML elements include:</p>
<ul>
<li>{@code &lt;b>} for <b>bold</b> text.</li>
<li>{@code &lt;i>} for <i>italic</i> text.</li>
<li>{@code &lt;u>} for <u>underline</u> text.</li>
</ul>
<p>Sometimes you may want to create a styled text resource that is also used as a format
string. Normally, this won't work because the <a
href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">{@code String.format(String, Object...)}</a>
method will strip all the style
information from the string. The work-around to this is to write the HTML tags with escaped
entities, which are then recovered with {@link android.text.Html#fromHtml(String)},
after the formatting takes place. For example:</p>
<ol>
<li>Store your styled text resource as an HTML-escaped string:
<pre>
&lt;resources&gt;
&lt;string name="welcome_messages"&gt;Hello, %1$s! You have &amp;lt;b>%2$d new messages&amp;lt;/b>.&lt;/string>
&lt;/resources&gt;
</pre>
<p>In this formatted string, a {@code &lt;b>} element is added. Notice that the opening bracket is
HTML-escaped, using the {@code &amp;lt;} notation.</p>
</li>
<li>Then format the string as usual, but also call {@link android.text.Html#fromHtml} to
convert the HTML text into styled text:
<pre>
Resources res = {@link android.content.Context#getResources()};
String text = String.<a
href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
</pre>
</li>
</ol>
<p>Because the {@link android.text.Html#fromHtml} method will format all HTML entities, be sure to
escape any possible HTML characters in the strings you use with the formatted text, using
{@link android.text.TextUtils#htmlEncode}. For instance, if you'll be passing a string argument to
<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">{@code String.format()}</a> that may contain characters such as
"&lt;" or "&amp;", then they must be escaped before formatting, so that when the formatted string
is passed through {@link android.text.Html#fromHtml}, the characters come out the way they were
originally written. For example:</p>
<pre>
String escapedUsername = TextUtil.{@link android.text.TextUtils#htmlEncode htmlEncode}(username);
Resources res = {@link android.content.Context#getResources()};
String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
CharSequence styledText = Html.fromHtml(text);
</pre>

View File

@ -0,0 +1,128 @@
page.title=Style Resource
parent.title=Resource Types
parent.link=available-resources.html
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>See also</h2>
<ol>
<li><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a></li>
</ol>
</div>
</div>
<p>A style resource defines the format and look for a UI.
A style can be applied to an individual {@link android.view.View} (from within a layout file) or to
an entire {@link android.app.Activity} or application (from within the manifest file).</p>
<p>For more information about creating and applying styles, please read
<a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>.</p>
<p class="note"><strong>Note:</strong> A style 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 style resources with other simple resources in the one XML file,
under one {@code &lt;resources>} element.</p>
<dl class="xml">
<dt>file location:</dt>
<dd><code>res/values/<em>filename</em>.xml</code><br/>
The filename is arbitrary. The element's {@code name} will be used as the resource ID.</dd>
<dt>resource reference:</dt>
<dd>
In XML: <code>@[package:]style/<em>style_name</em></code>
</dd>
<dt>syntax:</dt>
<dd>
<pre class="stx">
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;<a href="#resources-element">resources</a>>
&lt;<a href="#style-element">style</a>
name="<em>style_name</em>"
parent="@[package:]style/<em>style_to_inherit</em>">
&lt;<a href="#item-element">item</a>
name="<em>[package:]style_property_name</em>"
&gt;<em>style_value</em>&lt;/item>
&lt;/style>
&lt;/resources>
</pre>
</dd>
<dt>elements:</dt>
<dd>
<dl class="tag-list">
<dt id="resources-element"><code>&lt;resources&gt;</code></dt>
<dd><strong>Required.</strong> This must be the root node.
<p>No attributes.</p>
</dd>
<dt id="style-element"><code>&lt;style&gt;</code></dt>
<dd>Defines a single style. Contains {@code &lt;item&gt;} elements.
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>String</em>. <strong>Required</strong>. A name for the style, which is used as the
resource ID to apply the style to a View, Activity, or application.
</dd>
<dt><code>parent</code></dt>
<dd><em>Style resource</em>. Reference to a style from which this
style should inherit style properties.
</dd>
</dl>
</dd>
<dt id="item-element"><code>&lt;item&gt;</code></dt>
<dd>Defines a single property for the style. Must be a child of a
<code>&lt;style&gt;</code> element.</p>
<p class="caps">attributes:</p>
<dl class="atn-list">
<dt><code>name</code></dt>
<dd><em>Attribute resource</em>. <strong>Required</strong>. The name of the style property
to be defined, with a package prefix if necessary (for example {@code android:textColor}).
</dd>
</dl>
</dd>
</dl>
</dd> <!-- end elements and attributes -->
<dt>example:</dt>
<dd>
<dl>
<dt>XML file for the style (saved in <code>res/values/</code>):</dt>
<dd>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;style name="CustomText" parent="@style/Text">
&lt;item name="android:textSize">20sp&lt;/item>
&lt;item name="android:textColor">#008&lt;/item>
&lt;/style>
&lt;/resources>
</pre>
</dd>
<dt>XML file that applies the style to a {@link android.widget.TextView}
(saved in <code>res/layout/</code>):</dt>
<dd>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
</pre>
</dd>
</dl>
</dd> <!-- end example -->
</dl>