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 <code>watch</code> 566 </td> 567 <td> 568 <ul class="nolist"> 569 <li>{@code car}: Device is displaying in a car dock</li> 570 <li>{@code desk}: Device is displaying in a desk dock</li> 571 <li>{@code television}: Device is displaying on a television, providing 572 a "ten foot" experience where its UI is on a large screen that the 573 user is far away from, primarily oriented around DPAD or other 574 non-pointer interaction</li> 575 <li>{@code appliance}: Device is serving as an appliance, with 576 no display</li> 577 <li>{@code watch}: Device has a display and is worn on the wrist</li> 578 </ul> 579 <p><em>Added in API level 8, television added in API 13, watch added in API 20.</em></p> 580 <p>For information about how your app can respond when the device is inserted into or 581 removed from a dock, read <a 582 href="{@docRoot}training/monitoring-device-state/docking-monitoring.html">Determining 583and Monitoring the Docking State and Type</a>.</p> 584 <p>This can change during the life of your application if the user places the device in a 585dock. You can enable or disable some of these modes using {@link 586android.app.UiModeManager}. See <a href="runtime-changes.html">Handling Runtime Changes</a> for 587information about how this affects your application during runtime.</p> 588 </td> 589 </tr> 590 <tr id="NightQualifier"> 591 <td>Night mode</td> 592 <td> 593 <code>night</code><br/> 594 <code>notnight</code> 595 </td> 596 <td> 597 <ul class="nolist"> 598 <li>{@code night}: Night time</li> 599 <li>{@code notnight}: Day time</li> 600 </ul> 601 <p><em>Added in API level 8.</em></p> 602 <p>This can change during the life of your application if night mode is left in 603auto mode (default), in which case the mode changes based on the time of day. You can enable 604or disable this mode using {@link android.app.UiModeManager}. See <a 605href="runtime-changes.html">Handling Runtime Changes</a> for information about how this affects your 606application during runtime.</p> 607 </td> 608 </tr> 609 <tr id="DensityQualifier"> 610 <td>Screen pixel density (dpi)</td> 611 <td> 612 <code>ldpi</code><br/> 613 <code>mdpi</code><br/> 614 <code>hdpi</code><br/> 615 <code>xhdpi</code><br/> 616 <code>xxhdpi</code><br/> 617 <code>xxxhdpi</code><br/> 618 <code>nodpi</code><br/> 619 <code>tvdpi</code> 620 </td> 621 <td> 622 <ul class="nolist"> 623 <li>{@code ldpi}: Low-density screens; approximately 120dpi.</li> 624 <li>{@code mdpi}: Medium-density (on traditional HVGA) screens; approximately 625160dpi.</li> 626 <li>{@code hdpi}: High-density screens; approximately 240dpi.</li> 627 <li>{@code xhdpi}: Extra-high-density screens; approximately 320dpi. <em>Added in API 628Level 8</em></li> 629 <li>{@code xxhdpi}: Extra-extra-high-density screens; approximately 480dpi. <em>Added in API 630Level 16</em></li> 631 <li>{@code xxxhdpi}: Extra-extra-extra-high-density uses (launcher icon only, see the 632 <a href="{@docRoot}guide/practices/screens_support.html#xxxhdpi-note">note</a> 633 in <em>Supporting Multiple Screens</em>); approximately 640dpi. <em>Added in API 634Level 18</em></li> 635 <li>{@code nodpi}: This can be used for bitmap resources that you do not want to be scaled 636to match the device density.</li> 637 <li>{@code tvdpi}: Screens somewhere between mdpi and hdpi; approximately 213dpi. This is 638not considered a "primary" density group. It is mostly intended for televisions and most 639apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and 640the system will scale them as appropriate. This qualifier was introduced with API level 13.</li> 641 </ul> 642 <p>There is a 3:4:6:8:12:16 scaling ratio between the six primary densities (ignoring the 643tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi, 24x24 in xhdpi and so on. 644</p> 645 <p>If you decide that your image resources don't look good enough on a television or 646other certain devices and want to try tvdpi resources, the scaling factor is 1.33*mdpi. For 647example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.</p> 648 <p class="note"><strong>Note:</strong> Using a density qualifier does not imply that the 649resources are <em>only</em> for screens of that density. If you do not provide alternative 650resources with qualifiers that better match the current device configuration, the system may use 651whichever resources are the <a href="#BestMatch">best match</a>.</p> 652 <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple 653Screens</a> for more information about how to handle different screen densities and how Android 654might scale your bitmaps to fit the current density.</p> 655 </td> 656 </tr> 657 <tr id="TouchscreenQualifier"> 658 <td>Touchscreen type</td> 659 <td> 660 <code>notouch</code><br/> 661 <code>finger</code> 662 </td> 663 <td> 664 <ul class="nolist"> 665 <li>{@code notouch}: Device does not have a touchscreen.</li> 666 <li>{@code finger}: Device has a touchscreen that is intended to 667 be used through direction interaction of the user's finger.</li> 668 </ul> 669 <p>Also see the {@link android.content.res.Configuration#touchscreen} configuration field, 670which indicates the type of touchscreen on the device.</p> 671 </td> 672 </tr> 673 <tr id="KeyboardAvailQualifier"> 674 <td>Keyboard availability</td> 675 <td> 676 <code>keysexposed</code><br/> 677 <code>keyshidden</code><br/> 678 <code>keyssoft</code> 679 </td> 680 <td> 681 <ul class="nolist"> 682 <li>{@code keysexposed}: Device has a keyboard available. If the device has a 683software keyboard enabled (which is likely), this may be used even when the hardware keyboard is 684<em>not</em> exposed to the user, even if the device has no hardware keyboard. If no software 685keyboard is provided or it's disabled, then this is only used when a hardware keyboard is 686exposed.</li> 687 <li>{@code keyshidden}: Device has a hardware keyboard available but it is 688hidden <em>and</em> the device does <em>not</em> have a software keyboard enabled.</li> 689 <li>{@code keyssoft}: Device has a software keyboard enabled, whether it's 690visible or not.</li> 691 </ul> 692 <p>If you provide <code>keysexposed</code> resources, but not <code>keyssoft</code> 693resources, the system uses the <code>keysexposed</code> resources regardless of whether a 694keyboard is visible, as long as the system has a software keyboard enabled.</p> 695 <p>This can change during the life of your application if the user opens a hardware 696keyboard. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about how 697this affects your application during runtime.</p> 698 <p>Also see the configuration fields {@link 699android.content.res.Configuration#hardKeyboardHidden} and {@link 700android.content.res.Configuration#keyboardHidden}, which indicate the visibility of a hardware 701keyboard and and the visibility of any kind of keyboard (including software), respectively.</p> 702 </td> 703 </tr> 704 <tr id="ImeQualifier"> 705 <td>Primary text input method</td> 706 <td> 707 <code>nokeys</code><br/> 708 <code>qwerty</code><br/> 709 <code>12key</code> 710 </td> 711 <td> 712 <ul class="nolist"> 713 <li>{@code nokeys}: Device has no hardware keys for text input.</li> 714 <li>{@code qwerty}: Device has a hardware qwerty keyboard, whether it's visible to the 715user 716or not.</li> 717 <li>{@code 12key}: Device has a hardware 12-key keyboard, whether it's visible to the user 718or not.</li> 719 </ul> 720 <p>Also see the {@link android.content.res.Configuration#keyboard} configuration field, 721which indicates the primary text input method available.</p> 722 </td> 723 </tr> 724 <tr id="NavAvailQualifier"> 725 <td>Navigation key availability</td> 726 <td> 727 <code>navexposed</code><br/> 728 <code>navhidden</code> 729 </td> 730 <td> 731 <ul class="nolist"> 732 <li>{@code navexposed}: Navigation keys are available to the user.</li> 733 <li>{@code navhidden}: Navigation keys are not available (such as behind a closed 734lid).</li> 735 </ul> 736 <p>This can change during the life of your application if the user reveals the navigation 737keys. See <a href="runtime-changes.html">Handling Runtime Changes</a> for 738information about how this affects your application during runtime.</p> 739 <p>Also see the {@link android.content.res.Configuration#navigationHidden} configuration 740field, which indicates whether navigation keys are hidden.</p> 741 </td> 742 </tr> 743 <tr id="NavigationQualifier"> 744 <td>Primary non-touch navigation method</td> 745 <td> 746 <code>nonav</code><br/> 747 <code>dpad</code><br/> 748 <code>trackball</code><br/> 749 <code>wheel</code> 750 </td> 751 <td> 752 <ul class="nolist"> 753 <li>{@code nonav}: Device has no navigation facility other than using the 754touchscreen.</li> 755 <li>{@code dpad}: Device has a directional-pad (d-pad) for navigation.</li> 756 <li>{@code trackball}: Device has a trackball for navigation.</li> 757 <li>{@code wheel}: Device has a directional wheel(s) for navigation (uncommon).</li> 758 </ul> 759 <p>Also see the {@link android.content.res.Configuration#navigation} configuration field, 760which indicates the type of navigation method available.</p> 761 </td> 762 </tr> 763<!-- DEPRECATED 764 <tr> 765 <td>Screen dimensions</td> 766 <td>Examples:<br/> 767 <code>320x240</code><br/> 768 <code>640x480</code><br/> 769 etc. 770 </td> 771 <td> 772 <p>The larger dimension must be specified first. <strong>This configuration is deprecated 773and should not be used</strong>. Instead use "screen size," "wider/taller screens," and "screen 774orientation" described above.</p> 775 </td> 776 </tr> 777--> 778 <tr id="VersionQualifier"> 779 <td>Platform Version (API level)</td> 780 <td>Examples:<br/> 781 <code>v3</code><br/> 782 <code>v4</code><br/> 783 <code>v7</code><br/> 784 etc.</td> 785 <td> 786 <p>The API level supported by the device. For example, <code>v1</code> for API level 7871 (devices with Android 1.0 or higher) and <code>v4</code> for API level 4 (devices with Android 7881.6 or higher). See the <a 789href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">Android API levels</a> document for more information 790about these values.</p> 791 </td> 792 </tr> 793</table> 794 795 796<p class="note"><strong>Note:</strong> Some configuration qualifiers have been added since Android 7971.0, so not all versions of Android support all the qualifiers. Using a new qualifier implicitly 798adds the platform version qualifier so that older devices are sure to ignore it. For example, using 799a <code>w600dp</code> qualifier will automatically include the <code>v13</code> qualifier, because 800the available-width qualifier was new in API level 13. To avoid any issues, always include a set of 801default resources (a set of resources with <em>no qualifiers</em>). For more information, see the 802section about <a href="#Compatibility">Providing the Best Device Compatibility with 803Resources</a>.</p> 804 805 806 807<h3 id="QualifierRules">Qualifier name rules</h3> 808 809<p>Here are some rules about using configuration qualifier names:</p> 810 811<ul> 812 <li>You can specify multiple qualifiers for a single set of resources, separated by dashes. For 813example, <code>drawable-en-rUS-land</code> applies to US-English devices in landscape 814orientation.</li> 815 <li>The qualifiers must be in the order listed in <a href="#table2">table 2</a>. For 816example: 817 <ul> 818 <li>Wrong: <code>drawable-hdpi-port/</code></li> 819 <li>Correct: <code>drawable-port-hdpi/</code></li> 820 </ul> 821 </li> 822 <li>Alternative resource directories cannot be nested. For example, you cannot have 823<code>res/drawable/drawable-en/</code>.</li> 824 <li>Values are case-insensitive. The resource compiler converts directory names 825 to lower case before processing to avoid problems on case-insensitive 826 file systems. Any capitalization in the names is only to benefit readability.</li> 827 <li>Only one value for each qualifier type is supported. For example, if you want to use 828the same drawable files for Spain and France, you <em>cannot</em> have a directory named 829<code>drawable-rES-rFR/</code>. Instead you need two resource directories, such as 830<code>drawable-rES/</code> and <code>drawable-rFR/</code>, which contain the appropriate files. 831However, you are not required to actually duplicate the same files in both locations. Instead, you 832can create an alias to a resource. See <a href="#AliasResources">Creating 833alias resources</a> below.</li> 834</ul> 835 836<p>After you save alternative resources into directories named with 837these qualifiers, Android automatically applies the resources in your application based on the 838current device configuration. Each time a resource is requested, Android checks for alternative 839resource directories that contain the requested resource file, then <a href="#BestMatch">finds the 840best-matching resource</a> (discussed below). If there are no alternative resources that match 841a particular device configuration, then Android uses the corresponding default resources (the 842set of resources for a particular resource type that does not include a configuration 843qualifier).</p> 844 845 846 847<h3 id="AliasResources">Creating alias resources</h3> 848 849<p>When you have a resource that you'd like to use for more than one device 850configuration (but do not want to provide as a default resource), you do not need to put the same 851resource in more than one alternative resource directory. Instead, you can (in some cases) create an 852alternative 853resource that acts as an alias for a resource saved in your default resource directory.</p> 854 855<p class="note"><strong>Note:</strong> Not all resources offer a mechanism by which you can 856create an alias to another resource. In particular, animation, menu, raw, and other unspecified 857resources in the {@code xml/} directory do not offer this feature.</p> 858 859<p>For example, imagine you have an application icon, {@code icon.png}, and need unique version of 860it for different locales. However, two locales, English-Canadian and French-Canadian, need to 861use the same version. You might assume that you need to copy the same image 862into the resource directory for both English-Canadian and French-Canadian, but it's 863not true. Instead, you can save the image that's used for both as {@code icon_ca.png} (any 864name other than {@code icon.png}) and put 865it in the default {@code res/drawable/} directory. Then create an {@code icon.xml} file in {@code 866res/drawable-en-rCA/} and {@code res/drawable-fr-rCA/} that refers to the {@code icon_ca.png} 867resource using the {@code <bitmap>} element. This allows you to store just one version of the 868PNG file and two small XML files that point to it. (An example XML file is shown below.)</p> 869 870 871<h4>Drawable</h4> 872 873<p>To create an alias to an existing drawable, use the {@code <bitmap>} element. 874For example:</p> 875 876<pre> 877<?xml version="1.0" encoding="utf-8"?> 878<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 879 android:src="@drawable/icon_ca" /> 880</pre> 881 882<p>If you save this file as {@code icon.xml} (in an alternative resource directory, such as 883{@code res/drawable-en-rCA/}), it is compiled into a resource that you 884can reference as {@code R.drawable.icon}, but is actually an alias for the {@code 885R.drawable.icon_ca} resource (which is saved in {@code res/drawable/}).</p> 886 887 888<h4>Layout</h4> 889 890<p>To create an alias to an existing layout, use the {@code <include>} 891element, wrapped in a {@code <merge>}. For example:</p> 892 893<pre> 894<?xml version="1.0" encoding="utf-8"?> 895<merge> 896 <include layout="@layout/main_ltr"/> 897</merge> 898</pre> 899 900<p>If you save this file as {@code main.xml}, it is compiled into a resource you can reference 901as {@code R.layout.main}, but is actually an alias for the {@code R.layout.main_ltr} 902resource.</p> 903 904 905<h4>Strings and other simple values</h4> 906 907<p>To create an alias to an existing string, simply use the resource ID of the desired 908string as the value for the new string. For example:</p> 909 910<pre> 911<?xml version="1.0" encoding="utf-8"?> 912<resources> 913 <string name="hello">Hello</string> 914 <string name="hi">@string/hello</string> 915</resources> 916</pre> 917 918<p>The {@code R.string.hi} resource is now an alias for the {@code R.string.hello}.</p> 919 920<p> <a href="{@docRoot}guide/topics/resources/more-resources.html">Other simple values</a> work the 921same way. For example, a color:</p> 922 923<pre> 924<?xml version="1.0" encoding="utf-8"?> 925<resources> 926 <color name="yellow">#f00</color> 927 <color name="highlight">@color/red</color> 928</resources> 929</pre> 930 931 932 933 934<h2 id="Compatibility">Providing the Best Device Compatibility with Resources</h2> 935 936<p>In order for your application to support multiple device configurations, it's very important that 937you always provide default resources for each type of resource that your application uses.</p> 938 939<p>For example, if your application supports several languages, always include a {@code 940values/} directory (in which your strings are saved) <em>without</em> a <a 941href="#LocaleQualifier">language and region qualifier</a>. If you instead put all your string files 942in directories that have a language and region qualifier, then your application will crash when run 943on a device set to a language that your strings do not support. But, as long as you provide default 944{@code values/} resources, then your application will run properly (even if the user doesn't 945understand that language—it's better than crashing).</p> 946 947<p>Likewise, if you provide different layout resources based on the screen orientation, you should 948pick one orientation as your default. For example, instead of providing layout resources in {@code 949layout-land/} for landscape and {@code layout-port/} for portrait, leave one as the default, such as 950{@code layout/} for landscape and {@code layout-port/} for portrait.</p> 951 952<p>Providing default resources is important not only because your application might run on a 953configuration you had not anticipated, but also because new versions of Android sometimes add 954configuration qualifiers that older versions do not support. If you use a new resource qualifier, 955but maintain code compatibility with older versions of Android, then when an older version of 956Android runs your application, it will crash if you do not provide default resources, because it 957cannot use the resources named with the new qualifier. For example, if your <a 958href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code 959minSdkVersion}</a> is set to 4, and you qualify all of your drawable resources using <a 960href="#NightQualifier">night mode</a> ({@code night} or {@code notnight}, which were added in API 961Level 8), then an API level 4 device cannot access your drawable resources and will crash. In this 962case, you probably want {@code notnight} to be your default resources, so you should exclude that 963qualifier so your drawable resources are in either {@code drawable/} or {@code drawable-night/}.</p> 964 965<p>So, in order to provide the best device compatibility, always provide default 966resources for the resources your application needs to perform properly. Then create alternative 967resources for specific device configurations using the configuration qualifiers.</p> 968 969<p>There is one exception to this rule: If your application's <a 970href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is 4 or 971greater, you <em>do not</em> need default drawable resources when you provide alternative drawable 972resources with the <a href="#DensityQualifier">screen density</a> qualifier. Even without default 973drawable resources, Android can find the best match among the alternative screen densities and scale 974the bitmaps as necessary. However, for the best experience on all types of devices, you should 975provide alternative drawables for all three types of density.</p> 976 977 978 979<h2 id="BestMatch">How Android Finds the Best-matching Resource</h2> 980 981<p>When you request a resource for which you provide alternatives, Android selects which 982alternative resource to use at runtime, depending on the current device configuration. To 983demonstrate how Android selects an alternative resource, assume the following drawable directories 984each contain different versions of the same images:</p> 985 986<pre class="classic no-pretty-print"> 987drawable/ 988drawable-en/ 989drawable-fr-rCA/ 990drawable-en-port/ 991drawable-en-notouch-12key/ 992drawable-port-ldpi/ 993drawable-port-notouch-12key/ 994</pre> 995 996<p>And assume the following is the device configuration:</p> 997 998<p style="margin-left:1em;"> 999Locale = <code>en-GB</code> <br/> 1000Screen orientation = <code>port</code> <br/> 1001Screen pixel density = <code>hdpi</code> <br/> 1002Touchscreen type = <code>notouch</code> <br/> 1003Primary text input method = <code>12key</code> 1004</p> 1005 1006<p>By comparing the device configuration to the available alternative resources, Android selects 1007drawables from {@code drawable-en-port}.</p> 1008 1009<p>The system arrives at its decision for which resources to use with the following 1010logic:</p> 1011 1012 1013<div class="figure" style="width:371px"> 1014<img src="{@docRoot}images/resources/res-selection-flowchart.png" alt="" height="471" /> 1015<p class="img-caption"><strong>Figure 2.</strong> Flowchart of how Android finds the 1016best-matching resource.</p> 1017</div> 1018 1019 1020<ol> 1021 <li>Eliminate resource files that contradict the device configuration. 1022 <p>The <code>drawable-fr-rCA/</code> directory is eliminated, because it 1023contradicts the <code>en-GB</code> locale.</p> 1024<pre class="classic no-pretty-print"> 1025drawable/ 1026drawable-en/ 1027<strike>drawable-fr-rCA/</strike> 1028drawable-en-port/ 1029drawable-en-notouch-12key/ 1030drawable-port-ldpi/ 1031drawable-port-notouch-12key/ 1032</pre> 1033<p class="note"><strong>Exception:</strong> Screen pixel density is the one qualifier that is not 1034eliminated due to a contradiction. Even though the screen density of the device is hdpi, 1035<code>drawable-port-ldpi/</code> is not eliminated because every screen density is 1036considered to be a match at this point. More information is available in the <a 1037href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple 1038Screens</a> document.</p></li> 1039 1040 <li>Pick the (next) highest-precedence qualifier in the list (<a href="#table2">table 2</a>). 1041(Start with MCC, then move down.) </li> 1042 <li>Do any of the resource directories include this qualifier? </li> 1043 <ul> 1044 <li>If No, return to step 2 and look at the next qualifier. (In the example, 1045 the answer is "no" until the language qualifier is reached.)</li> 1046 <li>If Yes, continue to step 4.</li> 1047 </ul> 1048 </li> 1049 1050 <li>Eliminate resource directories that do not include this qualifier. In the example, the system 1051eliminates all the directories that do not include a language qualifier:</li> 1052<pre class="classic no-pretty-print"> 1053<strike>drawable/</strike> 1054drawable-en/ 1055drawable-en-port/ 1056drawable-en-notouch-12key/ 1057<strike>drawable-port-ldpi/</strike> 1058<strike>drawable-port-notouch-12key/</strike> 1059</pre> 1060<p class="note"><strong>Exception:</strong> If the qualifier in question is screen pixel density, 1061Android selects the option that most closely matches the device screen density. 1062In general, Android prefers scaling down a larger original image to scaling up a smaller 1063original image. See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple 1064Screens</a>.</p> 1065 </li> 1066 1067 <li>Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen 1068orientation is the next qualifier for which there are any matches. 1069So, resources that do not specify a screen orientation are eliminated: 1070<pre class="classic no-pretty-print"> 1071<strike>drawable-en/</strike> 1072drawable-en-port/ 1073<strike>drawable-en-notouch-12key/</strike> 1074</pre> 1075<p>The remaining directory is {@code drawable-en-port}.</p> 1076 </li> 1077</ol> 1078 1079<p>Though this procedure is executed for each resource requested, the system further optimizes 1080some aspects. One such optimization is that once the device configuration is known, it might 1081eliminate alternative resources that can never match. For example, if the configuration 1082language is English ("en"), then any resource directory that has a language qualifier set to 1083something other than English is never included in the pool of resources checked (though a 1084resource directory <em>without</em> the language qualifier is still included).</p> 1085 1086<p>When selecting resources based on the screen size qualifiers, the system will use resources 1087designed for a screen smaller than the current screen if there are no resources that better match 1088(for example, a large-size screen will use normal-size screen resources if necessary). However, if 1089the only available resources are <em>larger</em> than the current screen, the system will 1090<strong>not</strong> use them and your application will crash if no other resources match the device 1091configuration (for example, if all layout resources are tagged with the {@code xlarge} qualifier, 1092but the device is a normal-size screen).</p> 1093 1094<p class="note"><strong>Note:</strong> The <em>precedence</em> of the qualifier (in <a 1095href="#table2">table 2</a>) is more important 1096than the number of qualifiers that exactly match the device. For example, in step 4 above, the last 1097choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen 1098type, and input method), while <code>drawable-en</code> has only one parameter that matches 1099(language). However, language has a higher precedence than these other qualifiers, so 1100<code>drawable-port-notouch-12key</code> is out.</p> 1101 1102<p>To learn more about how to use resources in your application, continue to <a 1103href="accessing-resources.html">Accessing Resources</a>.</p> 1104