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