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