Home | History | Annotate | Download | only in resources
      1 page.title=More Resource Types
      2 parent.title=Resource Types
      3 parent.link=available-resources.html
      4 @jd:body
      5 
      6 <p>This page defines more types of resources you can externalize, including:</p>
      7 
      8 <dl>
      9   <dt><a href="#Bool">Bool</a></dt>
     10     <dd>XML resource that carries a boolean value.</dd>
     11   <dt><a href="#Color">Color</a></dt>
     12     <dd>XML resource that carries a color value (a hexadecimal color).</dd>
     13   <dt><a href="#Dimension">Dimension</a></dt>
     14     <dd>XML resource that carries a dimension value (with a unit of measure).</dd>
     15   <dt><a href="#Id">ID</a></dt>
     16     <dd>XML resource that provides a unique identifier for application resources and
     17 components.</dd>
     18   <dt><a href="#Integer">Integer</a></dt>
     19     <dd>XML resource that carries an integer value.</dd>
     20   <dt><a href="#IntegerArray">Integer Array</a></dt>
     21     <dd>XML resource that provides an array of integers.</dd>
     22   <dt><a href="#TypedArray">Typed Array</a></dt>
     23     <dd>XML resource that provides a {@link android.content.res.TypedArray} (which you can use
     24 for an array of drawables).</dd>
     25 </dl>
     26 
     27 
     28 
     29 
     30 <h2 id="Bool">Bool</h2>
     31 
     32 <p>A boolean value defined in XML.</p>
     33 
     34 <p class="note"><strong>Note:</strong> A bool is a simple resource that is referenced
     35 using the value provided in the {@code name} attribute (not the name of the XML file). As
     36 such, you can combine bool resources with other simple resources in the one XML file,
     37 under one {@code &lt;resources>} element.</p>
     38 
     39 <dl class="xml">
     40 
     41 <dt>file location:</dt>
     42 <dd><code>res/values/<em>filename</em>.xml</code><br/>
     43 The filename is arbitrary. The {@code &lt;bool>} element's {@code name} will be used as the resource
     44 ID.</dd>
     45 
     46 <dt>resource reference:</dt>
     47 <dd>
     48 In Java: <code>R.bool.<em>bool_name</em></code><br/>
     49 In XML: <code>@[<em>package</em>:]bool/<em>bool_name</em></code>
     50 </dd>
     51 
     52 <dt>syntax:</dt>
     53 <dd>
     54 <pre class="stx">
     55 &lt;?xml version="1.0" encoding="utf-8"?>
     56 &lt;<a href="#bool-resources-element">resources</a>&gt;
     57     &lt;<a href="#bool-element">bool</a>
     58         name="<em>bool_name</em>"
     59         &gt;[true | false]&lt;/bool>
     60 &lt;/resources>
     61 </pre>
     62 </dd>
     63 
     64 <dt>elements:</dt>
     65 <dd>
     66 <dl class="tag-list">
     67 
     68   <dt id="bool-resources-element"><code>&lt;resources&gt;</code></dt>
     69     <dd><strong>Required.</strong> This must be the root node.
     70       <p>No attributes.</p>
     71     </dd>
     72   <dt id="bool-element"><code>&lt;bool&gt;</code></dt>
     73     <dd>A boolean value: {@code true} or {@code false}.
     74       <p class="caps">attributes:</p>
     75       <dl class="atn-list">
     76         <dt><code>name</code></dt>
     77         <dd><em>String</em>. A name for the bool value. This will be used as the resource ID.</dd>
     78       </dl>
     79     </dd>
     80 
     81 </dl>
     82 </dd> <!-- end  elements and attributes -->
     83 
     84 <dt>example:</dt>
     85 <dd>XML file saved at <code>res/values-small/bools.xml</code>:
     86 <pre>
     87 &lt;?xml version="1.0" encoding="utf-8"?>
     88 &lt;resources&gt;
     89     &lt;bool name="screen_small">true&lt;/bool>
     90     &lt;bool name="adjust_view_bounds">true&lt;/bool>
     91 &lt;/resources>
     92 </pre>
     93 
     94   <p>This application code retrieves the boolean:</p>
     95 <pre>
     96 Resources res = {@link android.content.Context#getResources()};
     97 boolean screenIsSmall = res.{@link android.content.res.Resources#getBoolean(int) getBoolean}(R.bool.screen_small);
     98 </pre>
     99   <p>This layout XML uses the boolean for an attribute:</p>
    100 <pre>
    101 &lt;ImageView
    102     android:layout_height="fill_parent"
    103     android:layout_width="fill_parent"
    104     android:src="@drawable/logo"
    105     android:adjustViewBounds="@bool/adjust_view_bounds" />
    106 </pre>
    107 </dd> <!-- end example -->
    108 
    109 </dl>
    110 
    111 
    112 
    113 
    114 <h2 id="Color">Color</h2>
    115 
    116 <p>A color value defined in XML.
    117 The color is specified with an RGB value and alpha channel. You can use a color resource
    118 any place that accepts a hexadecimal color value. You can also use a color resource when a
    119 drawable resource is expected in XML (for example, {@code android:drawable="@color/green"}).</p>
    120 
    121 <p>The value always begins with a pound (#) character and then followed by the
    122 Alpha-Red-Green-Blue information in one of the following formats:</p>
    123 <ul>
    124   <li>#<em>RGB</em></li>
    125   <li>#<em>ARGB</em></li>
    126   <li>#<em>RRGGBB</em></li>
    127   <li>#<em>AARRGGBB</em></li>
    128 </ul>
    129 
    130 <p class="note"><strong>Note:</strong> A color is a simple resource that is referenced
    131 using the value provided in the {@code name} attribute (not the name of the XML file). As
    132 such, you can combine color resources with other simple resources in the one XML file,
    133 under one {@code &lt;resources>} element.</p>
    134 
    135 <dl class="xml">
    136 
    137 <dt>file location:</dt>
    138 <dd><code>res/values/colors.xml</code><br/>
    139 The filename is arbitrary. The {@code &lt;color>} element's {@code name} will be used as the
    140 resource ID.</dd>
    141 
    142 <dt>resource reference:</dt>
    143 <dd>
    144 In Java: <code>R.color.<em>color_name</em></code><br/>
    145 In XML: <code>@[<em>package</em>:]color/<em>color_name</em></code>
    146 </dd>
    147 
    148 <dt>syntax:</dt>
    149 <dd>
    150 <pre class="stx">
    151 &lt;?xml version="1.0" encoding="utf-8"?>
    152 &lt;<a href="#color-resources-element">resources</a>>
    153     &lt;<a href="#color-element">color</a>
    154         name="<em>color_name</em>"
    155         &gt;<em>hex_color</em>&lt;/color>
    156 &lt;/resources>
    157 </pre>
    158 </dd>
    159 
    160 <dt>elements:</dt>
    161 <dd>
    162 <dl class="tag-list">
    163 
    164   <dt id="color-resources-element"><code>&lt;resources&gt;</code></dt>
    165     <dd><strong>Required.</strong> This must be the root node.
    166       <p>No attributes.</p>
    167     </dd>
    168   <dt id="color-element"><code>&lt;color&gt;</code></dt>
    169     <dd>A color expressed in hexadecimal, as described above.
    170       <p class="caps">attributes:</p>
    171       <dl class="atn-list">
    172         <dt><code>name</code></dt>
    173         <dd><em>String</em>. A name for the color. This will be used as the resource ID.
    174         </dd>
    175       </dl>
    176     </dd>
    177 
    178 </dl>
    179 </dd> <!-- end  elements and attributes -->
    180 
    181 <dt>example:</dt>
    182 <dd>XML file saved at <code>res/values/colors.xml</code>:
    183 <pre>
    184 &lt;?xml version="1.0" encoding="utf-8"?>
    185 &lt;resources>
    186    &lt;color name="opaque_red">#f00&lt;/color>
    187    &lt;color name="translucent_red">#80ff0000&lt;/color>
    188 &lt;/resources>
    189 </pre>
    190 
    191     <p>This application code retrieves the color resource:</p>
    192 <pre>
    193 Resources res = {@link android.content.Context#getResources()};
    194 int color = res.{@link android.content.res.Resources#getColor(int) getColor}(R.color.opaque_red);
    195 </pre>
    196   <p>This layout XML applies the color to an attribute:</p>
    197 <pre>
    198 &lt;TextView
    199     android:layout_width="fill_parent"
    200     android:layout_height="wrap_content"
    201     android:textColor="@color/translucent_red"
    202     android:text="Hello"/>
    203 </pre>
    204 </dd> <!-- end example -->
    205 
    206 </dl>
    207 
    208 
    209 
    210 
    211 
    212 <h2 id="Dimension">Dimension</h2>
    213 
    214 <p>A dimension value defined in XML. A dimension
    215 is specified with a number followed by a unit of measure.
    216 For example: 10px, 2in, 5sp. The following units of measure are supported by Android:</p>
    217 <dl>
    218   <dt>{@code dp}</dt>
    219     <dd>Density-independent Pixels - an abstract unit that is based on the physical density of the
    220 screen. These units are relative to a 160 dpi (dots per inch) screen, so <em>{@code 160dp} is
    221 always one inch</em> regardless of the screen density. The ratio of dp-to-pixel will change with the
    222 screen density, but not necessarily in direct proportion. You should use these units when specifying
    223 view dimensions in your layout, so the UI properly scales to render at the same actual size on
    224 different screens. (The compiler accepts both "dip" and "dp", though "dp" is more consistent with
    225 "sp".)</dd>
    226   <dt>{@code sp}</dt>
    227     <dd>Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font
    228     size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted
    229     for both the screen density and the user's preference.</dd>
    230   <dt>{@code pt}</dt>
    231     <dd>Points - 1/72 of an inch based on the physical size of the screen.</dd>
    232   <dt>{@code px}</dt>
    233     <dd>Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because
    234     the actual representation can vary across devices; each devices may have a different number of pixels
    235     per inch and may have more or fewer total pixels available on the screen.</dd>
    236   <dt>{@code mm}</dt>
    237     <dd>Millimeters - based on the physical size of the screen.</dd>
    238   <dt>{@code in}</dt>
    239     <dd>Inches - based on the physical size of the screen.</dd>
    240 </dl>
    241 
    242 <p class="note"><strong>Note:</strong> A dimension is a simple resource that is referenced
    243 using the value provided in the {@code name} attribute (not the name of the XML file). As
    244 such, you can combine dimension resources with other simple resources in the one XML file,
    245 under one {@code &lt;resources>} element.</p>
    246 
    247 <dl class="xml">
    248 
    249 <dt>file location:</dt>
    250 <dd><code>res/values/<em>filename</em>.xml</code><br/>
    251 The filename is arbitrary. The {@code &lt;dimen>} element's {@code name} will be used as the
    252 resource ID.</dd>
    253 
    254 <dt>resource reference:</dt>
    255 <dd>
    256 In Java: <code>R.dimen.<em>dimension_name</em></code><br/>
    257 In XML: <code>@[<em>package</em>:]dimen/<em>dimension_name</em></code>
    258 </dd>
    259 
    260 <dt>syntax:</dt>
    261 <dd>
    262 <pre class="stx">
    263 &lt;?xml version="1.0" encoding="utf-8"?>
    264 &lt;<a href="#dimen-resources-element">resources</a>>
    265     &lt;<a href="#dimen-element">dimen</a>
    266         name="<em>dimension_name</em>"
    267         &gt;<em>dimension</em>&lt;/dimen&gt;
    268 &lt;/resources&gt;
    269 </pre>
    270 </dd>
    271 
    272 <dt>elements:</dt>
    273 <dd>
    274 <dl class="tag-list">
    275 
    276   <dt id="dimen-resources-element"><code>&lt;resources&gt;</code></dt>
    277     <dd><strong>Required.</strong> This must be the root node.
    278       <p>No attributes.</p>
    279     </dd>
    280   <dt id="dimen-element"><code>&lt;dimen&gt;</code></dt>
    281     <dd>A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in),
    282       as described above.
    283       <p class="caps">attributes:</p>
    284       <dl class="atn-list">
    285         <dt><code>name</code></dt>
    286         <dd><em>String</em>. A name for the dimension. This will be used as the resource ID.
    287         </dd>
    288       </dl>
    289     </dd>
    290 
    291 </dl>
    292 </dd> <!-- end  elements and attributes -->
    293 
    294 <dt>example:</dt>
    295 <dd>XML file saved at <code>res/values/dimens.xml</code>:
    296 <pre>
    297 &lt;?xml version="1.0" encoding="utf-8"?>
    298 &lt;resources>
    299     &lt;dimen name="textview_height">25dp&lt;/dimen>
    300     &lt;dimen name="textview_width">150dp&lt;/dimen>
    301     &lt;dimen name="ball_radius">30dp&lt;/dimen>
    302     &lt;dimen name="font_size">16sp&lt;/dimen>
    303 &lt;/resources>
    304 </pre>
    305 
    306     <p>This application code retrieves a dimension:</p>
    307 <pre>
    308 Resources res = {@link android.content.Context#getResources()};
    309 float fontSize = res.{@link android.content.res.Resources#getDimension(int) getDimension}(R.dimen.font_size);
    310 </pre>
    311   <p>This layout XML applies dimensions to attributes:</p>
    312 <pre>
    313 &lt;TextView
    314     android:layout_height="@dimen/textview_height"
    315     android:layout_width="@dimen/textview_width"
    316     android:textSize="@dimen/font_size"/>
    317 </pre>
    318   </dl>
    319 </dd> <!-- end example -->
    320 
    321 </dl>
    322 
    323 
    324 
    325 
    326 
    327 
    328 <h2 id="Id">ID</h2>
    329 
    330 <p>A unique resource ID defined in XML. Using the name you provide in the {@code &lt;item&gt;}
    331 element, the Android developer tools create a unique integer in your project's {@code
    332 R.java} class, which you can use as an
    333 identifier for an application resources (for example, a {@link android.view.View} in your UI layout)
    334 or a unique integer for use in your application code (for example, as an ID for a dialog or a
    335 result code).</p>
    336 
    337 <p class="note"><strong>Note:</strong> An ID is a simple resource that is referenced
    338 using the value provided in the {@code name} attribute (not the name of the XML file). As
    339 such, you can combine ID resources with other simple resources in the one XML file,
    340 under one {@code &lt;resources&gt;} element. Also, remember that an ID resources does not reference
    341 an actual resource item; it is simply a unique ID that you can attach to other resources or use
    342 as a unique integer in your application.</p>
    343 
    344 <dl class="xml">
    345 
    346 <dt>file location:</dt>
    347 <dd><code>res/values/<em>filename.xml</em></code><br/>
    348 The filename is arbitrary.</dd>
    349 
    350 <dt>resource reference:</dt>
    351 <dd>
    352 In Java: <code>R.id.<em>name</em></code><br/>
    353 In XML: <code>@[<em>package</em>:]id/<em>name</em></code>
    354 </dd>
    355 
    356 <dt>syntax:</dt>
    357 <dd>
    358 <pre class="stx">
    359 &lt;?xml version="1.0" encoding="utf-8"?>
    360 &lt;<a href="#id-resources-element">resources</a>&gt;
    361     &lt;<a href="#id-item-element">item</a>
    362         type="id"
    363         name="<em>id_name</em>" /&gt;
    364 &lt;/resources&gt;
    365 </pre>
    366 </dd>
    367 
    368 <dt>elements:</dt>
    369 <dd>
    370 <dl class="tag-list">
    371 
    372   <dt id="id-resources-element"><code>&lt;resources&gt;</code></dt>
    373     <dd><strong>Required.</strong> This must be the root node.
    374       <p>No attributes.</p>
    375     </dd>
    376   <dt id="id-item-element"><code>&lt;item&gt;</code></dt>
    377     <dd>Defines a unique ID. Takes no value, only attributes.
    378       <p class="caps">attributes:</p>
    379       <dl class="atn-list">
    380         <dt><code>type</code></dt>
    381         <dd>Must be "id".</dd>
    382         <dt><code>name</code></dt>
    383         <dd><em>String</em>. A unique name for the ID.</dd>
    384       </dl>
    385     </dd>
    386 
    387 </dl>
    388 </dd> <!-- end  elements and attributes -->
    389 
    390 <dt>example:</dt>
    391 <dd>
    392   <p>XML file saved at <code>res/values/ids.xml</code>:</p>
    393 <pre>
    394 &lt;?xml version="1.0" encoding="utf-8"?>
    395 &lt;resources>
    396     &lt;item type="id" name="button_ok" /&gt;
    397     &lt;item type="id" name="dialog_exit" /&gt;
    398 &lt;/resources>
    399 </pre>
    400 
    401     <p>Then, this layout snippet uses the "button_ok" ID for a Button widget:</p>
    402 <pre>
    403 &lt;Button android:id="<b>@id/button_ok</b>"
    404     style="@style/button_style" /&gt;
    405 </pre>
    406 
    407     <p>Notice that the {@code android:id} value does not include the plus sign in the ID reference,
    408 because the ID already exists, as defined in the {@code ids.xml} example above. (When you specify an
    409 ID to an XML resource using the plus sign&mdash;in the format {@code
    410 android:id="@+id/name"}&mdash;it means that the "name" ID does not exist and should be created.)</p>
    411 
    412   <p>As another example, the following code snippet uses the "dialog_exit" ID as a unique identifier
    413 for a dialog:</p>
    414 <pre>
    415 {@link android.app.Activity#showDialog(int) showDialog}(<b>R.id.dialog_exit</b>);
    416 </pre>
    417   <p>In the same application, the "dialog_exit" ID is compared when creating a dialog:</p>
    418 <pre>
    419 protected Dialog {@link android.app.Activity#onCreateDialog(int)}(int id) {
    420     Dialog dialog;
    421     switch(id) {
    422     case <b>R.id.dialog_exit</b>:
    423         ...
    424         break;
    425     default:
    426         dialog = null;
    427     }
    428     return dialog;
    429 }
    430 </pre>
    431 </dd> <!-- end example -->
    432 
    433 
    434 </dl>
    435 
    436 
    437 
    438 
    439 
    440 <h2 id="Integer">Integer</h2>
    441 
    442 <p>An integer defined in XML.</p>
    443 
    444 <p class="note"><strong>Note:</strong> An integer is a simple resource that is referenced
    445 using the value provided in the {@code name} attribute (not the name of the XML file). As
    446 such, you can combine integer resources with other simple resources in the one XML file,
    447 under one {@code &lt;resources>} element.</p>
    448 
    449 <dl class="xml">
    450 
    451 <dt>file location:</dt>
    452 <dd><code>res/values/<em>filename.xml</em></code><br/>
    453 The filename is arbitrary. The {@code &lt;integer>} element's {@code name} will be used as the
    454 resource ID.</dd>
    455 
    456 <dt>resource reference:</dt>
    457 <dd>
    458 In Java: <code>R.integer.<em>integer_name</em></code><br/>
    459 In XML: <code>@[<em>package</em>:]integer/<em>integer_name</em></code>
    460 </dd>
    461 
    462 <dt>syntax:</dt>
    463 <dd>
    464 <pre class="stx">
    465 &lt;?xml version="1.0" encoding="utf-8"?>
    466 &lt;<a href="#integer-resources-element">resources</a>>
    467     &lt;<a href="#integer-element">integer</a>
    468         name="<em>integer_name</em>"
    469         &gt;<em>integer</em>&lt;/integer&gt;
    470 &lt;/resources&gt;
    471 </pre>
    472 </dd>
    473 
    474 <dt>elements:</dt>
    475 <dd>
    476 <dl class="tag-list">
    477 
    478   <dt id="integer-resources-element"><code>&lt;resources&gt;</code></dt>
    479     <dd><strong>Required.</strong> This must be the root node.
    480       <p>No attributes.</p>
    481     </dd>
    482   <dt id="integer-element"><code>&lt;integer&gt;</code></dt>
    483     <dd>An integer.
    484       <p class="caps">attributes:</p>
    485       <dl class="atn-list">
    486         <dt><code>name</code></dt>
    487         <dd><em>String</em>. A name for the integer. This will be used as the resource ID.
    488         </dd>
    489       </dl>
    490     </dd>
    491 
    492 </dl>
    493 </dd> <!-- end  elements and attributes -->
    494 
    495 <dt>example:</dt>
    496 <dd>
    497   <p>XML file saved at <code>res/values/integers.xml</code>:</p>
    498 <pre>
    499 &lt;?xml version="1.0" encoding="utf-8"?>
    500 &lt;resources>
    501     &lt;integer name="max_speed">75&lt;/integer>
    502     &lt;integer name="min_speed">5&lt;/integer>
    503 &lt;/resources>
    504 </pre>
    505     <p>This application code retrieves an integer:</p>
    506 <pre>
    507 Resources res = {@link android.content.Context#getResources()};
    508 int maxSpeed = res.{@link android.content.res.Resources#getInteger(int) getInteger}(R.integer.max_speed);
    509 </pre>
    510 </dd> <!-- end example -->
    511 
    512 
    513 </dl>
    514 
    515 
    516 
    517 
    518 
    519 <h2 id="IntegerArray">Integer Array</h2>
    520 
    521 <p>An array of integers defined in XML.</p>
    522 
    523 <p class="note"><strong>Note:</strong> An integer array is a simple resource that is referenced
    524 using the value provided in the {@code name} attribute (not the name of the XML file). As
    525 such, you can combine integer array resources with other simple resources in the one XML file,
    526 under one {@code &lt;resources>} element.</p>
    527 
    528 
    529 <dl class="xml">
    530 
    531 <dt>file location:</dt>
    532 <dd><code>res/values/<em>filename</em>.xml</code><br/>
    533 The filename is arbitrary. The {@code &lt;integer-array>} element's {@code name} will be used as the
    534 resource ID.</dd>
    535 
    536 <dt>compiled resource datatype:</dt>
    537 <dd>Resource pointer to an array of integers.</dd>
    538 
    539 <dt>resource reference:</dt>
    540 <dd>
    541 In Java: <code>R.array.<em>string_array_name</em></code><br/>
    542 In XML: <code>@[<em>package</em>:]array.<em>integer_array_name</em></code>
    543 </dd>
    544 
    545 <dt>syntax:</dt>
    546 <dd>
    547 <pre class="stx">
    548 &lt;?xml version="1.0" encoding="utf-8"?>
    549 &lt;<a href="#integer-array-resources-element">resources</a>>
    550     &lt;<a href="#integer-array-element">integer-array</a>
    551         name="<em>integer_array_name</em>">
    552         &lt;<a href="#integer-array-item-element">item</a>
    553             &gt;<em>integer</em>&lt;/item&gt;
    554     &lt;/integer-array>
    555 &lt;/resources>
    556 </pre>
    557 </dd>
    558 
    559 <dt>elements:</dt>
    560 <dd>
    561 <dl class="tag-list">
    562   <dt id="integer-array-resources-element"><code>&lt;resources&gt;</code></dt>
    563     <dd><strong>Required.</strong> This must be the root node.
    564       <p>No attributes.</p>
    565     </dd>
    566   <dt id="integer-array-element"><code>&lt;string-array&gt;</code></dt>
    567     <dd>Defines an array of integers. Contains one or more child {@code &lt;item>} elements.
    568       <p class="caps">attributes:</p>
    569       <dl class="atn-list">
    570         <dt><code>android:name</code></dt>
    571         <dd><em>String</em>. A name for the array. This name will be used as the resource
    572 ID to reference the array.</dd>
    573       </dl>
    574     </dd>
    575   <dt id="integer-array-item-element"><code>&lt;item&gt;</code></dt>
    576     <dd>An integer. The value can be a referenced to another
    577 integer resource. Must be a child of a {@code &lt;integer-array&gt;} element.
    578       <p>No attributes.</p>
    579     </dd>
    580 </dl>
    581 </dd> <!-- end  elements -->
    582 
    583 <dt>example:</dt>
    584 <dd>XML file saved at <code>res/values/integers.xml</code>:
    585 <pre>
    586 &lt;?xml version="1.0" encoding="utf-8"?>
    587 &lt;resources>
    588     &lt;integer-array name="bits">
    589         &lt;item>4&lt;/item>
    590         &lt;item>8&lt;/item>
    591         &lt;item>16&lt;/item>
    592         &lt;item>32&lt;/item>
    593     &lt;/integer-array>
    594 &lt;/resources>
    595 </pre>
    596 
    597   <p>This application code retrieves the integer array:</p>
    598 <pre>
    599 Resources res = {@link android.content.Context#getResources()};
    600 int[] bits = res.{@link android.content.res.Resources#getIntArray(int) getIntArray}(R.array.bits);
    601 </pre>
    602 </dd> <!-- end example -->
    603 
    604 </dl>
    605 
    606 
    607 
    608 
    609 
    610 <h2 id="TypedArray">Typed Array</h2>
    611 
    612 <p>A {@link android.content.res.TypedArray} defined in XML. You can use
    613 this to create an array of other resources, such as drawables. Note that the array
    614 is not required to be homogeneous, so you can create an array of mixed resource types, but
    615 you must be aware of what and where the data types are in the array so that you can properly obtain
    616 each item with the {@link android.content.res.TypedArray}'s {@code get...()} methods.</p>
    617 
    618 <p class="note"><strong>Note:</strong> A typed array is a simple resource that is referenced
    619 using the value provided in the {@code name} attribute (not the name of the XML file). As
    620 such, you can combine typed array resources with other simple resources in the one XML file,
    621 under one {@code &lt;resources&gt;} element.</p>
    622 
    623 
    624 <dl class="xml">
    625 
    626 <dt>file location:</dt>
    627 <dd><code>res/values/<em>filename</em>.xml</code><br/>
    628 The filename is arbitrary. The {@code &lt;array>} element's {@code name} will be used as the
    629 resource ID.</dd>
    630 
    631 <dt>compiled resource datatype:</dt>
    632 <dd>Resource pointer to a {@link android.content.res.TypedArray}.</dd>
    633 
    634 <dt>resource reference:</dt>
    635 <dd>
    636 In Java: <code>R.array.<em>array_name</em></code><br/>
    637 In XML: <code>@[<em>package</em>:]array.<em>array_name</em></code>
    638 </dd>
    639 
    640 <dt>syntax:</dt>
    641 <dd>
    642 <pre class="stx">
    643 &lt;?xml version="1.0" encoding="utf-8"?>
    644 &lt;<a href="#array-resources-element">resources</a>>
    645     &lt;<a href="#array-element">array</a>
    646         name="<em>integer_array_name</em>">
    647         &lt;<a href="#array-item-element">item</a>&gt;<em>resource</em>&lt;/item&gt;
    648     &lt;/array>
    649 &lt;/resources>
    650 </pre>
    651 </dd>
    652 
    653 <dt>elements:</dt>
    654 <dd>
    655 <dl class="tag-list">
    656   <dt id="array-resources-element"><code>&lt;resources&gt;</code></dt>
    657     <dd><strong>Required.</strong> This must be the root node.
    658       <p>No attributes.</p>
    659     </dd>
    660   <dt id="array-element"><code>&lt;array&gt;</code></dt>
    661     <dd>Defines an array. Contains one or more child {@code &lt;item>} elements.
    662       <p class="caps">attributes:</p>
    663       <dl class="atn-list">
    664         <dt><code>android:name</code></dt>
    665         <dd><em>String</em>. A name for the array. This name will be used as the resource
    666 ID to reference the array.</dd>
    667       </dl>
    668     </dd>
    669   <dt id="array-item-element"><code>&lt;item&gt;</code></dt>
    670     <dd>A generic resource. The value can be a reference to a resource or a simple data type.
    671 Must be a child of an {@code &lt;array&gt;} element.
    672       <p>No attributes.</p>
    673     </dd>
    674 </dl>
    675 </dd> <!-- end  elements -->
    676 
    677 <dt>example:</dt>
    678 <dd>XML file saved at <code>res/values/arrays.xml</code>:
    679 <pre>
    680 &lt;?xml version="1.0" encoding="utf-8"?>
    681 &lt;resources>
    682     &lt;array name="icons">
    683         &lt;item>@drawable/home&lt;/item>
    684         &lt;item>@drawable/settings&lt;/item>
    685         &lt;item>@drawable/logout&lt;/item>
    686     &lt;/array>
    687     &lt;array name="colors">
    688         &lt;item>#FFFF0000&lt;/item>
    689         &lt;item>#FF00FF00&lt;/item>
    690         &lt;item>#FF0000FF&lt;/item>
    691     &lt;/array>
    692 &lt;/resources>
    693 </pre>
    694 
    695   <p>This application code retrieves each array and then obtains the first entry in each array:</p>
    696 <pre>
    697 Resources res = {@link android.content.Context#getResources()};
    698 TypedArray icons = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.icons);
    699 Drawable drawable = icons.{@link android.content.res.TypedArray#getDrawable(int) getDrawable}(0);
    700 
    701 TypedArray colors = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.colors);
    702 int color = colors.{@link android.content.res.TypedArray#getColor(int,int) getColor}(0,0);
    703 </pre>
    704 </dd> <!-- end example -->
    705 
    706 </dl>
    707 
    708 
    709 
    710 
    711 
    712 
    713 
    714 
    715 
    716 
    717 <!-- TODO
    718 
    719 
    720 <h2>Styleable Attribute</h2>
    721 
    722 
    723 <dl class="xml">
    724 
    725 <dt>syntax:</dt>
    726 <dd>
    727 <pre class="stx">
    728 </pre>
    729 </dd>
    730 
    731 <dt>file location:</dt>
    732 <dd><code>res/</code></dd>
    733 
    734 <dt>compiled resource datatype:</dt>
    735 <dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd>
    736 
    737 <dt>resource reference:</dt>
    738 <dd>Java: <code>R.</code><br/>
    739     XML:
    740 </dd>
    741 
    742 <dt>elements and attributes:</dt>
    743 <dd>
    744 <dl class="attr">
    745 
    746   <dt><code></code></dt>
    747     <dd></dd>
    748   <dt><code></code></dt>
    749     <dd>Valid attributes:
    750       <dl>
    751         <dt><code></code></dt>
    752         <dd>
    753         </dd>
    754         <dt><code></code></dt>
    755         <dd>
    756         </dd>
    757       </dl>
    758     </dd>
    759 
    760 </dl>
    761 </dd> 
    762 
    763 <dt>example:</dt>
    764 <dd>
    765   <dl>
    766 
    767     <dt>XML file saved at <code>res/</code>:</dt>
    768     <dd>
    769 <pre>
    770 
    771 </pre>
    772     </dd>
    773 
    774     <dt>Java code :</dt>
    775     <dd>
    776 <pre>
    777 
    778 </pre>
    779     </dd>
    780 
    781   </dl>
    782 </dd> 
    783 
    784 
    785 <dt>see also:</dt>
    786 <dd>
    787 <ul>
    788   <li></li>
    789 </ul>
    790 </dd>
    791 
    792 </dl>
    793 
    794 
    795 
    796 
    797 
    798 
    799 -->
    800 
    801 
    802 
    803 
    804