OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / docs / html / guide / practices / screens_support.jd
1 page.title=Supporting Multiple Screens
2
3 @jd:body
4
5 <div id="qv-wrapper">
6 <div id="qv">
7
8   <h2>Quickview</h2>
9   <ul>
10     <li>Android runs on devices that have different screen sizes and resolutions.</li>
11     <li>The screen on which your application is displayed can affect its user interface.</li>
12     <li>The platform handles most of the work of adapting your app to the current screen.</li>
13     <li>You can create screen-specific resources for precise control of your UI, if needed. </li>
14     <li>Older applications run in a compatibility mode that provides best-effort rendering on the current screen.</li>
15     <li>It's important to follow the best practices described in this document and test your application in all supported screens. </li>
16   </ul>
17
18   <h2>In this document</h2>
19   <ol>
20     <li><a href="#overview">Overview of Screen Support</a></li>
21     <ol>
22        <li><a href="#range">Range of screens supported</a></li>
23        <li><a href="#support">How Android supports multiple screens</a></li>
24        <li><a href="#density-independence">Density independence</a></li>
25        <li><a href="#attrs">Manifest attributes</a></li>
26        <li><a href="#qualifiers">Resource qualifiers</a></li>
27     </ol>
28     <li style="padding-top:4px;"><a href="#screen-independence">Best Practices for Screen Independence</a></li>
29     <li><a href="#strategies">Strategies for Legacy Apps</a></li>
30     <li><a href="#testing">How to Test Your App</a></li>
31
32   </ol>
33
34   <h2>See Also</h2>
35   <ol>
36     <li><code><a href="{@docRoot}guide/topics/manifest/supports-screens-element.html">&lt;supports-screens&gt;</a></code></li>
37     <li><code><a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a></code></li>
38     <li><a
39 href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">
40 Providing Alternative Resources</a></li>
41     <li><a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a></li>
42   </ol>
43
44 </div>
45 </div>
46
47 <p>Android is designed to run on a variety of devices that offer a range of
48 screen sizes and resolutions. For applications, the platform provides a
49 consistent environment across devices and handles much of the complexity of
50 adapting an application's UI to the screen on which it is being displayed. At
51 the same time, the platform exposes APIs that give application developers
52 precise control over their application's UI when displayed on specific screen
53 sizes and resolutions. </p>
54
55 <p>This document explains the screens-support features provided by the platform
56 and how you use them in your application. By following the practices described
57 here, you can easily create an application that displays properly on all
58 supported device screens and that you can deploy to any device as a single {@code .apk}.
59 </p>
60
61 <p>If you have already developed and published an application for Android 1.5 or
62 earlier, you should read this document and consider how you may need to adapt
63 your application for proper display on new devices that offer different screens
64 and that are running Android 1.6 or later. In most cases, only minor adjustments
65 are needed, however you should make sure to <a href="#testing">test your
66 application</a> on all supported screens. </p>
67
68 <p>Starting in Android 2.2, the platform includes support for extra high density screens
69 (<em>xhdpi</em>), and starting in Android 2.3, the platform includes support for extra large screens
70 (<em>xlarge</em>). If you've already followed the guidance in this document to support all other
71 screen types, you should consider providing additional support for <em>xhdpi</em> and
72 <em>xlarge</em> screens.</p>
73
74 <p>In particular, if you have an existing application that you would like to
75 make available on small screens (such as QVGA) or for which you would like to provide better support
76 for extra large screens, please see <a href="#strategies">Strategies for Legacy Applications</a> for
77 more information about how to do that. </p>
78
79
80 <h2 id="overview">Overview of Screens Support</h2>
81
82 <p>The sections below provide an overview of the Android platform's support for
83 multiple screens, including an introduction to terms and concepts used in this
84 document and in the API, a summary of the screen configurations that the
85 platform supports, and an overview of the API and underlying
86 screen-compatibility features.</p>
87
88
89 <h3>Terms and Concepts</h3>
90
91 <dl>
92 <dt><em>Screen size</em></dt>
93   <dd>Actual physical size, measured as the screen's diagonal.
94
95   <p>For simplicity, Android collapses all actual screen sizes into four
96 generalized sizes: small, normal, large, and extra large. Applications can provide custom
97 layouts for each of these four sizes &mdash; the platform transparently handles
98 the rendering of the layouts at the actual screen size.</p></dd>
99
100 <dt><em>Aspect ratio</em></dt>
101   <dd>The porportional relationship of the screen's physical width to its
102 height. Applications can provide layout resources for specific aspect ratios by
103 using the resource qualifiers <code>long</code> and <code>notlong</code>. </dd>
104
105 <dt><em>Resolution</em></dt>
106   <dd>The total number of physical pixels on a screen. Note that, although
107 resolution is often expressed as <em>width</em> x <em>height</em>, resolution
108 does not imply a specific aspect ratio. In Android, applications do not work
109 directly with resolution.</dd>
110
111 <dt><em>Density</em></dt>
112   <dd>Based on the screen resolution, the spread of pixels across the physical
113 width and height of the screen.
114
115   <p>A screen with lower density has fewer available pixels spread across the
116 screen width and height, where a screen with higher density has more &mdash;
117 sometimes significantly more &mdash; pixels spread across the same area. The
118 density of a screen is important because, other things being equal, a UI element
119 (such as a button) whose height and width are defined in terms of screen pixels
120 will appear larger on the lower density screen and smaller on the higher density
121 screen.</p>
122
123   <p>For simplicity, Android collapses all actual screen densities into four
124 generalized densities: low, medium, large, and extra large. Applications can provide custom
125 resources for each of these densities &mdash; the platform handles any necessary
126 scaling of the resources up or down to meet the specific screen density. </p></dd>
127 <dt><em>Density-independent pixel (dp)</em></dt>
128   <dd>A virtual pixel unit that applications can use in defining their UI, to
129 express layout dimensions or position in a density-independent way.
130   <p>The density-independent pixel is equivalent to one physical pixel on a 160
131 dpi screen, the baseline density assumed by the platform (as described later in
132 this document). At run time, the platform transparently handles any scaling of
133 the dp units needed, based on the actual density of the screen in use. The
134 conversion of dp units to screen pixels is simple: <nobr><code>pixels = dps *
135 (density / 160)</code></nobr>. For example, on 240 dpi screen, 1 dp would equal 1.5
136 physical pixels. Using dp units to define your application's UI is highly
137 recommended, as a way of ensuring proper display of your UI on different
138 screens. </p></dd>
139 </dl>
140
141
142 <h3 id="range">Range of screens supported</h3>
143
144 <p>Starting from Android 1.6, the platform provides support for multiple screen
145 sizes and resolutions, reflecting the many new types and sizes of devices on
146 which the platform runs. If you are developing an application that will run
147 on Android 1.6 or later, you can use the compatibility features of the Android
148 platform to ensure that your application UI renders properly across the range of
149 supported screen sizes and resolutions.</p>
150
151 <p>To simplify the way that developers design their user interfaces for
152 multiple devices and to allow more devices to participate without affecting
153 applications, the platform divides the range of actual supported screen sizes
154 and resolutions into:</p>
155
156 <ul>
157 <li>A set of four generalized sizes: <em>small</em>, <em>normal</em>, <em>large</em>,
158 and <em>xlarge</em></em>
159 <li>A set of four generalized densities: <em>ldpi</em> (low), <em>mdpi</em> (medium),
160 <em>hdpi</em> (high), and <em>xhdpi</em> (extra high)
161 </ul>
162
163 <p class="note"><strong>Note:</strong> The <code>xhdpi</code> density category was added in
164 Android 2.2 (API Level 8). The <em>xlarge</em> size category was added in Android 2.3 (API Level
165 9).</p>
166
167 <p>Applications can provide custom resources (primarily layouts) for any of the
168 four generalized sizes and can provide resources (primarily drawables such as
169 images) for any of the four generalized densities. Applications do not need to
170 work with the actual physical size or density of the device screen. At run time,
171 the platform handles the loading of the correct size or density resources, based
172 on the generalized size or density of the current device screen, and adapts them
173 to the actual pixel map of the screen.</p>
174
175 <p>The generalized size/density configurations are arranged around a
176 baseline configuration that is assigned a size of <em>normal</em> and a density of
177 <em>mdpi</em> (medium). All applications written for Android 1.5 or earlier are (by
178 definition) designed for the baseline HVGA screen used on the T-Mobile G1 and
179 similar devices, which is size <em>normal</em> and density
180 <em>mdpi</em>.</p>
181
182 <p>Each generalized screen configuration spans a range of actual screen
183 densities and physical sizes. For example, that means that multiple devices that
184 report a screen size of <em>normal</em> might offer screens that differ slightly
185 in actual size or aspect ratio. Similarly, devices that report a screen density
186 of <em>hdpi</em> might offer screens with slightly different pixel densities.
187 The platform makes these differences abstract, however &mdash; applications can
188 offer UI designed for the generalized sizes and densities and let the system
189 handle the actual rendering of the UI on the current device screen according to
190 its characteristics. </p>
191
192
193 <img src="{@docRoot}images/screens_support/screens-ranges.png" />
194 <p class="img-caption"><strong>Figure 1.</strong>
195 Illustration of how the Android platform maps actual screen densities and sizes
196 to generalized density and size configurations. </p>
197
198 <p>Although the platform lets your application provide customized resources for
199 the various size and density configurations, you do not need to do write
200 custom code or provide custom resources for every combination of screen size and density.
201 The platform provides robust compatibility features, described
202 in the sections below, that can handle most of the work of rendering your
203 application on any device screen, provided that you've implemented your
204 application UI properly. For more information about how to implement a UI that
205 renders properly across device screens and platform versions, see
206 <a href="#screen-independence">Best Practices for Screen Independence</a>.</p>
207
208 <p>To help you test your applications, the Android SDK includes emulator skins
209 that replicate the sizes and densities of actual device screens on which your
210 application is likely to run. You can also modify the default size and density
211 of the emulator skins to replicate the characteristics of any specific
212 screen.</p>
213
214 <p class="table-caption" id="screens-table"><strong>Table 1.</strong> Screen
215 sizes and densities of emulator skins included in the Android SDK.</p>
216
217   <table>
218     <tbody>
219     <tr>
220       <td style="border:none"></td>
221       <td style="background-color:#f3f3f3">
222         <nobr>Low density (120), <em>ldpi</em></nobr>
223       </td>
224       <td style="background-color:#f3f3f3">
225         <nobr>Medium density (160), <em>mdpi</em></nobr>
226       </td>
227       <td  style="background-color:#f3f3f3">
228         <nobr>High density (240), <em>hdpi</em><nobr>
229       </td>
230       <td  style="background-color:#f3f3f3">
231         <nobr>Extra high density (320), <em>xhdpi</em><nobr>
232       </td>
233     </tr>
234     <tr>
235       <td  style="background-color:#f3f3f3">
236         <em>Small</em> screen
237       </td>
238       <td style="font-size:.9em;">QVGA (240x320)</td>
239       </td>
240       <td></td>
241       <td></td>
242       <td></td>
243     </tr>
244     <tr>
245       <td style="background-color:#f3f3f3">
246         <em>Normal</em> screen
247       </td>
248       <td style="font-size:.9em;">WQVGA400 (240x400)<br>WQVGA432 (240x432)</td>
249       <td style="font-size:.9em;">HVGA (320x480)</td>
250       <td style="font-size:.9em;">WVGA800 (480x800)<br>WVGA854 (480x854)</td>
251       <td style="font-size:.9em;"></td>
252     </tr>
253     <tr>
254       <td style="background-color:#f3f3f3">
255         <em>Large</em> screen
256       </td>
257       <td></td>
258       <td style="font-size:.9em;">WVGA800* (480x800)<br>WVGA854* (480x854)</td>
259       <td></td>
260       <td></td>
261     </tr>
262     <tr>
263       <td style="background-color:#f3f3f3">
264         <em>Extra Large</em> screen
265       </td>
266       <td></td>
267       <td></td>
268       <td></td>
269       <td></td>
270     </tr>
271     <tr>
272       <td colspan="4" style="border:none;font-size:90%;">* To emulate this
273         configuration, specify a custom density of 160 when
274         creating an AVD that uses a WVGA800 or WVGA854 skin.
275       </td>
276     </tr>
277 </table>
278
279 <p>For an overview of the relative numbers of high (hdpi), medium (mdpi), and
280 low (ldpi) density screens in Android-powered devices available now, see the <a
281 href="{@docRoot}resources/dashboard/screens.html">Screen Sizes and Densities</a> dashboard.</p>
282
283
284 <h3 id="support">How Android supports multiple screens</h3>
285
286 <div class="sidebox-wrapper">
287 <div class="sidebox">
288 <h2>Using the alternative resources framework</h2>
289
290 <p>The platform's support for loading screen size- and density-specific
291 resources at run time is based on the alternative resources framework.
292
293 <p> If you want to use size- or density-specific layouts or drawables in your
294 application and you are not familiar with resource qualifiers or how the
295 platform uses them, please read
296 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">
297 Providing Alternative Resources</a>.
298 </div>
299 </div>
300
301 <p>The foundation of Android's support for multiple screens is a set of built-in
302 compatibility features that together manage the rendering of application
303 resources in an appropriate way for the current device screen. The platform
304 handles most of the work of rendering your application, but also gives you two
305 key ways to control how your application is displayed, if you need or want
306 to use them:</p>
307
308 <ul>
309   <li>The platform supports a set of resource qualifiers that let you provide
310 size- and density-specific resources, if needed. The qualifiers for
311 size-specific resources are <code>small</code>, <code>normal</code>, <code>large</code>, and
312 <code>xlarge</code>. Those for density-specific resources are <code>ldpi</code>
313 (low), <code>mdpi</code> (medium), <code>hdpi</code> (high), and <code>xhdpi</code> (extra high).
314 The qualifiers correspond to the generalized densities described in
315 <a href="#range">Range of screens supported</a>, above.</li>
316   <li>The platform also provides a
317 <a href="{@docRoot}guide/topics/manifest/supports-screens-element.html">
318 <code>&lt;supports-screens&gt;</code></a>
319 manifest element, whose attributes
320 <code>android:smallScreens</code>, <code>android:normalScreens</code>,
321 <code>android:largeScreens</code>, and <code>android:xlargeScreens</code> let you specify what
322 generalized screen sizes
323 your application supports. Another attribute, <code>android:anyDensity</code>,
324 lets you indicate whether or not your application includes built-in support for
325 multiple densities.</li>
326 </ul>
327
328 <p>At run time, the platform provides three types of support to your
329 application, to ensure the best possible display on the current device
330 screen:</p>
331
332 <ol>
333 <li><em>Pre-scaling of resources (such as image assets)</em>
334
335   <p>Based on the density of the current screen, the platform automatically
336 loads any size- or density-specific resources from your application and displays
337 them without scaling. If no matching resources are available, the platform loads
338 the default resources and scales them up or down as needed to match the current
339 screen's generalized density. The platform assumes that default resources are
340 designed for proper display at the baseline screen density of "medium" (160),
341 unless they are loaded from a density-specific resource directory.</p>
342
343   <p>For example, if the current screen's density is "high", the platform loads
344 resources that are tagged with the qualifier <code>hdpi</code> and uses them
345 without scaling. If no such resources are available, the platform uses the
346 default resources instead, scaling them from the baseline density ("medium") to
347 "high".  </p>
348
349   <p>For more information about how to create size- and density-specific
350 resources, see <a href="#qualifiers">Resource qualifiers</a>.</p></li>
351
352 <li><em>Auto-scaling of pixel dimensions and coordinates</em>
353
354   <p>If the application states that it does not support different screen
355 densities, the platform auto-scales any absolute pixel coordinates, pixel
356 dimension values, and pixel math used in the application (such as might be used
357 for specifying the width or padding for a view). It does this to ensure that
358 pixel-defined screen elements are displayed at approximately the same physical
359 size as they would be at the baseline density of "medium" (160). The platform
360 handles this scaling transparently to the application and also reports scaled
361 overall pixel dimensions to the application, rather than physical pixel
362 dimensions. </p>
363
364   <p>For instance, suppose a given device is using a WVGA high-denisty screen,
365 which is 480x800 and about the same size as a traditional HVGA screen, but it's
366 running an app that states that it does not support multiple densities. In this
367 case, the system will "lie" to the application when it queries for screen
368 dimensions, and report 320x533. Then, when the app does drawing operations, such
369 as invalidating the rectangle from (10,10) to (100, 100), the system will
370 likewise automatically transform the coordinates by scaling them the appropriate
371 amount, and actually invalidate the region (15,15) to (150, 150).  The same
372 thing happens in the other direction, if the application is running on a
373 lower-density screen, coordinates are scaled down.<p>
374
375   <p>For more information, see the <code>android:anyDensity</code> attribute in
376 <a href="#attrs">Manifest attributes for screens support</a>.</p></li>
377
378 <div class="sidebox-wrapper" xstyle="margin-bottom:2em;margin-top:.5em;width:90%;">
379   <img id="rule" src="{@docRoot}assets/images/grad-rule-qv.png">
380   <div id="qv-sub-rule">
381     <img src="{@docRoot}assets/images/icon_market.jpg" style="float:left;margin:0;padding:0;">
382     <p style="color:#669999;">Publishing to Small Screen Devices</p>
383     <p>To ensure the best experience for users on small-screen devices, Android
384 Market only shows applications that explicitly declare support for small
385 screens. If you developed an application on Android 1.5 or earlier and published
386 it on Android Market, you need to <a href="#testing">test your application</a>
387 on small screens and then upload an updated version that explicitly
388 <a href="#attrs">indicates support for small screens</a>. </p>
389   </div>
390 </div>
391
392 <li><em>Compatibility-mode display on larger screen-sizes</em>
393
394   <p>If the current screen's size is larger than your application supports, as
395 specified in the <code>supports-screens</code> element, the platform displays
396 the application at the baseline size ("normal") and density ("medium). For
397 screens larger than baseline, the platform displays the application in a
398 baseline-sized portion of the overall screen, against a black background. </p>
399
400   <p>For instance, suppose a given device is using a WVGA medium density screen,
401 classified as a "large" screen, but the application states that it does not
402 support large screens; in this case, the system will again "lie" to the
403 application when it queries for screen dimensions, and report 320x480.  Instead
404 of scaling the application, however, the application's 320x480 interface will be
405 placed as a "postage stamp" in the larger 480x800 screen.</p>
406
407   <p>For more information, see the <code>android:anyDensity</code> attribute in
408 <a href="#attrs">Manifest elements for screens support</a> and the
409 <a href="#compatibility-examples">Screen-Compatibility Examples</a>
410 section.</p></li>
411 </ol>
412
413 <p>In general, these compatibility features ensure that all applications,
414 including those written against Android 1.5 and earlier platform versions, can
415 display properly on most devices, especially when the device's screen is at the
416 baseline "normal" size or larger. </p>
417
418 <p>However, note that applications written for the baseline screen may need
419 minor adjustments before they display properly on smaller screens such as QVGA.
420 With the reduced screen area of small screens, there may be tradeoffs in design,
421 content, and function that you, as the application developer, need to consider.
422 For more information about how to prepare an existing application for display on
423 small screens, see <a href="#strategies">Strategies for Legacy
424 Applications</a>.</p>
425
426 <p>The sections below provide more information how to take advantage of the
427 platform's multiple-screens support. </p>
428
429
430 <h3 id="density-independence">Density independence</h3>
431
432 <p>The goal of density independence is to preserve the physical size, from the
433 user's point of view, of user interface elements declared in an application,
434 when the application is displayed on screens with different densities. Density
435 independence applies to both layouts and drawables such as icons. Maintaining
436 density-independence is important because, other things being equal, a UI
437 element (such as a button) whose height and width are defined in terms of screen
438 pixels will appear physically larger on the lower density screen and smaller on
439 the higher density screen. Such density-related size changes can cause problems
440 in application layout, usability, and consistency with other applications
441 installed on the device.</p>
442
443 <p>The platform provides density independence to applications by default. It
444 does this in three ways: </p>
445
446 <ul>
447 <li>Through pre-scaling of drawable resources (scaled at resource loading
448 time)</li>
449 <li>Through auto-scaling of density-independent pixel (dp) values used in
450 layouts</li>
451 <li>Through auto-scaling of absolute pixel values used in the application (only
452 needed if the application has set <code>android:anyDensity="false"</code> in its
453 manifest)</li>
454 </ul>
455
456 <p>The example screens below illustrate the density independence provided by the
457 platform. Note that both the layouts and launcher icons are displayed at the
458 same physical sizes, although screen sizes, aspect ratios, and densities are
459 different.</p>
460
461
462 <div id=vi09 style=TEXT-ALIGN:left>
463 <img src="{@docRoot}images/screens_support/dip.png" style="padding-bottom:0;margin-bottom:0;" />
464 <p class="caption" style="margin:0 0 1.5em 1em;padding:0 0 0
465 1em;"><strong>Figure 2.</strong> Examples of density independence on WVGA high
466 density (left), HVGA medium density (center), and QVGA low density (right). </p>
467 </div>
468
469 <p>In most cases, you can take advantage of density independence in your
470 application simply by making sure that your layouts specify all dimension values
471 in density-independent pixels (<code>dp</code> or <code>dp</code>) or
472 scale-independent pixels (<code>sip</code> or <code>sp</code>, for text only).
473 If you are using absolute pixel values in the application and manifest includes
474 <a href="#attrs"><code>android:anyDensity="true"</code></a>, you will also need
475 to scale the pixel values. See <a href="#dips-pels">Converting dp units to
476 pixel units</a> for more information. </p>
477
478
479 <h3 id="attrs">Manifest attributes for screens support</h3>
480
481 <p> Android 1.6 introduced a new manifest element,
482 <a href="{@docRoot}guide/topics/manifest/supports-screens-element.html"><code>&lt;supports-screens&gt;</code></a>,
483 whose attributes you can use to control the
484 display of your application on different classes of device screens, as listed
485 in table 2. The <code>smallScreens</code>, <code>normalScreens</code>, <code>largeScreens</code> and
486 <code>xlargeScreens</code> attributes correspond to the generalized screen sizes
487 described in <a href="#range">Range of screens supported</a>, earlier in this
488 document. Notice that the default values for each attribute vary, depending
489 on your minimum and targeted platform, as indicated in the <a
490 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code
491 android:minSdkVersion}</a> and <a
492 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code
493 android:targetSdkVersion}</a> attributes of your <a
494 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code &lt;uses-sdk&gt;}</a>
495 manifest element.</p>
496
497 <p class="table-caption" id="table2"><strong>Table 2.</strong> Summary of attributes for the <a
498 href="{@docRoot}guide/topics/manifest/supports-screens-element.html">{@code
499 &lt;supports-screens&gt;}</a> manifest element, including default values based on platform
500 version.</p>
501     <table id="vrr8">
502       <tr>
503         <th>
504           Attribute
505         </th>
506         <th >
507           Description
508         </th>
509         <th>
510           Default value, when<br><nobr><code>minSdkVersion</code> or</nobr>
511 <code>targetSdkVersion</code> is 4 or lower
512         </th>
513         <th>
514           Default value, when<br><nobr><code>minSdkVersion</code> or</nobr>
515 <code>targetSdkVersion</code> is 5 or higher
516         </th>
517       </tr>
518       <tr>
519         <td>
520           <code>android:smallScreens</code>
521         </td>
522         <td>
523           Whether or not the application UI is designed for use on
524 <em>small</em> screens &mdash; "<code>true</code>" if it is, and
525 "<code>false</code>" if not. </p>
526         </td>
527 <td>"<code>false</code>"</td>
528 <td>"<code>true</code>"</td>
529       </tr>
530       <tr>
531         <td>
532           <code>android:normalScreens</code>
533         </td>
534         <td>
535            Whether or not the application UI is designed for use on
536 <em>normal</em> screens &mdash; "<code>true</code>" if it is, and
537 "<code>false</code>" if not. The default value is always "<code>true</code>".
538         </td>
539 <td>"<code>true</code>"</td>
540 <td>"<code>true</code>"</td>
541       </tr>
542       <tr>
543         <td>
544           <code>android:largeScreens</code>
545         </td>
546         <td>
547            Whether or not the application UI is designed for use on
548 <em>large</em> screens &mdash; "<code>true</code>" if it is, and
549 "<code>false</code>" if not.
550         </td>
551 <td>"<code>false</code>"</td>
552 <td>"<code>true</code>"</td>
553       </tr>
554       <tr>
555         <td>
556           <code>android:anyDensity</code>
557         </td>
558         <td>
559          <p>Whether or not the application is designed to manage its UI properly
560 in different density environments &mdash; "<code>true</code>" if so, and
561 "<code>false</code>" if not. </p>
562         <ul>
563           <li>If set to "<code>true</code>", the platform disables its
564 density-compatibility features for all screen densities &mdash; specifically,
565 the auto-scaling of absolute pixel units (<code>px</code>) and math &mdash; and
566 relies on the application to use density-independent pixel units
567 (<code>dp</code>) and/or math to manage the adaptation of pixel values according
568 to density of the current screen. That is, as long as your application uses
569 density-independent units (dp) for screen layout sizes, then it will perform
570 properly on different densities when this attribute is set to
571 "<code>true</code>".</li>
572
573           <li>If set to "<code>false</code>", the platform enables its
574 density-compatibility features for all screen densities. In this case, the
575 platform provides a scaled, virtual screen pixel map to the application, against
576 which it can layout and draw its UI as though against a medium-density screen
577 (160). The platform then transparently auto-scales the application's pixel units
578 and math as needed to match the actual device screen density. </li>
579         </ul>
580 <p>Note that the setting of this attribute affects density-compatibility only.
581 It does not affect size-compatibility features such as display on a virtual
582 baseline screen.</p>
583         </td>
584 <td>"<code>false</code>"</td>
585 <td>"<code>true</code>"</td>
586       </tr>
587       <tr>
588         <td colspan="4"><strong>Note:</strong> Android 2.3 (API Level 9) introduced a new
589 attribute for the <code>&lt;supports-screens&gt;</code> element: <code>xlargeScreens</code>, shown
590 below. It works the same as the other screen attributes above, but, if neither your
591 <code>minSdkVersion</code> or <code>targetSdkVersion</code> are set to "9", the default value is
592 "false" when your application is installed on a device running Android 2.3.</td>
593       </tr>
594       <tr>
595         <th>
596           Attribute
597         </th>
598         <th >
599           Description
600         </th>
601         <th>
602           Default value, when<br><nobr><code>minSdkVersion</code> or</nobr>
603 <code>targetSdkVersion</code> is 8 or lower
604         </th>
605         <th>
606           Default value, when<br><nobr><code>minSdkVersion</code> or</nobr>
607 <code>targetSdkVersion</code> is 9 or higher
608         </th>
609       </tr>
610       <tr>
611         <td>
612           <code>android:xlargeScreens</code>
613         </td>
614         <td>
615            Whether or not the application UI is designed for use on
616 <em>xlarge</em> screens &mdash; "<code>true</code>" if it is, and
617 "<code>false</code>" if not.
618         </td>
619 <td>"<code>false</code>"</td>
620 <td>"<code>true</code>"</td>
621       </tr>
622     </table>
623
624 <p>In general, when you declare a screen-size attribute
625 (<code>smallScreens</code>, <code>normalScreens</code>, <code>largeScreens</code>, or
626 <code>xlargeScreens</code>) as "<code>true</code>", you are signaling to the
627 platform that your application is designed to render properly on that screen
628 size. As a result, the platform does not apply any size-compatibility features
629 (such as a virtual HVGA display area). If you declare a screen-size attribute as
630 "<code>false</code>", you are signaling that your application is <em>not</em>
631 designed for that screen size. In this case, the platform <em>does</em> apply
632 size-compatibility features, rendering the application in an HVGA baseline
633 display area. If the current screen is larger than <em>normal</em> size, the
634 platform renders the application in a virtual HVGA screen on the larger screen.
635 See <a href="#compatibility-examples">Screen-Compatibility Examples</a> for an
636 illustration of what an application looks like when displayed in a virtual HVGA
637 screen.</p>
638
639 <p>In other words, setting a <code>&lt;supports-screens&gt;</code> attribute to
640 "<code>false</code>" tells the platform to enable it's compatibility features
641 when displaying the application on a screen of that size <em>or any larger
642 size</em>, if also disallowed. Otherwise, the platform gives the application a
643 normal display area that can use the full device screen area, if
644 appropriate.</p>
645
646 <p>Android Market also makes use of the <code>&lt;supports-screens&gt;</code>
647 attributes. It uses them to filter the application from devices whose screens
648 are not supported by the application. Specifically, Android Market considers an
649 application compatible with a device if the application supports a screen that
650 is the same or smaller than the current device screen. Android Market filters
651 the application if it disallows the device's screen size and does not support a
652 smaller size. In general, Android does not provide downward size-compatibility
653 features for applications.</p>
654
655 <p>Here are some examples:</p>
656
657 <ul>
658     <li>Assume that you declare <code>smallScreens="false" normalScreens="true"
659 largeScreens="false" xlargeScreens="false"</code> in your application's manifest. <p>Although the
660 application is not designed for display on large or extra large screens, the platform can still
661 run it successfully in <a href="#compatibility-examples">screen-compatibility
662 mode</a>. Android Market shows the application to devices with
663 <em>normal</em>, <em>large</em>, and <em>xlarge</em> size screens, but does filter it from
664 <em>small</em> size screens, because the application provides no screen support at
665 <em>small</em> size. Android's <a href="#compatibility-examples">screen-compatibility
666 mode</a> mode does not provide support for screens that are smaller than those the
667 application supports&mdash;it only provides support for screens that are larger. Thus,
668 although the application declares "false" for <em>large</em> and <em>xlarge</em> screens,
669 the application still functions, but runs in compatibility mode.</p></li>
670
671     <li>Assume that you declare <code>smallScreens="false" normalScreens="false"
672 largeScreens="true" xlargeScreens="true"</code> in your application's manifest. <p>Android Market
673 filters the application from users of devices with <em>small</em> and
674 <em>normal</em> size screens. In effect, this prevents such users from
675 installing the application.</p></li>
676 </ul>
677
678 <p>If you declare the <code>android:anyDensity</code> attribute as
679 "<code>true</code>", you are signaling to the platform that your application is
680 designed to display properly on any screen density. In this case, the
681 application must ensure that it declares its UI dimensions using
682 density-independent pixels (<code>dp</code>) and scales any absolute pixel
683 values (<code>px</code>) or math by the scaling factor available from {@link
684 android.util.DisplayMetrics#density android.util.DisplayMetrics.density}. See <a
685 href="#dips-pels">Converting dp units to pixel units</a> for an example.</p>
686
687 <p>Note that the setting of the <code>android:anyDensity</code> attribute does
688 not affect the platform's pre-scaling of drawable resources, such as bitmaps and
689 nine-patch images, which always takes place by default. </p>
690
691 <p>The following example shows a manifest that declares support for small, normal, large, and
692  xlarge screens in any density.</p>
693
694 <pre>
695 &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"&gt;
696     &lt;supports-screens
697         android:smallScreens="true"
698         android:normalScreens="true"
699         android:largeScreens="true"
700         android:xlargeScreens="true"
701         android:anyDensity="true" /&gt;
702     ...
703 &lt;/manifest&gt;
704 </pre>
705 <!--  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; android:resizeable="true" -->
706 <h4 id="defaults">
707   Default values for attributes
708 </h4>
709
710 <p>The default values for the <code>&lt;supports-screens&gt;</code> attributes
711 differ, depending on the the value of the
712 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html"><code>android:minSdkVersion</code></a>
713  attribute in the application's manifest, as well as on
714 the value of <a
715 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code
716 android:targetSdkVersion}</a>, if declared.</p>
717
718 <p>Above, <a href="#table2">table 2</a> indicates the default values for each attribute, based on
719 the values you provide for the <a
720 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code
721 android:minSdkVersion}</a> and <a
722 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code
723 android:targetSdkVersion}</a>, in the <a
724 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code &lt;uses-sdk&gt;}</a>
725 element.</p>
726
727 <p class="note"><strong>Note:</strong> If your application uses APIs introduced in Android 1.6 or
728 higher, but does not support specific screen densities and/or screen sizes, you need to explicitly
729 set the appropriate attributes to "<code>false</code>" (because most are "true", by default).</p>
730
731
732 <h3 id="qualifiers">Resource directory qualifiers for screen size and density</h3>
733
734 <p>Android supports resource directory qualifiers for controlling the selection
735 of resources based on the characteristics of the screen on which your application
736 is running. You can use these qualifiers to provide size- and density-specific
737 resources in your application. For more information about the generalized sizes
738 and densities that correspond to the qualifiers, see <a href="#range">Range
739 of Screens Supported</a>, earlier in this document.</p>
740
741 <table>
742 <tr>
743 <th>Screen characteristic</th>
744 <th>Qualifier</th>
745 <th>Description</th>
746 </tr>
747
748 <tr>
749   <td rowspan="4">Size</td>
750   <td><code>small</code></td>
751   <td>Resources designed for <em>small</em> size screens.</td>
752 </tr>
753 <tr>
754   <td><code>normal</code></td>
755   <td>Resources designed for <em>normal</em> size screens.</td>
756 </tr>
757 <tr>
758 <td><code>large</code></td>
759 <td>Resources designed for <em>large</em> size screens.</td>
760 </tr>
761 <tr>
762 <td><code>xlarge</code></td>
763 <td>Resources designed for <em>extra large</em> size screens.</td>
764 </tr>
765
766 <tr>
767 <td rowspan="5">Density</td>
768 <td><code>ldpi</code></td>
769 <td>Resources designed for low-density (<em>ldpi</em>) screens.</td>
770 </tr>
771 <tr>
772 <td><code>mdpi</code></td>
773 <td>Resources designed for medium-density (<em>mdpi</em>) screens.</td>
774 </tr>
775 <tr>
776 <td><code>hdpi</code></td>
777 <td>Resources designed for high-density (<em>hdpi</em>) screens.</td>
778 </tr>
779 <tr>
780 <td><code>xhdpi</code></td>
781 <td>Resources designed for extra high-density (<em>xhdpi</em>) screens.</td>
782 </tr>
783 <tr>
784 <td><code>nodpi</code></td>
785 <td>Density-independent resources. The platform does not auto-scale resources
786 tagged with this qualifier, regardless of the current screen's density.</td>
787 </tr>
788
789 <tr>
790 <td rowspan="2">Aspect ratio</td>
791 <td><code>long</code></td>
792 <td>Resources for screens of any size or density that have a significantly
793 taller (in portrait mode) and wider (in landscape mode) aspect ratio than the
794 baseline screen configuration.</td>
795 </tr>
796 <tr>
797 <td><code>notlong</code></td>
798 <td>Resources for use only on screens that have an aspect ratio that is similar
799 to the baseline screen configuration.</td>
800 </tr>
801 <tr>
802 <td>Platform version</td>
803 <td><nobr><code>v&lt;api-level&gt;</code></nobr></td>
804 <td>Resources that are for use only on a specific API Level or higher. For
805 example, if your application is designed to run on both Android 1.5 (API Level
806 3) and Android 1.6 (API Level 4 and higher), you can use the <code>-v4</code>
807 qualifier to tag any resources that should be excluded when your application is
808 running on Android 1.5 (API Level 3).  </td>
809 </tr>
810 </table>
811
812 <p>
813 Note that the density and the screen size are independent parameters and are
814 interpreted by the system individually. For example, WVGA high density is
815 considered a normal screen because its physical size is about the same as one of
816 T-Mobile G1. On the other hand, a WVGA medium density screen is considered a
817 <i>large</i> screen &mdash; it offers the same resolution but at lower pixel
818 density, meaning that it is both physically larger than the baseline screen and
819 can display significantly more information than a normal screen size.
820 </p>
821
822 <p>Here is an example of the resource directory structure of an application that
823 employs different layout schemes for different screen sizes and supports low and high density
824 screens.</p>
825
826 <pre>
827 res/layout/my_layout.xml            // layout for normal screen size
828 res/layout-small/my_layout.xml      // layout for small screen size
829 res/layout-large/my_layout.xml      // layout for large screen size
830 res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode
831 res/layout-xlarge/my_layout.xml     // layout for extra large screen size
832
833 res/drawable-lhdpi/my_icon.png      // image for low density
834 res/drawable-mdpi/dpi/my_icon.png   // image for medium density
835 res/drawable-hdpi/my_icon.png       // image for high density
836
837 res/drawable-nodpi/composite.xml    // density independent resource
838 </pre>
839
840 <p>For more information about how to use resource qualifiers or how the platform
841 selects them, please read
842 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">
843 Providing Alternative Resources</a>.</p>
844
845
846 <h2 id="screen-independence">Best practices for Screen Independence</h2>
847
848 <p>The objective of supporting multiple screens is to create an application that
849 can run properly on any display and function properly on any of the generalized
850 screen configurations supported by the platform.
851 </p>
852
853 <p>You can easily ensure that your application will display properly on
854 different screens. Here is a quick checklist:</p>
855
856 <ol>
857   <li>
858    Use {@code wrap_content}, {@code fill_parent}, or the {@code dp} unit (instead of {@code px}),
859 when specifying dimensions in an XML layout file
860   </li>
861   <li>
862     Do not use {@code AbsoluteLayout}
863   </li>
864   <li>
865     Do not use hard coded pixel values in your code
866   </li>
867   <li>
868     Use density and/or resolution specific resources
869   </li>
870 </ol>
871
872 <h3 id="use-relative">1. Use wrap_content, fill_parent, or the dp unit, instead of
873 absolute pixels<br> </h3>
874
875 <p>When defining the <code>layout_width</code> and <code>layout_height</code> of
876 views in an XML layout file, using <code>wrap_content</code>,
877 <code>fill_parent</code> or the <code>dp</code> will guarantee that the view is
878 given an appropriate size on the current device screen. For instance, a view
879 with a <code>layout_width="100dp"</code> will measure 100 pixels wide on an
880 HVGA@160 density display and 150 pixels on a WVGA@240 density display, but the
881 view will occupy approximately the same physical space. </p>
882
883 <p>Similarly, you should prefer the <code>sp</code> (scale-independent pixel,
884 the scale factor depends on a user setting) or <code>dp</code> (if you don't
885 want to allow the user to scale the text) to define font sizes.</p>
886
887 <h3 id="avoid-absolute">2. Avoid AbsoluteLayout </h3>
888
889 <p>{@link android.widget.AbsoluteLayout AbsoluteLayout}
890 is one of the layout containers offered by the Android UI toolkit. Unlike the
891 other layouts however, <code>AbsoluteLayout</code> enforces the use of fixed
892 positions which might easily lead to user interfaces that do not work well on
893 different displays. Because of this, <code>AbsoluteLayout</code> was deprecated
894 in Android 1.5 (API Level 3). </p>
895
896 <p>You can achieve much the same layout by using a
897 {@link android.widget.FrameLayout FrameLayout} instead, and setting
898 <code>layout_margin</code> attributes of the children. This approach is more
899 flexible and will yield better results on different screens.</p>
900
901 <h3>3. Do not use hard-coded pixel values in your code</h3>
902
903 <p>For performance reasons and to keep the code simpler, the Android framework
904 API uses pixels as the standard unit for expressing dimension or coordinate
905 values. That means that the dimensions of a View are always expressed in the
906 code in pixels. For instance, if <code>myView.getWidth()</code> returns 10, the
907 view is 10 pixels wide. In some cases, you may need to scale the pixel values
908 that you use in your code. The sections below provide more information. </p>
909
910 <h4 id="dips-pels">Converting dp units to pixel units</h4>
911
912 <p>In some cases, you will need to express dimensions in <code>dp</code> and
913 then convert them to pixels. Imagine an application in which a scroll gesture is
914 recognized after the user's finger has moved by at least 16 pixels. On a
915 baseline screen, the user will have to move his finger by 16 pixels / 160
916 dpi = 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a
917 device with a high (240) density display, the user will move his finger by only
918 16 pixels / 240 dpi = 1/15th of an inch (or 1.7 mm.) The distance is much
919 shorter and the application thus appears more sensitive to the user. To fix this
920 issue, the gesture threshold must be expressed in the code in <code>dp</code>
921 and then converted to actual pixels.</p>
922
923 <pre>// The gesture threshold expressed in dp
924 private static final float GESTURE_THRESHOLD_DP = 16.0f;
925
926 // Convert the dps to pixels
927 final float scale = getContext().getResources().getDisplayMetrics().density;
928 mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);</span>
929
930 // Use mGestureThreshold as a distance in pixels
931 </pre>
932
933 <p>The {@link android.util.DisplayMetrics#density android.util.DisplayMetrics.density}
934 field specifies the the scale factor you must use to
935 convert dps to pixels according to the current screen density. You can access
936 the current screen's metrics through a <code>Context</code> or
937 <code>Activity</code>. On a medium (160) density screen,
938 <code>DisplayMetrics.density</code> equals "1.0", whereas on a high (240)
939 density screen it equals "1.5". You can refer to the documentation of the
940 {@link android.util.DisplayMetrics DisplayMetrics}
941 class for details.</p>
942
943 <h4>Use pre-scaled configuration values</h4>
944
945 <p>The {@link android.view.ViewConfiguration ViewConfiguration} class can be
946 used to access the most common distances, speeds, and times used in the Android
947 framework. For instance, the distance in pixels used by the framework as the
948 scroll threshold can be obtained as follows:</p>
949
950 <pre>ViewConfiguration.get(aContext).getScaledTouchSlop()</pre>
951
952 <p>Methods starting with the <code>getScaled</code> prefix are guaranteed to return a value in pixels that will display properly regardless of the current screen density.</p>
953
954 <h3>4. Use density and/or size-specific resources</h3>
955
956 <div style="float: right;background-color:#fff;margin: 0;padding: 20px 0 20px 20px;">
957 <img src="{@docRoot}images/screens_support/scale-test.png" style="padding:0;margin:0;">
958 <p class="caption" style="margin:0;padding:0;"><strong>Figure 3.</strong> Comparison of pre-scaled and auto-scaled bitmaps.</p>
959 </div>
960
961 <p>Even with the size- and density-compatibility features that the platform
962 provides, you may still want to make adjustments to the UI of your application
963 when it displayed on certain screen sizes or densities. You can do this by
964 providing size- or density-specific resources &mdash; assets, layouts, strings,
965 and so on. If you want, you can also take control over the scaling of images
966 assets. The sections below provide more information.</p>
967
968 <h4 id="resource-dirs">Custom resources and directories</h4>
969
970 <p>If you need to control exactly how your application will look on various
971 displays, simply adjust your assets and layouts in configuration-specific
972 resources directories. For example, consider an icon that you want to display on
973 medium and high density screens. Simply create your icon at two different sizes
974 (for instance 100x100 for medium density and 150x150 for high density) and put
975 the two variations in the appropriate directories, using the proper
976 qualifiers:</p>
977
978 <p style="margin-left:2em;"><code>res/drawable-mdpi/icon.png&nbsp;&nbsp;&nbsp;//
979 for medium-density screens</code></p>
980
981 <p style="margin-left:2em;"><code>res/drawable-hdpi/icon.png&nbsp;&nbsp;&nbsp;//
982 for high-density screens</code></p>
983
984 <p>If a density qualifier is not defined in a resource directory name, the
985 platform assumes that the resources in that directory are designed for the
986 baseline medium density. It is not recommended that you put density-specific
987 resources such as images in the default directory.</p>
988
989 <p>For more information about valid resource qualifiers, see
990 <a href="#qualifiers">Resource directory qualifiers</a>, earlier in this
991 document.</p>
992
993 <h4 id="scaling">Pre-scaling and auto-scaling of bitmaps and nine-patches</h4>
994
995 <p>When a bitmap or nine-patch image is loaded from the application's resources,
996 the platform attempts to pre-scale it to match the display's density. For
997 instance, if you placed a 100x100 icon in the <code>res/drawable/</code>
998 directory and loaded that icon as a bitmap on a high-density screen, Android
999 would automatically scale up the icon and produce a 150x150 bitmap.</p>
1000
1001 <p>This pre-scaling mechanism works independently of the source. For instance,
1002 an application targeted for a high-density screen may have bitmaps only in the
1003 <code>res/drawable-hdpi/</code> directory. If one of the bitmaps is a 240x240
1004 icon and is loaded on a medium-density screen, the resulting bitmap will measure
1005 160x160.</p>
1006
1007 <p>The platform pre-scales resources as needed, whether the application is
1008 running with density-compatibility features enabled or not (as specified by the
1009 value of <code>android:anyDensity</code>). However, when running with
1010 density-compatibility enabled, the platform continues to report the size of
1011 pre-scaled bitmaps and other resources as if they were loaded in a
1012 medium-density environment. For example, when density-compatibility is enabled,
1013 if you load a 76x76 image from the default resources for display on a
1014 high-density screen, the platform will pre-scale the image to 114x114
1015 internally. However, the API still reports the size of the image as 76x76. This
1016 discrepancy may cause unexpected behavior if your application somehow directly
1017 manipulates the scaled bitmap, but this was considered a reasonable trade-off to
1018 keep the performance of existing applications as good as possible.</p>
1019
1020 <p>This does not apply for the case that an application creates an in-memory
1021 bitmap internally and draws something on it, for later display on the screen.
1022 The platform auto-scales such bitmaps on the fly, at draw time. Other side
1023 effects of such a case might be that fonts drawn in such a bitmap will be scaled
1024 at the bitmap level, when the off-screen bitmap is finally rendered to the
1025 display, resulting in scaling artifacts.</p>
1026
1027 <p>There are situations in which you may not want Android to automatically scale
1028 a resource. The easiest way to accomplish this is to put it in a "nodpi"
1029 resource directory:</p>
1030
1031 <p style="margin-left:2em;"><code>res/drawable-nodpi/icon.png</code></p>
1032
1033 <p>You can also take complete control of the scaling mechanism by using the
1034 {@link android.graphics.BitmapFactory.Options BitmapFactory.Options} class,
1035 which lets you define whether you want the bitmap to be pre-scaled and what the
1036 density of the bitmap should be. For instance, if you are loading a bitmap from
1037 a web server, you may want to force the bitmap's density to be high density.
1038 When pre-scaling is disabled, the resulting bitmap is in auto-scaling mode. The
1039 bitmap is associated with a density (that you may or may not have specified
1040 through the <code>BitmapFactory.Options</code>) which will be used to scale the
1041 bitmap on screen <em>at drawing time</em>.
1042
1043 <p>Using auto-scaling instead of pre-scaling is more CPU expensive than
1044 pre-scaling but uses less memory. You can refer to the documentation of
1045 {@link android.graphics.BitmapFactory BitmapFactory},
1046 {@link android.graphics.Bitmap Bitmap}, and
1047 {@link android.graphics.Canvas Canvas} for more
1048 information on auto-scaling.</p>
1049
1050 <p>Figure 3, at right, demonstrates the results of the pre-scale and auto-scale
1051 mechanisms when loading low (120), medium (160) and high (240) density bitmaps
1052 on a baseline screen. The differences are subtle, because all of the bitmaps are
1053 being scaled to match the current screen density, however the scaled bitmaps
1054 have slightly different appearances depending on whether they are pre-scaled or
1055 auto-scaled at draw time.</p>
1056
1057 <h2 id="strategies">Strategies for Legacy Applications</h2>
1058
1059 <p>If you have already developed and published an Android application based on
1060 Android 1.5 or earlier platform version, you need to consider how you will adapt
1061 your application so that it is deployable to:</p>
1062
1063 <ul>
1064 <li>Existing devices, which may be running Android 1.5 (or lower) platform
1065 version, as well as to </li>
1066 <li>Newer devices that are running Android 1.6 (or higher) and offering various
1067 screen sizes and resolutions</li>
1068 </ul>
1069
1070 <p class="note"><strong>Note:</strong> Even if your application targets Android 1.6 already, you
1071 should follow the same strategies below in order to support <em>xhdpi</em> and <em>xlarge</em>
1072 screens on Android 2.3 (API Level 9), while maintaining compatibility with older versions of
1073 the platform.</p>
1074
1075 <p>To support the newer devices and the different screens they use, you might
1076 need to make some changes in your app, but at the same time your app may be very
1077 stable and so you want to minimize the changes. There are a variety of ways that
1078 you can extend your existing application to support new devices with multiple
1079 screens <em>and</em> existing devices running older platform versions. You
1080 should be able to make these changes to your application such that you can
1081 distribute a single {@code .apk} to all devices.</p>
1082
1083 <p>The recommended strategy is to develop against the most recent version of the
1084 platform you are targeting, and test on the minimum platform version you want to run on.
1085 Here's how to do that:</p>
1086
1087 <ol>
1088   <li>Maintain compatibility with existing devices by leaving your application's
1089 <code>android:minSdkVersion</code> attribute as it is. You <em>do not</em> need
1090 to increment the value of the attribute to support new devices and multiple
1091 screens. </li>
1092   <li>Extend compatibility for Android 1.6 (and higher) devices by adding
1093 a new attribute &mdash; <code>android:targetSdkVersion</code> &mdash; to the
1094 <code>uses-sdk</code> element. Set the value of the attribute to
1095 <code>"4"</code>. [To support <em>xhdpi</em> and <em>xlarge</em> screens, set the value to
1096 <code>"9"</code>.] This allows your application to "inherit" the platform's
1097 multiple screens support, even though it is technically using an earlier version
1098 of the API. </li>
1099   <li>Add an empty <code>&lt;supports-screens&gt;</code> element as a child of
1100 <code>&lt;manifest&gt;</code>. If you need to enable size or density attributes
1101 later, this is where you will add them.</li>
1102   <li>Change your application's build properties, such that it compiles against
1103 the Android 1.6 (API Level 4) library [or against Android 2.3 (API Level 9) to support
1104 <em>xhdpi</em> and <em>xlarge</em> screens], rather than against the Android 1.5 (or
1105 earlier) library. You will not be able to compile your application against the
1106 older platform because of the new manifest attribute. </li>
1107   <li>Set up AVDs for testing your application on Android 1.6 [or Android 2.3] and higher
1108 releases. Create AVDs that use the screen sizes and densities that you want to
1109 support. When you create the AVDs, make sure to select the Android 1.6 [or Android 2.3] or higher
1110 platform as the system image to run. For more information, see <a
1111 href="#testing">How to Test Your Application on Multiple Screens</a>,
1112 below.</li>
1113   <li>Set up AVDs for testing your application on older versions of the platform, as low as the
1114 version declared by your <code>android:minSdkVersion</code>. You need AVDs running the older
1115 platforms you are targeting, so that
1116 you can test for compatibility and ensure that there are no functional
1117 regressions. </li>
1118   <li>Compile your application against the Android 1.6 [or Android 2.3] library and run it on the
1119 AVDs you created. Observe the way your application looks and runs, and test all
1120 of the user interactions. </li>
1121   <li>Debug any display or functional issues. For issues that you resolve in
1122 your application code, <span style="color:red">make certain not to use any APIs
1123 introduced later than the version declared by your <code>android:minSdkVersion</code></span>. If you
1124 are in doubt, refer to SDK reference documentation and look for the API Level specifier for the API
1125 you want to use. Using newer APIs not supported by your minimum version will mean that your
1126 application will no longer be compatible with devices running on that version.</li>
1127   <li>For resource-related issues, you can try resolving them by:
1128     <ul>
1129     <li>Adding a <code>anyDensity="false"</code> attribute to
1130 <code>&lt;supports-screens&gt;</code>, to enable density-compatibility
1131 scaling.</li>
1132     <li>Creating any size- or density-specific resources you need and placing
1133 them in directories tagged with the <a href="#qualifiers">correct
1134 qualifiers</a>. Qualifiers must be arranged in a proscribed order. See
1135 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">
1136 Providing Alternative Resources</a> for more information. </li>
1137     <li>Note that if you add size- or density-specific resource directories
1138 tagged with any of the resource qualifiers listed in this document, you should
1139 make sure to also tag those directories with the <code>v&lt;api-level&gt;</code>
1140 qualifier (for example, <code>-v4</code> to target API Level 4). This ensures that those resources
1141 will be ignored when the application is run on Android 1.5 or lower platform
1142 versions.</p></li>
1143     </ul>
1144   </li>
1145   <li>If your application does not offer support (such as custom layouts) for
1146 large screens and you want the platform to display your application in
1147 screen-compatibility mode on larger screens, add the
1148 <code>largeScreens="false"</code> and <code>xlargeScreens="false"</code> attributes to the
1149 <code>&lt;supports-screens&gt;</code> element in the manifest. See
1150 <a href="#compatibility-examples">Screen-Compatibility Examples</a> for
1151 illustrations of how the platform displays your application in this case.</li>
1152   <li>If your application does not offer support (such as custom layouts) for
1153 small screens (such as on a QVGA low-density screen) and you do not want Android
1154 Market to offer the application to users of small-screen devices, you
1155 <em>must</em> add a <code>smallScreens="false"</code> attribute to the
1156 <code>&lt;supports-screens&gt;</code> element. </li>
1157   <li>Continue testing and debugging until your application performs as expected
1158 on all of the platforms and screen sizes your application will support.</li>
1159   <li>Export, zipalign, and sign your application using the same private key you
1160 used when publishing the previous version, then publish the application to users
1161 as an update. </li>
1162 </ol>
1163
1164 <p>In particular, remember to test your application on an AVD that emulates a
1165 small-screen device. Devices that offer screens with QVGA resolution at low
1166 density are available now. Users of those devices may want to download your
1167 application, so you should understand how your application will look and
1168 function on a small-screen device. In many cases, the reduced screen area and
1169 density mean that you may need to make tradeoffs in design, content, and
1170 function on those devices. </p>
1171
1172 <p>Also give extra attention to testing your application on an AVD that emulates an <em>xlarge</em>
1173 screen. Devices with extra large screens
1174 are tablet-sized or larger, so you should pay close attention to how usable your application is on
1175 such screens. You might want to design new layouts specifically for extra large screens, to address
1176 usability aspects such as the location and size of buttons in your UI. To test your application on
1177 an extra large screen, create an AVD targeted to Android 2.3 with a high resolution, such as 1280 x
1178 800, and the default density of 160dpi. This AVD will use any resources you've provided with the
1179 <code>xlarge</code> <a href="#qualifiers">resouce qualifier</a>.</p>
1180
1181
1182 <h2 id="testing">How to Test Your Application on Multiple Screens</h2>
1183
1184 <p>Before publishing an application that supports multiple screens, you should
1185 thoroughly test it in all of the targeted screen sizes and densities. You can
1186 test how it displays with the platform's compatibility features enabled or with
1187 screen-specific UI resources included in your application. The Android SDK
1188 includes all the tools you need to test your application on any supported
1189 screen.</p>
1190
1191 <!-- You can test in any minsdk, and you can test with compatabiltiy code or
1192 not. Once you've tested your application and found that it displays properly on
1193 various screen sizes, you should make sure to add the corresponding size
1194 attribute(s) to your application's manifest. -->
1195
1196 <div id="f9.5" class="figure" style="width:530px">
1197   <img src="{@docRoot}images/screens_support/avds-config.png" />
1198   <p class="img-caption"><strong>Figure 4.</strong>
1199   A typical set of AVDs for testing screens support.</p>
1200 </div>
1201
1202 <p>As a test environment for your applications, set up a series of AVDs that
1203 emulate the screen sizes and densities you want to support. The Android SDK
1204 includes several emulator skins to get you started. You can use the Android AVD
1205 Manager or the <code>android</code> tool to create AVDs that use the various
1206 emulator skins and you can also set up custom AVDs to test densities other than
1207 the defaults. For general information about working with AVDs, see
1208 <a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual
1209 Devices</a>.</p>
1210
1211 <p>The Android SDK provides a set of default emulator skins that you can use for
1212 testing. The skins are included as part of each Android platform that you can
1213 install in your SDK. The Android 1.6 platform offers these default skins:</p>
1214
1215 <ul>
1216   <li>
1217     QVGA (240x320, low density, small screen)
1218   </li>
1219   <li>
1220     HVGA (320x480, medium density, normal screen)
1221   </li>
1222   <li>
1223     WVGA800 (480x800, high density, normal screen)
1224   </li>
1225   <li>
1226     WVGA854 (480x854 high density, normal screen)
1227   </li>
1228 </ul>
1229
1230 <p>The Android 2.0 platform offers all of the Android 1.6 default skins,
1231 above, plus:</p>
1232
1233 <ul>
1234   <li>
1235     WQVGA400 (240x400, low density, normal screen)
1236   </li>
1237   <li>
1238     WQVGA432 (240x432, low density, normal screen)
1239   </li>
1240 </ul>
1241
1242 <p>If you are using the <code>android</code> tool command line to create your
1243 AVDs, here's an example of how to specify the skin you want to use:</p>
1244
1245 <pre>android create avd ... --skin WVGA800</pre>
1246
1247 <p>We also recommend that you test your application in an emulator that is set
1248 up to run at a physical size that closely matches an actual device. This makes
1249 it a lot easier to compare the results at various resolutions and densities. To
1250 do so you will need to know the approximate density, in dpi, of your computer
1251 monitor (a 30" Dell monitor has for instance a density of about 96 dpi.). Use
1252 your monitor's dpi as the value of the <code>-scale</code> option, when
1253 launching the emulator, for example:</p>
1254
1255 <pre>emulator -avd &lt;name&gt; -scale 96dpi</pre>
1256
1257 <p>If you are working in Eclipse with ADT, you can specify the <code>-scale
1258 96dpi</code> option in the Target tab of run and debug configurations, under
1259 "Additional Emulator Command Line Options" field. </p>
1260
1261 <p>Note that starting the emulator with the <code>-scale</code> option will
1262 scale the entire emulator display, based on both the dpi of the skin and of your
1263 monitor. The default emulator skins included in the Android SDK are listed
1264 in <a href="#screens-table">Table 1</a>, earlier in this document.</p>
1265
1266 <div class="figure" style="width:324px">
1267   <img src="{@docRoot}images/screens_support/avd-density.png" >
1268   <p class="img-caption"><strong>Figure 5.</strong>
1269   Resolution and density options that you can use, when creating an AVD using the AVD Manager.</p>
1270 </div>
1271
1272 <p>You should also make sure to test your application on different physical
1273 screen sizes within a single size-density configuration. For example, to
1274 display this screen configuration on a 30" monitor you will need to adjust
1275 the value passed to <code>-scale</code> to 96*2.8/3.3 = 81dpi. You can also
1276 pass a float value to <code>-scale</code> to specify your own scaling factor:</p>
1277
1278 <pre>emulator -avd &lt;name&gt; -scale 0.6</pre>
1279
1280 <p>If you would like to test your application on a screen that uses a resolution
1281 or density not supported by the built-in skins, you can either adjust an
1282 existing skin, or create an AVD that uses a custom resolution or density.</p>
1283
1284 <p>In the AVD Manager, you can specify a custom skin resolution or density in
1285 the Create New AVD dialog, as shown in Figure 5, at right.</p>
1286
1287 <p>In the <code>android</code> tool, follow these steps to create an AVD with a
1288 custom resolution or density:</p>
1289
1290 <ol>
1291   <li>Use the <code>create avd</code> command to create a new AVD, specifying
1292 the <code>--skin</code> option with a value that references either a default
1293 skin name (such as "WVGA800") or a custom skin resolution (such as 240x432).
1294 Here's an example:
1295      <pre>android create avd -n &lt;name&gt; -t &lt;targetID&gt; --skin WVGA800</pre>
1296   </li>
1297   <li>To specify a custom density for the skin, answer "yes" when asked whether
1298 you want to create a custom hardware profile for the new AVD.</li>
1299   <li>Continue through the various profile settings until the tool asks you to
1300 specify "Abstracted LCD density" (<em>hw.lcd.density</em>). Enter an appropriate
1301 value, such as "120" for a low-density screen, "160" for a medium density screen,
1302 or "240" for a high-density screen.</li>
1303   <li>Set any other hardware options and complete the AVD creation.</li>
1304 </ol>
1305
1306 <p>In the example above (WVGA medium density), the new AVD will emulate a 5.8"
1307 WVGA screen.</p>
1308
1309 <p>As an alternative to adjusting the emulator skin configuration, you can use
1310 the emulator skin's default density and add the <code>-dpi-device</code> option
1311 to the emulator command line when starting the AVD. For example, </p>
1312
1313 <pre>emulator -avd WVGA800 -scale 96dpi -dpi-device 160</pre>
1314
1315
1316 <h2 id="compatibility-examples">Screen-Compatibility Examples</h2>
1317
1318 <p>This section provides examples of how the Android platform displays an
1319 application written for the baseline screen configuration &mdash; HVGA (320x480)
1320 resolution on a 3.2" screen &mdash; with all of the platform's size- and
1321 density-compatibility features enabled. That is, the examples show how
1322 the platform displays an application that doesn't provide built-in support
1323 for the screen on which it is being rendered, but which instead relies completely
1324 on the platform.</p>
1325
1326 <p>The platform's screen-compatibility features are designed to provide such
1327 an application with a virtual baseline screen environment against which to run,
1328 while at the same time ensuring for the user a physical display that is
1329 approximately the same as the baseline screen size and density. </p>
1330
1331 <p>Legacy applications that have not been modified to support multiple
1332 screens would be typical examples of such applications. In most cases,
1333 you would want to add multiple-screens support to a legacy application and
1334 publish an updated version, as described in <a href="#strategies">Strategies
1335 for Legacy Applications</a>. However, if you did not do so, the
1336 platform still performs best-effort rendering of your application, as
1337 illustrated below.</p>
1338
1339 <p> Internally, these are the compatibility features that the platform
1340 provides, based on the current device screen:</p>
1341
1342   <ul>
1343     <li>
1344       If the device's screen density is <em>not medium</em>, the application's
1345 layout and drawing of its content is as if the screen <em>is</em> medium density, but the
1346 framework scales the layout and images (if the image for the target density is
1347 not available) to fit the target density. It scales 1.5 times if the target
1348 density is high density (160-&gt;240 virtual dpi), or 0.75 times if the target
1349 density is low density (160 -&gt; 120 virtual dpi).
1350     </li>
1351     <li>
1352       If the device's screen size is <em>small</em>, there are few options
1353 options for making Android 1.5 applications work well on such a screen, so
1354 Android Market will filter applications that are not known to support these
1355 screens from the device.
1356     </li>
1357     <li>
1358       If the device's screen size is <em>large</em>, it limits the application's
1359 screen to the normal size and draws a black background around the application.
1360 For example, if an application supports high density, but does not support large
1361 screens, it only uses a 480x720 area of the screen and the rest will be filled
1362 with a black background (see example below).
1363     </li>
1364   </ul>
1365
1366 <table style="width:10%;margin-left:.5em;">
1367   <tr>
1368     <td>
1369       HVGA, normal size, normal density<br>
1370       [ emulator -skin HVGA ]<br>
1371       <img height=149 src="{@docRoot}images/screens_support/afdvfckr9j_15dcsvrscg_b.png" width=225>
1372     </td>
1373     <td>
1374        WVGA, normal size, high density<br>
1375       [emulator -skin WVGA854 -dpi-device 240]<br>
1376       <img height=143 src="{@docRoot}images/screens_support/afdvfckr9j_18c6mhm3cm_b.png" width=254><br>
1377       <p>The application occupies full screen as its considered to be normal size. (close to 480x720)</p>
1378     </td>
1379   </tr>
1380   <tr>
1381     <td>
1382       VGA, large size, medium density<br>
1383       [ emulator -skin 640x480 ]<br>
1384       <img height=243 src="{@docRoot}images/screens_support/afdvfckr9j_14fj6dhsc3_b.png" width=324>
1385       <p>The application occupies 320x480 of VGA.</p>
1386     </td>
1387     <td>
1388       SVGA, large size, high density<br>
1389       [ emulator -skin 800x600 -dpi-device 240]<br>
1390       <img height=223 src="{@docRoot}images/screens_support/afdvfckr9j_19c743p6cr_b.png" width=294>
1391       <p>The application occupies 480x720 (=1.5 x [320x480]) of 800x600.</p>
1392     </td>
1393   </tr>
1394 </table>
1395
1396
1397 <h3>Screen-compatibility limitations on small, low-density screens</h3>
1398
1399 <p>Because these device has smaller state/resolution, there are known
1400 limitations when application runs in compatibility mode.</p>
1401
1402 <h4>QVGA</h4>
1403
1404 <p>Because QVGA (240x320) screens have less screen area available and lower
1405 density than normal, which is 240x360 in low density, some applications cannot
1406 render all their content properly on those screens.&nbsp; As a result, on a QVGA
1407 device, Android Market will filter out all applications that do not declare they
1408 support small screens.</p>
1409
1410 <p>Examples:</p>
1411
1412 <table style="width:10%;margin-left:.5em;">
1413   <tr>
1414     <td>The part of z value graph is chopped.</td>
1415     <td>The lap time area is chopped.<br></td>
1416   </tr>
1417   <tr>
1418     <td><img height=207 src="{@docRoot}images/screens_support/afdvfckr9j_16g95wjqg3_b.png" width="155"></td>
1419     <td><img height=186 src="{@docRoot}images/screens_support/afdvfckr9j_17p2w4txgc_b.png" width="139"></td>
1420   </tr>
1421 </table>
1422
1423
1424 <h4>Images with 1 pixel height/width.</h4>
1425
1426 <p>If an image has 1 pixel height or width, it may not be shown on the screen
1427 due to rounding issue. This is inevitable as it just does not have enough
1428 pixels.</p>
1429
1430 <p>For example, in the screen below, the divider in the menu is invisible
1431 because the width of the image is trancated to 0. (This particular problem is
1432 solvable because menu is handled inside framework, but there is no generic
1433 solution as it just does not have enough pixels.)</p>
1434
1435 <img height=222 src="{@docRoot}images/screens_support/afdvfckr9j_20fvptbbdd_b.png" width=166>
1436
1437
1438