providing-resources.jd revision 7ef674b596ba1bac6b26350a6c478d5e6a16bfd7
1page.title=Providing Resources
2parent.title=Application Resources
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8  <h2>Quickview</h2>
9  <ul>
10    <li>Different types of resources belong in different subdirectories of {@code res/}</li>
11    <li>Alternative resources provide configuration-specific resource files</li>
12    <li>Always include default resources so your app does not depend on specific configurations</li>
13  </ul>
14  <h2>In this document</h2>
15  <ol>
16    <li><a href="#ResourceTypes">Grouping Resource Types</a></li>
17    <li><a href="#AlternativeResources">Providing Alternative Resources</a>
18      <ol>
19        <li><a href="#QualifierRules">Qualifier name rules</a></li>
20        <li><a href="#AliasResources">Creating alias resources</a></li>
21      </ol>
22    </li>
23    <li><a href="#Compatibility">Providing the Best Device Compatibility with Resources</a></li>
24    <li><a href="#BestMatch">How Android Finds the Best-matching Resource</a></li>
25    <li><a href="#KnownIssues">Known Issues</a></li>
26  </ol>
27
28  <h2>See also</h2>
29  <ol>
30    <li><a href="accessing-resources.html">Accessing Resources</a></li>
31    <li><a href="available-resources.html">Resource Types</a></li>
32    <li><a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
33Screens</a></li>
34  </ol>
35</div>
36</div>
37
38<p>You should always externalize application resources such as images and strings from your
39code, so that you can maintain them independently. You can also provide alternative resources for
40specific device configurations, by grouping them in specially-named resource directories. Android
41then applies the appropriate resource based on the current configuration. For
42instance, you might want to provide a different UI layout depending on the screen size.</p>
43
44<p>Once you externalize your application resources, you can access them
45using resource IDs that are generated in your project's {@code R} class. How to use
46resources in your application is discussed in <a href="accessing-resources.html">Accessing
47Resources</a>. This document shows you how to group your resources in your Android project and
48provide alternative resources for specific device configurations.</p>
49
50
51<h2 id="ResourceTypes">Grouping Resource Types</h2>
52
53<p>You should place each type of resource in a specific subdirectory of your project's
54{@code res/} directory. For example, here's the file hierarchy for a simple project:</p>
55
56<pre class="classic no-pretty-print">
57MyProject/
58    src/  <span style="color:black">
59        MyActivity.java  </span>
60    res/
61        drawable/  <span style="color:black">
62            icon.png  </span>
63        layout/  <span style="color:black">
64            main.xml
65            info.xml</span>
66        values/  <span style="color:black">
67            strings.xml  </span>
68</pre>
69
70<p>As you can see in this example, the {@code res/} directory contains all the resources (in
71subdirectories): an image resource, two layout resources, and a string resource file. The resource
72directory names are important and are described in table 1.</p>
73
74<p class="table-caption" id="table1"><strong>Table 1.</strong> Resource directories
75supported inside project {@code res/} directory.</p>
76
77<table>
78  <tr>
79    <th scope="col">Directory</th>
80    <th scope="col">Resource Type</th>
81  </tr>
82
83  <tr>
84    <td><code>anim/</code></td>
85    <td>XML files that define tween animations. See <a
86href="animation-resource.html">Animation Resources</a>.</td>
87  </tr>
88
89  <tr>
90    <td><code>color/</code></td>
91    <td>XML files that define a state list of colors. See <a href="color-list-resource.html">Color
92State List Resource</a></td>
93  </tr>
94
95  <tr>
96    <td><code>drawable/</code></td>
97    <td><p>Bitmap files ({@code .png}, {@code .9.png}, {@code .jpg}, {@code .gif}) or XML files that
98are compiled into the following drawable resource subtypes:</p>
99      <ul>
100        <li>Bitmap files</li>
101        <li>Nine-Patches (re-sizable bitmaps)</li>
102        <li>State lists</li>
103        <li>Shapes</li>
104        <li>Animation drawables</li>
105        <li>Other drawables</li>
106      </ul>
107      <p>See <a href="drawable-resource.html">Drawable Resources</a>.</p>
108    </td>
109  </tr>
110
111  <tr>
112    <td><code>layout/</code></td>
113    <td>XML files that define a user interface layout.
114        See <a href="layout-resource.html">Layout Resource</a>.</td>
115  </tr>
116
117  <tr>
118    <td><code>menu/</code></td>
119    <td>XML files that define application menus, such as an Options Menu, Context Menu, or Sub
120Menu. See <a href="menu-resource.html">Menu Resource</a>.</td>
121  </tr>
122
123  <tr>
124    <td><code>raw/</code></td>
125    <td><p>Arbitrary files to save in their raw form. Files in here are not compressed by the
126system. To open these resources with a raw {@link java.io.InputStream}, call {@link
127android.content.res.Resources#openRawResource(int)
128Resources.openRawResource()} with the resource ID, which is {@code R.raw.<em>filename</em>}.</p>
129      <p>However, if you need access to original file names and file hierarchy, you might consider
130saving some resources in the {@code
131assets/} directory (instead of {@code res/raw/}). Files in {@code assets/} are not given a
132resource ID, so you can read them only using {@link android.content.res.AssetManager}.</p></td>
133  </tr>
134
135  <tr>
136    <td><code>values/</code></td>
137    <td><p>XML files that contain simple values, such as strings, integers, and colors.</p>
138      <p>Whereas XML resource files in other {@code res/} subdirectories define a single resource
139based on the XML filename, files in the {@code values/} directory describe multiple resources.
140For a file in this directory, each child of the {@code &lt;resources&gt;} element defines a single
141resource. For example, a {@code &lt;string&gt;} element creates an
142{@code R.string} resource and a  {@code &lt;color&gt;} element creates an {@code R.color}
143resource.</p>
144      <p>Because each resource is defined with its own XML element, you can name the file
145whatever you want and place different resource types in one file. However, for clarity, you might
146want to place unique resource types in different files. For example, here are some filename
147conventions for resources you can create in this directory:</p>
148      <ul>
149        <li>arrays.xml for resource arrays (<a
150href="more-resources.html#TypedArray">typed arrays</a>).</li>
151        <li>colors.xml for <a
152href="more-resources.html#Color">color values</a></li>
153        <li>dimens.xml for <a
154href="more-resources.html#Dimension">dimension values</a>.</li>
155        <li>strings.xml for <a href="string-resource.html">string
156values</a>.</li>
157        <li>styles.xml for <a href="style-resource.html">styles</a>.</li>
158      </ul>
159      <p>See <a href="string-resource.html">String Resources</a>,
160        <a href="style-resource.html">Style Resource</a>, and
161        <a href="more-resources.html">More Resource Types</a>.</p>
162    </td>
163  </tr>
164
165  <tr>
166    <td><code>xml/</code></td>
167    <td>Arbitrary XML files that can be read at runtime by calling {@link
168android.content.res.Resources#getXml(int) Resources.getXML()}. Various XML configuration files
169must be saved here, such as a <a
170href="{@docRoot}guide/topics/search/searchable-config.html">searchable configuration</a>.
171<!-- or preferences configuration. --></td>
172  </tr>
173</table>
174
175<p class="caution"><strong>Caution:</strong> Never save resource files directly inside the
176{@code res/} directory&mdash;it will cause a compiler error.</p>
177
178<p>For more information about certain types of resources, see the <a
179href="available-resources.html">Resource Types</a> documentation.</p>
180
181<p>The resources that you save in the subdirectories defined in table 1 are your "default"
182resources. That is, these resources define the default design and content for your application.
183However, different types of Android-powered devices might call for different types of resources.
184For example, if a device has a larger than normal screen, then you should provide
185different layout resources that take advantage of the extra screen space. Or, if a device has a
186different language setting, then you should provide different string resources that translate the
187text in your user interface. To provide these different resources for different device
188configurations, you need to provide "alternative" resources, in addition to your default
189resources.</p>
190
191
192<h2 id="AlternativeResources">Providing Alternative Resources</h2>
193
194
195<div class="figure" style="width:421px">
196<img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" />
197<p class="img-caption">
198<strong>Figure 1.</strong> Two device configurations, one using alternative resources.</p>
199</div>
200
201<p>Almost every application should provide alternative resources to support specific device
202configurations. For instance, you should include alternative drawable resources for different
203screen densities and alternative string resources for different languages. At runtime, Android
204detects the current device configuration and loads the appropriate
205resources for your application.</p>
206
207<p>To specify configuration-specific alternatives for a set of resources:</p>
208<ol>
209  <li>Create a new directory in {@code res/} named in the form {@code
210<em>&lt;resources_name&gt;</em>-<em>&lt;config_qualifier&gt;</em>}.
211    <ul>
212      <li><em>{@code &lt;resources_name&gt;}</em> is the directory name of the corresponding default
213resources (defined in table 1).</li>
214      <li><em>{@code &lt;config_qualifier&gt;}</em> is a name that specifies a configuration
215for which these resources are to be used (defined in table 2).</li>
216    </ul>
217    <p>You can append more than one <em>{@code &lt;config_qualifier&gt;}</em>. Separate each
218one with a dash.</p>
219  </li>
220  <li>Save the respective alternative resources in this new directory. The resource files must be
221named exactly the same as the default resource files.</li>
222</ol>
223
224<p>For example, here are some default and alternative resources:</p>
225
226<pre class="classic no-pretty-print">
227res/
228    drawable/   <span style="color:black">
229        icon.png
230        background.png    </span>
231    drawable-hdpi/  <span style="color:black">
232        icon.png
233        background.png  </span>
234</pre>
235
236<p>The {@code hdpi} qualifier indicates that the resources in that directory are for devices with a
237high-density screen. The images in each of these drawable directories are sized for a specific
238screen density, but the filenames are exactly
239the same. This way, the resource ID that you use to reference the {@code icon.png} or {@code
240background.png} image is always the same, but Android selects the
241version of each resource that best matches the current device, by comparing the device
242configuration information with the qualifiers in the alternative resource directory name.</p>
243
244<p>Android supports several configuration qualifiers and you can
245add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2
246lists the valid configuration qualifiers, in order of precedence&mdash;if you use multiple
247qualifiers for one resource directory, they must be added to the directory name in the order they
248are listed in the table.</p>
249
250<p class="note"><strong>Note:</strong> Some resource qualifiers were added after Android 1.0, so not
251all versions of Android support all the qualifiers listed in table 2. New qualifiers
252indicate the version in which they were added. To avoid any issues, always include a set of default
253resources for resources that your application uses. For more information, see the section about <a
254href="#Compatibility">Providing the Best Device Compatibility with Resources</a>.</p>
255
256<p class="table-caption" id="table2"><strong>Table 2.</strong> Alternative resource qualifier
257names.</p>
258<table>
259    <tr>
260        <th>Qualifier</th>
261        <th>Values</th>
262        <th>Description</th>
263    </tr>
264    <tr id="MccQualifier">
265      <td>MCC and MNC</td>
266      <td>Examples:<br/>
267        <code>mcc310</code><br/>
268        <code><nobr>mcc310-mnc004</nobr></code><br/>
269        <code>mcc208-mnc00</code><br/>
270        etc.
271      </td>
272      <td>
273        <p>The mobile country code (MCC), optionally followed by mobile network code (MNC)
274        from the SIM card in the device. For example, <code>mcc310</code> is U.S. on any carrier,
275        <code>mcc310-mnc004</code> is U.S. on Verizon, and <code>mcc208-mnc00</code> is France on
276        Orange.</p>
277        <p>If the device uses a radio connection (GSM phone), the MCC comes
278        from the SIM, and the MNC comes from the network to which the
279        device is connected.</p>
280        <p>You can also use the MCC alone (for example, to include country-specific legal
281resources in your application). If you need to specify based on the language only, then use the
282<em>language and region</em> qualifier instead (discussed next). If you decide to use the MCC and
283MNC qualifier, you should do so with care and test that it works as expected.</p>
284        <p>Also see the configuration fields {@link
285android.content.res.Configuration#mcc}, and {@link
286android.content.res.Configuration#mnc}, which indicate the current mobile country code
287and mobile network code, respectively.</p>
288      </td>
289    </tr>
290    <tr id="LocaleQualifier">
291      <td>Language and region</td>
292      <td>Examples:<br/>
293        <code>en</code><br/>
294        <code>fr</code><br/>
295        <code>en-rUS</code><br/>
296        <code>fr-rFR</code><br/>
297        <code>fr-rCA</code><br/>
298        etc.
299      </td>
300      <td><p>The language is defined by a two-letter <a
301href="http://www.loc.gov/standards/iso639-2/php/code_list.php">ISO
302              639-1</a> language code, optionally followed by a two letter
303              <a
304href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO
305              3166-1-alpha-2</a> region code (preceded by lowercase &quot;{@code r}&quot;).
306        </p><p>
307        The codes are <em>not</em> case-sensitive; the {@code r} prefix is used to
308        distinguish the region portion.
309        You cannot specify a region alone.</p>
310        <p>This can change during the life
311of your application if the user changes his or her language in the system settings. See <a
312href="runtime-changes.html">Handling Runtime Changes</a> for information about
313how this can affect your application during runtime.</p>
314        <p>See <a href="localization.html">Localization</a> for a complete guide to localizing
315your application for other languages.</p>
316        <p>Also see the {@link android.content.res.Configuration#locale} configuration field, which
317indicates the current locale.</p>
318      </td>
319    </tr>
320    <tr id="ScreenSizeQualifier">
321      <td>Screen size</td>
322      <td>
323        <code>small</code><br/>
324        <code>normal</code><br/>
325        <code>large</code>
326      </td>
327      <td>
328        <ul class="nolist">
329        <li>{@code small}: Screens based on the space available on a
330        low-density QVGA screen.  Considering a portrait HVGA display, this has
331        the same available width but less height&mdash;it is 3:4 vs. HVGA's
332        2:3 aspect ratio.  Examples are QVGA low density and VGA high
333        density.</li>
334        <li>{@code normal}: Screens based on the traditional
335        medium-density HVGA screen.  A screen is considered to be normal if it is
336        at least this size (independent of density) and not larger.  Examples
337        of such screens a WQVGA low density, HVGA medium density, WVGA
338        high density.</li>
339        <li>{@code large}: Screens based on the space available on a
340        medium-density VGA screen.  Such a screen has significantly more
341        available space in both width and height than an HVGA display.
342        Examples are VGA and WVGA medium density screens.</li>
343        </ul>
344        <p><em>Added in API Level 4.</em></p>
345        <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
346Screens</a> for more information.</p>
347        <p>Also see the {@link android.content.res.Configuration#screenLayout} configuration field,
348which indicates whether the screen is small, normal,
349or large.</p>
350      </td>
351    </tr>
352    <tr id="ScreenLongQualifier">
353      <td>Wider/taller screens</td>
354      <td>
355        <code>long</code><br/>
356        <code>notlong</code>
357      </td>
358      <td>
359        <ul class="nolist">
360          <li>{@code long}: Long screens, such as WQVGA, WVGA, FWVGA</li>
361          <li>{@code notlong}: Not long screens, such as QVGA, HVGA, and VGA</li>
362        </ul>
363        <p><em>Added in API Level 4.</em></p>
364        <p>This is based purely on the aspect ratio of the screen (a "long" screen is wider). This
365is not related to the screen orientation.</p>
366        <p>Also see the {@link android.content.res.Configuration#screenLayout} configuration field,
367which indicates whether the screen is long.</p>
368      </td>
369    </tr>
370    <tr id="OrientationQualifier">
371      <td>Screen orientation</td>
372      <td>
373        <code>port</code><br/>
374        <code>land</code>  <!-- <br/>
375        <code>square</code>  -->
376      </td>
377      <td>
378        <ul class="nolist">
379          <li>{@code port}: Device is in portrait orientation (vertical)</li>
380          <li>{@code land}: Device is in landscape orientation (horizontal)</li>
381          <!-- Square mode is currently not used. -->
382        </ul>
383        <p>This can change during the life of your application if the user rotates the
384screen. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about
385how this affects your application during runtime.</p>
386        <p>Also see the {@link android.content.res.Configuration#orientation} configuration field,
387which indicates the current device orientation.</p>
388      </td>
389    </tr>
390    <tr id="DockQualifier">
391      <td>Dock mode</td>
392      <td>
393        <code>car</code><br/>
394        <code>desk</code>
395      </td>
396      <td>
397        <ul class="nolist">
398          <li>{@code car}: Device is in a car dock</li>
399          <li>{@code desk}: Device is in a desk dock</li>
400        </ul>
401        <p><em>Added in API Level 8.</em></p>
402        <p>This can change during the life of your application if the user places the device in a
403dock. You can enable or disable this mode using {@link
404android.app.UiModeManager}. See <a href="runtime-changes.html">Handling Runtime Changes</a> for
405information about how this affects your application during runtime.</p>
406      </td>
407    </tr>
408    <tr id="NightQualifier">
409      <td>Night mode</td>
410      <td>
411        <code>night</code><br/>
412        <code>notnight</code>
413      </td>
414      <td>
415        <ul class="nolist">
416          <li>{@code night}: Night time</li>
417          <li>{@code notnight}: Day time</li>
418        </ul>
419        <p><em>Added in API Level 8.</em></p>
420        <p>This can change during the life of your application if night mode is left in
421auto mode (default), in which case the mode changes based on the time of day.  You can enable
422or disable this mode using {@link android.app.UiModeManager}. See <a
423href="runtime-changes.html">Handling Runtime Changes</a> for information about how this affects your
424application during runtime.</p>
425      </td>
426    </tr>
427    <tr id="DensityQualifier">
428      <td>Screen pixel density (dpi)</td>
429      <td>
430        <code>ldpi</code><br/>
431        <code>mdpi</code><br/>
432        <code>hdpi</code><br/>
433        <code>nodpi</code>
434      </td>
435      <td>
436        <ul class="nolist">
437          <li>{@code ldpi}: Low-density screens; approximately 120dpi.</li>
438          <li>{@code mdpi}: Medium-density (on traditional HVGA) screens; approximately
439160dpi.</li>
440          <li>{@code hdpi}: High-density screens; approximately 240dpi.</li>
441          <li>{@code nodpi}: This can be used for bitmap resources that you do not want to be scaled
442to match the device density.</li>
443        </ul>
444        <p><em>Added in API Level 4.</em></p>
445        <p>There is thus a 4:3 scaling factor between each density, so a 9x9 bitmap
446         in ldpi is 12x12 in mdpi and 16x16 in hdpi.</p>
447        <p>When Android selects which resource files to use,
448         it handles screen density differently than the other qualifiers.
449         In step 1 of <a href="#BestMatch">How Android finds the best
450         matching directory</a> (below), screen density is always considered to
451         be a match. In step 4, if the qualifier being considered is screen
452         density, Android selects the best final match at that point,
453         without any need to move on to step 5.
454         </p>
455        <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
456Screens</a> for more information about how to handle screen sizes and how Android might scale
457your bitmaps.</p>
458       </td>
459    </tr>
460    <tr id="TouchscreenQualifier">
461      <td>Touchscreen type</td>
462      <td>
463        <code>notouch</code><br/>
464        <code>stylus</code><br/>
465        <code>finger</code>
466      </td>
467      <td>
468        <ul class="nolist">
469          <li>{@code notouch}: Device does not have a touchscreen.</li>
470          <li>{@code stylus}: Device has a resistive touchscreen that's suited for use with a
471stylus.</li>
472          <li>{@code finger}: Device has a touchscreen.</li>
473        </ul>
474        <p>Also see the {@link android.content.res.Configuration#touchscreen} configuration field,
475which indicates the type of touchscreen on the device.</p>
476      </td>
477    </tr>
478    <tr id="KeyboardAvailQualifier">
479      <td>Keyboard availability</td>
480      <td>
481        <code>keysexposed</code><br/>
482        <code>keyssoft</code>
483      </td>
484      <td>
485        <ul class="nolist">
486          <li>{@code keysexposed}: Device has a keyboard available. If the device has a
487software keyboard enabled (which is likely), this may be used even when the hardware keyboard is
488<em>not</em> exposed to the user, even if the device has no hardware keyboard. If no software
489keyboard is provided or it's disabled, then this is only used when a hardware keyboard is
490exposed.</li>
491          <li>{@code keyshidden}: Device has a hardware keyboard available but it is
492hidden <em>and</em> the device does <em>not</em> have a software keyboard enabled.</li>
493          <li>{@code keyssoft}: Device has a software keyboard enabled, whether it's
494visible or not.</li>
495        </ul>
496        <p>If you provide <code>keysexposed</code> resources, but not <code>keyssoft</code>
497resources, the system uses the <code>keysexposed</code> resources regardless of whether a
498keyboard is visible, as long as the system has a software keyboard enabled.</p>
499        <p>This can change during the life of your application if the user opens a hardware
500keyboard. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about how
501this affects your application during runtime.</p>
502        <p>Also see the configuration fields {@link
503android.content.res.Configuration#hardKeyboardHidden} and {@link
504android.content.res.Configuration#keyboardHidden}, which indicate the visibility of a hardware
505keyboard and and the visibility of any kind of keyboard (including software), respectively.</p>
506      </td>
507    </tr>
508    <tr id="ImeQualifier">
509      <td>Primary text input method</td>
510      <td>
511        <code>nokeys</code><br/>
512        <code>qwerty</code><br/>
513        <code>12key</code>
514      </td>
515      <td>
516        <ul class="nolist">
517          <li>{@code nokeys}: Device has no hardware keys for text input.</li>
518          <li>{@code qwerty}: Device has a hardware qwerty keyboard, whether it's visible to the
519user
520or not.</li>
521          <li>{@code 12key}: Device has a hardware 12-key keyboard, whether it's visible to the user
522or not.</li>
523        </ul>
524        <p>Also see the {@link android.content.res.Configuration#keyboard} configuration field,
525which indicates the primary text input method available.</p>
526      </td>
527    </tr>
528    <tr id="NavAvailQualifier">
529      <td>Navigation key availability</td>
530      <td>
531        <code>navexposed</code><br/>
532        <code>navhidden</code>
533      </td>
534      <td>
535        <ul class="nolist">
536          <li>{@code navexposed}: Navigation keys are available to the user.</li>
537          <li>{@code navhidden}: Navigation keys are not available (such as behind a closed
538lid).</li>
539        </ul>
540        <p>This can change during the life of your application if the user reveals the navigation
541keys. See <a href="runtime-changes.html">Handling Runtime Changes</a> for
542information about how this affects your application during runtime.</p>
543        <p>Also see the {@link android.content.res.Configuration#navigationHidden} configuration
544field, which indicates whether navigation keys are hidden.</p>
545      </td>
546    </tr>
547    <tr id="TouchQualifier">
548      <td>Primary non-touch navigation method</td>
549      <td>
550        <code>nonav</code><br/>
551        <code>dpad</code><br/>
552        <code>trackball</code><br/>
553        <code>wheel</code>
554      </td>
555      <td>
556        <ul class="nolist">
557          <li>{@code nonav}: Device has no navigation facility other than using the
558touchscreen.</li>
559          <li>{@code dpad}: Device has a directional-pad (d-pad) for navigation.</li>
560          <li>{@code trackball}: Device has a trackball for navigation.</li>
561          <li>{@code wheel}: Device has a directional wheel(s) for navigation (uncommon).</li>
562        </ul>
563        <p>Also see the {@link android.content.res.Configuration#navigation} configuration field,
564which indicates the type of navigation method available.</p>
565      </td>
566    </tr>
567<!-- DEPRECATED
568    <tr>
569      <td>Screen dimensions</td>
570      <td>Examples:<br/>
571        <code>320x240</code><br/>
572        <code>640x480</code><br/>
573        etc.
574      </td>
575      <td>
576        <p>The larger dimension must be specified first. <strong>This configuration is deprecated
577and should not be used</strong>. Instead use "screen size," "wider/taller screens," and "screen
578orientation" described above.</p>
579      </td>
580    </tr>
581-->
582    <tr id="VersionQualifier">
583      <td>System Version (API Level)</td>
584      <td>Examples:<br/>
585        <code>v3</code><br/>
586        <code>v4</code><br/>
587        <code>v7</code><br/>
588        etc.</td>
589      <td>
590        <p>The API Level supported by the device. For example, <code>v1</code> for API Level
5911 (devices with Android 1.0 or higher) and <code>v4</code> for API Level 4 (devices with Android
5921.6 or higher). See the <a
593href="{@docRoot}guide/appendix/api-levels.html">Android API Levels</a> document for more information
594about these values.</p>
595        <p class="caution"><strong>Caution:</strong> Android 1.5 and 1.6 only match resources
596with this qualifier when it exactly matches the system version. See the section below about <a
597href="#KnownIssues">Known Issues</a> for more information.</p>
598      </td>
599    </tr>
600</table>
601
602
603<h3 id="QualifierRules">Qualifier name rules</h3>
604
605<p>Here are some rules about using resource qualifier names:</p>
606
607<ul>
608    <li>You can specify multiple qualifiers for a single set of resources, separated by dashes. For
609example, <code>drawable-en-rUS-land</code> applies to US-English devices in landscape
610orientation.</li>
611    <li>The qualifiers must be in the order listed in <a href="#table2">table 2</a>. For
612example:
613      <ul>
614        <li>Wrong: <code>drawable-hdpi-port/</code></li>
615        <li>Correct: <code>drawable-port-hdpi/</code></li>
616      </ul>
617    </li>
618    <li>Alternative resource directories cannot be nested. For example, you cannot have
619<code>res/drawable/drawable-en/</code>.</li>
620    <li>Values are case-insensitive.  The resource compiler converts directory names
621    to lower case before processing to avoid problems on case-insensitive
622    file systems. Any capitalization in the names is only to benefit readability.</li>
623    <li>Only one value for each qualifier type is supported. For example, if you want to use
624the same drawable files for Spain and France, you <em>cannot</em> have a directory named
625<code>drawable-rES-rFR/</code>. Instead you need two resource directories, such as
626<code>drawable-rES/</code> and <code>drawable-rFR/</code>, which contain the appropriate files.
627However, you are not required to actually duplicate the same files in both locations. Instead, you
628can create an alias to a resource. See <a href="#AliasResources">Creating
629alias resources</a> below.</li>
630</ul>
631
632<p>After you save alternative resources into directories named with
633these qualifiers, Android automatically applies the resources in your application based on the
634current device configuration. Each time a resource is requested, Android checks for alternative
635resource directories that contain the requested resource file, then <a href="#BestMatch">finds the
636best-matching resource</a> (discussed below).</p>
637
638
639
640<h3 id="AliasResources">Creating alias resources</h3>
641
642<p>When you have a resource that you'd like to use for more than one device
643configuration (but not for all configurations), you do not need to put the same resource in
644each alternative resource directory. Instead, you can (in some cases) create an alternative
645resource that acts as an alias for a resource saved in your default resource directory.</p>
646
647<p class="note"><strong>Note:</strong> Not all resources offer a mechanism by which you can
648create an alias to another resource. In particular, animation, menu, raw, and other unspecified
649resources in the {@code xml/} directory do not offer this feature.</p>
650
651<p>For example, imagine you have an application icon, {@code icon.png}, and need unique version of
652it for different locales. However, two locales, English-Canadian and French-Canadian, need to
653use the same version. You might assume that you need to copy the same image
654into the resource directory for both English-Canadian and French-Canadian, but it's
655not true. Instead, you can save the image that's used for both as {@code icon_ca.png} (any
656name other than {@code icon.png}) and put
657it in the default {@code res/drawable/} directory. Then create an {@code icon.xml} file in {@code
658res/drawable-en-rCA/} and {@code res/drawable-fr-rCA/} that refers to the {@code icon_ca.png}
659resource using the {@code &lt;bitmap&gt;} element. This allows you to store just one version of the
660PNG file and two small XML files that point to it. (An example XML file is shown below.)</p>
661
662
663<h4>Drawable</h4>
664
665<p>To create an alias to an existing drawable, use the {@code &lt;bitmap&gt;} element.
666For example:</p>
667
668<pre>
669&lt;?xml version="1.0" encoding="utf-8"?>
670&lt;bitmap xmlns:android="http://schemas.android.com/apk/res/android"
671    android:src="@drawable/icon_ca" />
672</pre>
673
674<p>If you save this file as {@code icon.xml} (in an alternative resource directory, such as
675{@code res/drawable-en-rCA/}), it is compiled into a resource that you
676can reference as {@code R.drawable.icon}, but is actually an alias for the {@code
677R.drawable.icon_ca} resource (which is saved in {@code res/drawable/}).</p>
678
679
680<h4>Layout</h4>
681
682<p>To create an alias to an existing layout, use the {@code &lt;include&gt;}
683element, wrapped in a {@code &lt;merge&gt;}. For example:</p>
684
685<pre>
686&lt;?xml version="1.0" encoding="utf-8"?>
687&lt;merge>
688    &lt;include layout="@layout/main_ltr"/>
689&lt;/merge>
690</pre>
691
692<p>If you save this file as {@code main.xml}, it is compiled into a resource you can reference
693as {@code R.layout.main}, but is actually an alias for the {@code R.layout.main_ltr}
694resource.</p>
695
696
697<h4>Strings and other simple values</h4>
698
699<p>To create an alias to an existing string, simply use the resource ID of the desired
700string as the value for the new string. For example:</p>
701
702<pre>
703&lt;?xml version="1.0" encoding="utf-8"?>
704&lt;resources>
705    &lt;string name="hello">Hello&lt;/string>
706    &lt;string name="hi">@string/hello&lt;/string>
707&lt;/resources>
708</pre>
709
710<p>The {@code R.string.hi} resource is now an alias for the {@code R.string.hello}.</p>
711
712<p> <a href="{@docRoot}guide/topics/resources/more-resources.html">Other simple values</a> work the
713same way. For example, a color:</p>
714
715<pre>
716&lt;?xml version="1.0" encoding="utf-8"?>
717&lt;resources>
718    &lt;color name="yellow">#f00&lt;/color>
719    &lt;color name="highlight">@color/red&lt;/color>
720&lt;/resources>
721</pre>
722
723
724
725
726
727<h2 id="Compatibility">Providing the Best Device Compatibility with Resources</h2>
728
729<p>In order for your application to support multiple device configurations, it's very important that
730you always provide default resources for each type of resource that your application uses.</p>
731
732<p>For example, if your application supports several languages, always include a {@code
733values/} directory (in which your strings are saved) <em>without</em> a <a
734href="#LocalQualifier">language and region qualifier</a>. If you instead put all your string files
735in directories that have a language and region qualifier, then your application will crash when run
736on a device set to a language that your strings do not support. But, as long as you provide default
737{@code values/} resources, then your application will run properly (even if the user doesn't
738understand that language&mdash;it's better than crashing).</p>
739
740<p>Likewise, if you provide different layout resources based on the screen orientation, you should
741pick one orientation as your default. For example, instead of providing layout resources in {@code
742layout-land/} for landscape and {@code layout-port/} for portrait, leave one as the default, such as
743{@code layout/} for landscape and {@code layout-port/} for portrait.</p>
744
745<p>Providing default resources is important not only because your application might run on a
746configuration you had not anticipated, but also because new versions of Android sometimes add
747resource qualifiers that older versions do not support. If you use a new resource qualifier,
748but maintain code compatibility with older versions of Android, then when an older version of
749Android runs your application, it will crash if you do not provide default resources, because it
750cannot use the resources named with the new qualifier. For example, if your <a
751href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
752minSdkVersion}</a> is set to 4, and you qualify all of your drawable resources using <a
753href="NightQualifier">night mode</a> ({@code night} or {@code notnight}, which were added in API
754Level 8), then an API Level 4 device cannot access your drawable resources and will crash. In this
755case, you probably want {@code notnight} to be your default resources, so you should exclude that
756qualifier so your drawable resources are in either {@code drawable/} or {@code drawable-night/}.</p>
757
758<p>The bottom line is: For every type of resource your application uses,
759provide a set of default resources that allow your application to perform well, then create
760variations of those resources for specific device configurations.</p>
761
762<p>There is one exception to this rule: If your application's <a
763href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is 4 or
764greater, you <em>do not</em> need default drawable resources when you provide alternative drawable
765resources with the <a href="#DensityQualifier">screen density</a> qualifier. Even without default
766drawable resources, Android can find the best match among the alternative screen densities and scale
767the bitmaps as necessary. However, for the best experience on all types of devices, you should
768provide alternative drawables for all three types of density. (If your <a
769href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is
770<em>less than</em> 4, see the section below about <a href="#KnownIssues">known issues</a> for
771information about how to support multiple screen densities.)</p>
772
773
774
775
776<h2 id="BestMatch">How Android Finds the Best-matching Resource</h2>
777
778<p>When you request a resource for which you provide alternatives, Android selects which
779alternative resource to use at runtime, depending on the current device configuration. To
780demonstrate how Android selects an alternative resource, assume the following drawable directories
781each contain different versions of the same images:</p>
782
783<pre class="classic no-pretty-print">
784drawable/
785drawable-en/
786drawable-fr-rCA/
787drawable-en-port/
788drawable-en-notouch-12key/
789drawable-port-ldpi/
790drawable-port-notouch-12key/
791</pre>
792
793<p>And assume the following is the device configuration:</p>
794
795<p style="margin-left:1em;">
796Locale = <code>en-GB</code> <br/>
797Screen orientation = <code>port</code> <br/>
798Screen pixel density = <code>hdpi</code> <br/>
799Touchscreen type = <code>notouch</code> <br/>
800Primary text input method = <code>12key</code>
801</p>
802
803<p>By comparing the device configuration to the available alternative resources, Android selects
804drawables from {@code drawable-en-port}. It arrives at this decision using the following logic:</p>
805
806
807<div class="figure" style="width:280px">
808<img src="{@docRoot}images/resources/res-selection-flowchart.png" alt="" height="590" />
809<p class="img-caption"><strong>Figure 2.</strong> Flowchart of how Android finds the
810best-matching resource.</p>
811</div>
812
813
814<ol>
815  <li>Eliminate resource files that contradict the device configuration.
816    <p>The <code>drawable-fr-rCA/</code> directory is eliminated, because it
817contradicts the <code>en-GB</code> locale.</p>
818<pre class="classic no-pretty-print">
819drawable/
820drawable-en/
821<strike>drawable-fr-rCA/</strike>
822drawable-en-port/
823drawable-en-notouch-12key/
824drawable-port-ldpi/
825drawable-port-notouch-12key/
826</pre>
827<p class="note"><strong>Exception:</strong> Screen pixel density is the one qualifier that is not
828eliminated due to a contradiction. Even though the screen density of the device is mdpi,
829<code>drawable-port-ldpi/</code> is not eliminated because every screen density is
830considered to be a match at this point. More information is available in the <a
831href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
832Screens</a> document.</p></li>
833
834  <li>Pick the (next) highest-precedence qualifier in the list (<a href="#table2">table 2</a>).
835(Start with MCC, then move down.) </li>
836  <li>Do any of the resource directories include this qualifier?  </li>
837    <ul>
838      <li>If No, return to step 2 and look at the next qualifier. (In the example,
839  the answer is &quot;no&quot; until the language qualifier is reached.)</li>
840      <li>If Yes, continue to step 4.</li>
841    </ul>
842  </li>
843
844  <li>Eliminate resource directories that do not include this qualifier. In the example, the system
845eliminates all the directories that do not include a language qualifier:</li>
846<pre class="classic no-pretty-print">
847<strike>drawable/</strike>
848drawable-en/
849drawable-en-port/
850drawable-en-notouch-12key/
851<strike>drawable-port-ldpi/</strike>
852<strike>drawable-port-notouch-12key/</strike>
853</pre>
854<p class="note"><strong>Exception:</strong> If the qualifier in question is screen pixel density,
855Android
856selects the option that most closely matches the device, and the selection process is complete.
857In general, Android prefers scaling down a larger original image to scaling  up a smaller
858original image. See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
859Screens</a>.</p>
860  </li>
861
862  <li>Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen
863orientation is the next qualifier for which there are any matches.
864So, resources that do not specify a screen orientation are eliminated:
865<pre class="classic no-pretty-print">
866<strike>drawable-en/</strike>
867drawable-en-port/
868<strike>drawable-en-notouch-12key/</strike>
869</pre>
870<p>The remaining directory is {@code drawable-en-port}.</p>
871  </li>
872</ol>
873
874<p>Though this procedure is executed for each resource requested, the system further optimizes
875some aspects. One such optimization is that once the device configuration is known, it might
876eliminate alternative resources that can never match. For example, if the configuration
877language is English ("en"), then any resource directory that has a language qualifier set to
878something other than English is never included in the pool of resources checked (though a
879resource directory <em>without</em> the language qualifier is still included).</p>
880
881<p class="note"><strong>Note:</strong> The <em>precedence</em> of the qualifier (in <a
882href="#table2">table 2</a>) is more important
883than the number of qualifiers that exactly match the device. For example, in step 4 above, the last
884choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen
885type, and input method), while <code>drawable-en</code> has only one parameter that matches
886(language). However, language has a higher precedence than these other qualifiers, so
887<code>drawable-port-notouch-12key</code> is out.</p>
888
889<p>To learn more about how to use resources in your application, continue to <a
890href="accessing-resources.html">Accessing Resources</a>.</p>
891
892
893
894
895<h2 id="KnownIssues">Known Issues</h2>
896
897<p>The following are known issues in terms of how Android finds the best-matching resource on
898certain versions of Android.</p>
899
900<h3>Android 1.5 (and lower)</h3>
901
902<h4>Density and screen size qualifiers are not supported</h4>
903
904<p>Android 1.5 (and lower) does not support the following resource qualifers:</p>
905<dl>
906  <dt><a href="#DensityQualifier">Density</a></dt>
907    <dd>{@code ldpi}, {@code mdpi}, {@code ldpi}, and {@code nodpi}</dd>
908  <dt><a href="#ScreenSizeQualifier">Screen size</a></dt>
909    <dd>{@code small}, {@code normal}, and {@code large}</dd>
910  <dt><a href="#ScreenLongQualifier">Screen length</a></dt>
911    <dd>{@code long} and {@code notlong}</dd>
912</dl>
913
914<p>These resource qualifiers were introduced in Android 1.6, so Android 1.5 (API Level 3) and lower
915does not support them. If your application supports Android 1.5 and includes drawable resources for
916each density type ({@code drawable-ldpi/}, {@code drawable-mdpi/}, and {@code drawable-ldpi/}), then
917an Android 1.5 device might use any one of them, because it doesn't support the density qualifier,
918will ignore it, and will use which ever otherwise-matching drawable resource it finds first. This
919caveat applies the same to screen size and screen length resources.<p>
920
921<p><b>The fix:</b> Ensure that your <a href="{@docRoot}sdk/tools-notes.html">SDK Tools</a> version
922is r6 or greater and provide default drawable resources. For example, to support Android 1.5
923while providing resources for all screen densities, include a set of drawable resources that does
924not use the screen density qualifier. In fact, because all Android 1.5 devices have a medium-density
925screen, you can remove the {@code mdpi} qualifer and put all medium density images in {@code
926drawable/} (instead of {@code drawable-mdpi/}). For the screen size, put your {@code normal} size
927resources in the default resource directory, and for the screen length, put your {@code notlong}
928resources in the default resource directory (because all Android 1.5 devices have medium-density,
929normal, not-long screens).</p>
930
931<p>You need SDK Tools, Revision 6 (or greater), because it includes a new packaging tool that
932automatically applies an appropriate <a href="#VersionQualifier">version qualifier</a> to any
933resource directory named with a qualifier that did not exist in Android 1.0. For example, because
934the density qualifier was introduced in Android 1.6 (API Level 4), when the packaging tool
935encounters a resource directory using the density qualifier, it adds {@code v4} to the directory
936name to ensure that older versions do not use those resources (only API Level 4 and higher support
937that qualifier). Thus, by putting your medium-density resources in a directory <em>without</em> the
938{@code mdpi} qualifier, they are still accessible by Android 1.5, and any device that supports the
939density qualifer and has a medium-density screen also uses these resources because they are the best
940match for the device (instead of using the {@code ldpi} or {@code hdpi} resources).</p>
941
942<p class="note"><strong>Note:</strong> Later versions of Android, such as API Level 8,
943introduce other resource qualifiers that older version do not support. To provide the best
944compatibility, you should always include a set of default resources for each type of resource
945that your application uses. See the section about <a href="#Compatibility">Providing the Best Device
946Compatibility with Resources</a> for more information.</p>
947
948
949
950<h3>Android 1.5 and 1.6</h3>
951
952<h4>Version qualifier performs exact match, instead of best match</h4>
953
954<p>The correct behavior is for the system to match resources marked with a <a
955href="#VersionQualifier">version qualifier</a> equal
956to or less than the system version on the device, but on Android 1.5 and 1.6, (API Level 3 and 4),
957there is a bug that causes the system to match resources marked with the version qualifier
958only when it exactly matches the version on the device.</p>
959
960<p><b>The fix:</b> To provide version-specific resources, abide by this behavior. However, because
961this bug is fixed in versions of Android available after 1.6, if
962you need to differentiate resources between Android 1.5, 1.6, and later versions, then you only need
963to apply the version qualifier to the 1.6 resources and one to match all later versions. Thus, this
964is effectively a non-issue.</p>
965
966<p>For example, if you want drawable resources that are different on each Android 1.5, 1.6,
967and 2.0.1 (and later), create three drawable directories: {@code drawable/} (for 1.5 and lower),
968{@code drawable-v4} (for 1.6), and {@code drawable-v6} (for 2.0.1 and later&mdash;version 2.0, v5,
969is no longer available).</p>
970
971
972