Scott Main | adaf5a7 | 2011-02-16 15:52:38 -0800 | [diff] [blame^] | 1 | page.title=Optimizing Apps for Android 3.0 |
| 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | <ol> |
| 8 | <li><a href="#Setup">Set Up Your SDK with Android 3.0</a></li> |
| 9 | <li><a href="#SearchableConfiguration">Optimize Your App for Tablets and Similar Devices</a></li> |
| 10 | <li><a href="#SearchableActivity">Upgrade or Develop a New App for Tablets and Similar |
| 11 | Devices</a></li> |
| 12 | </ol> |
| 13 | |
| 14 | </div> |
| 15 | </div> |
| 16 | |
| 17 | <p>If you're developing an Android application, Android 3.0 introduces several features that allow |
| 18 | you to enhance your user's experience on tablets and similar devices. Any application you've already |
| 19 | published is compatible with devices running Android 3.0, by default, because Android applications |
| 20 | are forward-compatible. However, there are some simple changes you should make to optimize your |
| 21 | application for tablet-type devices.</p> |
| 22 | |
| 23 | <p>This document shows how you can optimize your existing application for Android 3.0 and |
| 24 | maintain compatibility with older versions or upgrade your application completely with new APIs.</p> |
| 25 | |
| 26 | |
| 27 | <p><b>To get started:</b></p> |
| 28 | |
| 29 | <ol> |
| 30 | <li><a href="#Setup">Set up your SDK with Android 3.0</a>.</li> |
| 31 | <li>Then choose to either optimize or upgrade: |
| 32 | <ol type="a"> |
| 33 | <li><a href="#Optimize">Optimize Your App for Tablets and Similar Devices</a>. |
| 34 | <p>When you have an existing application and want to maintain compatibility with |
| 35 | older versions of Android.</p> |
| 36 | </li> |
| 37 | <li><a href="#Upgrade">Upgrade or Develop a New App for Tablets and Similar Devices</a>. |
| 38 | <p>When you want to upgrade your application to use APIs introduced in Android 3.0 or |
| 39 | create a new application targeted to tablets and similar devices.</p></li> |
| 40 | </ol> |
| 41 | </li> |
| 42 | </ol> |
| 43 | |
| 44 | |
| 45 | <h2 id="Setup">Set Up Your SDK with Android 3.0</h2> |
| 46 | |
| 47 | <p>To start testing and developing your application on Android 3.0, set up your existing Android |
| 48 | SDK with the new platform:</p> |
| 49 | |
| 50 | <p>(If you don't have an existing Android SDK, <a href="{@docRoot}sdk/index.html">download the |
| 51 | SDK starter package now</a>.)</p> |
| 52 | |
| 53 | <ol> |
| 54 | <li><a href="{@docRoot}sdk/adding-components.html#launching">Launch the Android SDK and AVD |
| 55 | Manager</a> and install the following: |
| 56 | <ul> |
| 57 | <li>SDK Platform Android 3.0</li> |
| 58 | <li>Android SDK Tools, revision 10</li> |
| 59 | <li>Android SDK Platform-tools, revision 3</li> |
| 60 | <li>Documentation for Android SDK, API 11</li> |
| 61 | <li>Samples for SDK API 11</li> |
| 62 | </ul> |
| 63 | </li> |
| 64 | <li><a href="{@docRoot}guide/developing/other-ide.html#AVD">Create an AVD</a> for a tablet-type |
| 65 | device: |
| 66 | <p>Set the target to "Android 3.0" and the skin to "WXGA" (the default skin).</p></li> |
| 67 | </ol> |
| 68 | |
| 69 | |
| 70 | <h3>About emulator performance</h3> |
| 71 | |
| 72 | <p>Because the Android emulator must simulate the ARM instruction set on your computer |
| 73 | and the WXGA screen is significantly larger than a typical virtual device, emulator performance is |
| 74 | much slower than a real device.</p> |
| 75 | |
| 76 | <p>In particular, initializing the emulator can be slow and can take several minutes, depending on |
| 77 | your hardware. When the emulator is booting, there is limited user feedback, so please be patient |
| 78 | and wait until you see the home screen (or lock screen) appear. </p> |
| 79 | |
| 80 | <p>However, you don't need to boot the emulator each time you rebuild your |
| 81 | application—typically you only need to boot at the start of a session and keep it running. |
| 82 | Also see the tip below for information about using a snapshot to drastically reduce startup time |
| 83 | after the first initialization. </p> |
| 84 | |
| 85 | <p>We're working hard to resolve the performance issues and it will improve in future tools |
| 86 | releases. For the time being, the emulator is still best way to evaluate your application's |
| 87 | appearance and functionality on Android 3.0 without a real device.</p> |
| 88 | |
| 89 | <p class="note"><strong>Tip:</strong> To improve the startup time for the emulator, enable snapshots |
| 90 | for the AVD when you create it with the SDK and AVD Manager (there's a checkbox in the AVD creator |
| 91 | to <strong>Enable</strong> snapshots). Then, start the AVD from the AVD manager and check <b>Launch |
| 92 | from snapshot</b> and <b>Save to snapshot</b>. This way, when you close the emulator, a snapshot of |
| 93 | the AVD state is saved and used to quickly relaunch the AVD next time. However, when you choose to |
| 94 | save a snapshot, the emulator will be slow to close, so you might want to disable <b>Save to |
| 95 | snapshot</b> after you've acquired an initial snapshot (after you close the AVD for the first |
| 96 | time).</p> |
| 97 | |
| 98 | |
| 99 | |
| 100 | <h2 id="Optimize">Optimize Your Application for Tablets and Similar Devices</h2> |
| 101 | |
| 102 | <p>If you've already developed an application for an earlier version of Android, there are a few |
| 103 | things you can do to optimize it for a tablet-style experience on Android 3.0 without changing the |
| 104 | minimum version required (you don't need to change your manifest's <a |
| 105 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code |
| 106 | android:minSdkVersion}</a>).</p> |
| 107 | |
| 108 | <p class="note"><strong>Note:</strong> All Android applications are forward-compatible, so |
| 109 | there's nothing you <em>have to</em> do—if your application is a good citizen of the Android |
| 110 | APIs, your app should work fine on devices running Android 3.0. However, in order to provide users |
| 111 | a better experience when using your app on an Android 3.0 tablet or similar-size device, you |
| 112 | should update your application to inherit the new system theme and provide some optimizations for |
| 113 | larger screens.</p> |
| 114 | |
| 115 | <p>Here are a few things you can do to optimize your application for devices running Android |
| 116 | 3.0:</p> |
| 117 | |
| 118 | <ol> |
| 119 | <li><b>Test your current application on Android 3.0</b> |
| 120 | <ol> |
| 121 | <li>Build your application as-is and install it on your Android 3.0 AVD (created above during |
| 122 | <a href="#Setup">setup</a>).</li> |
| 123 | <li>Perform your usual tests to be sure everything works and looks as expected.</li> |
| 124 | </ol> |
| 125 | </li> |
| 126 | |
| 127 | <li><b>Apply the new "holographic" theme to your application</b> |
| 128 | <ol> |
| 129 | <li>Open your manifest file and update the <a |
| 130 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code <uses-sdk>}</a> element to |
| 131 | set <a |
| 132 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code |
| 133 | android:targetSdkVersion}</a> to {@code "11"}. For example: |
| 134 | <pre> |
| 135 | <manifest ... > |
| 136 | <uses-sdk android:minSdkVersion="4" |
| 137 | android:targetSdkVersion="11" /> |
| 138 | <application ... > |
| 139 | ... |
| 140 | <application> |
| 141 | </manifest> |
| 142 | </pre> |
| 143 | <p>By targeting the Android 3.0 platform, the system automatically applies the holographic theme |
| 144 | to each activity when your application runs on an Android 3.0 device. The holographic theme |
| 145 | provides a new design for widgets, such as buttons and text boxes, and restyles other |
| 146 | visual elements. This is the standard theme in applications built for Android 3.0, so your |
| 147 | application will look more at home by enabling the theme.</p> |
| 148 | <p>Additionally, the holographic theme enables the <a |
| 149 | href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> in your activities when running on an |
| 150 | Android 3.0 device. The Action Bar replaces the traditional title bar at the top of the activity |
| 151 | window and provides the user access to the activity's Options Menu.</p> |
| 152 | </li> |
| 153 | <li>Continue to build your application against the minimum version specified by <a |
| 154 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>, |
| 155 | but install it on the Android 3.0 AVD. Repeat your tests to be sure that your user interface works |
| 156 | well with the holographic theme. |
| 157 | <p class="note"><strong>Note:</strong> If you have applied other themes directly to your |
| 158 | activities, they will override the inherited holographic theme. To resolve this, you can use |
| 159 | the <a href="{@docRoot}guide/topics/resources/providing-resources.html#VersionQualifier">system |
| 160 | version qualifier</a> to provide an alternative theme for Android 3.0 devices that's based on the |
| 161 | holographic theme. For more information, read how to <a |
| 162 | href="{@docRoot}guide/topics/ui/themes.html#SelectATheme">select a theme based on platform |
| 163 | version</a>.</p> |
| 164 | </ol> |
| 165 | </li> |
| 166 | |
| 167 | <li><b>Supply alternative layout resources for xlarge screens</b> |
| 168 | <p>By providing <a |
| 169 | href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">alternative |
| 170 | resources</a> when running on extra large screens (using the <code>xlarge</code> resource |
| 171 | qualifier), you can improve the user experience of your application on tablet-type devices without |
| 172 | using new APIs.</p> |
| 173 | <p>For example, here are some things to consider when creating a new layout for extra large |
| 174 | screens:</p> |
| 175 | <ul> |
| 176 | <li>Landscape layout: The "normal" orientation for tablet-type devices is usually landscape |
| 177 | (wide), so you should be sure that your activities offer a layout that's optimized for a wide |
| 178 | viewing area. <p>You can specify landscape resources with the <code>land</code> resource |
| 179 | qualifier, but if you want alternative resources for an extra large landscape screen, you |
| 180 | should use both <code>xlarge</code> and <code>land</code> qualifiers. For example, {@code |
| 181 | res/layout-xlarge-land/}. The order of the qualifier names is important; see <a |
| 182 | href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources"> |
| 183 | Providing Alternative Resources</a> for more information.</p></li> |
| 184 | <li>Button position: Consider whether the position of the most common buttons in your UI are |
| 185 | easily accessible while holding a tablet with two hands.</li> |
| 186 | <li>Font sizes: Be sure your application uses {@code sp} units when setting font |
| 187 | sizes. This alone should ensure a readable experience on tablet-style devices. In some cases, |
| 188 | however, you might want to consider larger font sizes for <code>xlarge</code> configurations.</li> |
| 189 | </ul> |
| 190 | <p>In general, always be sure that your application follows the <a |
| 191 | href="{@docRoot}guide/practices/screens_support.html#screen-independence">Best Practices |
| 192 | for Screen Independence</a>.</p> |
| 193 | </li> |
| 194 | </ol> |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | <h2 id="Upgrade">Upgrade or Develop a New App for Tablets and Similar Devices</h2> |
| 201 | |
| 202 | <p>If you want to develop an application that's fully enhanced for tablet-type devices running |
| 203 | Android 3.0, then you need to use new APIs in Android 3.0. This section introduces some of |
| 204 | the new features you should use.</p> |
| 205 | |
| 206 | |
| 207 | <h3>Declare the minimum system version</h3> |
| 208 | |
| 209 | <p>The first thing to do when you create a project for Android 3.0 is set your manifest's <a |
| 210 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a> |
| 211 | to {@code "11"}. For example:</p> |
| 212 | |
| 213 | <pre> |
| 214 | <manifest ... > |
| 215 | <uses-sdk android:minSdkVersion="11" /> |
| 216 | <application ... > |
| 217 | ... |
| 218 | <application> |
| 219 | </manifest> |
| 220 | </pre> |
| 221 | |
| 222 | <p>By targeting the Android 3.0 platform, the system automatically applies the new holographic theme |
| 223 | to each of your activities.</p> |
| 224 | |
| 225 | <p>Additionally, the holographic theme enables the Action Bar for each activity.</p> |
| 226 | |
| 227 | |
| 228 | <h3>Use the Action Bar</h3> |
| 229 | |
| 230 | <p>The Action Bar is a widget for activities that replaces the traditional title bar at the top of |
| 231 | the screen. By default, the Action Bar includes the application logo on the left side, followed by |
| 232 | the activity title, and any available items from the Options Menu on the right side.</p> |
| 233 | |
| 234 | <p>You can enable items from your activity's Options Menu to appear directly in the Action Bar as |
| 235 | "action items" by adding {@code showAsAction="ifRoom"} to specific items in your <a |
| 236 | href="{@docRoot}guide/topics/resources/menu-resource.html">menu resource</a>. You can also add |
| 237 | navigation features to the Action Bar, such as tabs, and use the application icon to navigate to |
| 238 | your application's "home" activity or "up" the activity hierarchy.</p> |
| 239 | |
| 240 | <p>For more information, read <a href="{@docRoot}guide/topics/ui/actionbar.html">Using the |
| 241 | Action Bar</a>.</p> |
| 242 | |
| 243 | |
| 244 | |
| 245 | <h3>Divide your activities into fragments</h3> |
| 246 | |
| 247 | <p>A fragment represents a behavior or a portion of user interface in an activity. You can combine |
| 248 | multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple |
| 249 | activities. You can think of a fragment as a modular section of an activity, which has its own |
| 250 | lifecycle, receives its own input events, and which you can add or remove while the activity is |
| 251 | running.</p> |
| 252 | |
| 253 | <p>For example, a news application can use one fragment to show a list of articles on the left and |
| 254 | another fragment to display an article on the right—both fragments appear in one activity, |
| 255 | side by side, and each fragment has its own set of lifecycle callback methods and handles its own |
| 256 | input events. Thus, instead of using one activity to select an article and another activity to |
| 257 | read the article, the user can select an article and read it all within the same activity.</p> |
| 258 | |
| 259 | <p>For more information, read the <a |
| 260 | href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> document.</p> |
| 261 | |
| 262 | |
| 263 | <h3>Use new animation APIs for transitions</h3> |
| 264 | |
| 265 | <p>An all new flexible animation framework allows you to animate arbitrary properties of any object |
| 266 | (View, Drawable, Fragment, Object, or anything else). You can define several animation aspects |
| 267 | (such as duration, repeat, interpolation, and more) for an object's int, float, and hexadecimal |
| 268 | color values, by default. That is, when an object has a property field for one of these types, you |
| 269 | can change its value over time to affect an animation.</p> |
| 270 | |
| 271 | <p>The {@link android.view.View} class also provides new APIs that leverage the new animation |
| 272 | framework, allowing you to easily apply 2D and 3D transformations to views in your activity layout. |
| 273 | New transformations are made possible with a set of object properties that define the view's layout |
| 274 | position, orientation, transparency and more.</p> |
| 275 | |
| 276 | <p>For more information, read the <a |
| 277 | href="{@docRoot}guide/topics/graphics/animation.html">Property Animation</a> document.</p> |
| 278 | |
| 279 | |
| 280 | <h3>Enable hardware acceleration</h3> |
| 281 | |
| 282 | <p>You can now enable the OpenGL renderer for your application by setting {@code |
| 283 | android:hardwareAccelerated="true"} in your manifest's <a |
| 284 | href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> |
| 285 | element or for individual <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code |
| 286 | <activity>}</a> elements. Hardware acceleration results in smoother animations, smoother |
| 287 | scrolling, and overall better performance and response to user interaction. When enabled, be sure |
| 288 | that you thoroughly test your application on a device that supports hardware acceleration.</p> |
| 289 | |
| 290 | |
| 291 | <h3>Enhance your app widgets</h3> |
| 292 | |
| 293 | <p>App widgets allow users to access information from your application directly from the Home |
| 294 | screen and interact with ongoing services (such as preview their email and control music playback). |
| 295 | Android 3.0 enhances these capabilities by enabling collections, created with widgets such as |
| 296 | {@link android.widget.ListView}, {@link android.widget.GridView}, and the new {@link |
| 297 | android.widget.StackView}. These widgets allow you to create more interactive app |
| 298 | widgets, such as one with a scrolling list, and can automatically update their data through a {@link |
| 299 | android.widget.RemoteViewsService}.</p> |
| 300 | |
| 301 | <p>Additionally, you should create a preview image of your app widget using the Widget Preview |
| 302 | application (pre-installed in an Android 3.0 AVD) and reference it with the {@link |
| 303 | android.appwidget.AppWidgetProviderInfo#previewImage android:previewImage} attribute, so that users |
| 304 | can see what the app widget looks like before adding it to their Home screen.</p> |
| 305 | |
| 306 | |
| 307 | <h3>Add other new features</h3> |
| 308 | |
| 309 | <p>Android 3.0 introduces many more APIs that you might find valuable for your |
| 310 | application, such as drag and drop APIs, new Bluetooth APIs, a system-wide clipboard framework, a |
| 311 | new graphics engine called Renderscript, and more.</p> |
| 312 | |
| 313 | <p>To learn more about the APIs mentioned above and more, see the <a |
| 314 | href="{@docRoot}sdk/android-3.0.html">Android 3.0 Platform</a> document.</p> |
| 315 | |
| 316 | |
| 317 | <h3>Publish your app for extra large screens</h3> |
| 318 | |
| 319 | <p>You should also decide whether your application is <em>only</em> for |
| 320 | tablet-type devices (specifically, <em>xlarge</em> devices) or for all types of screen sizes.</p> |
| 321 | |
| 322 | <p>If you want your application to be available to all screen sizes (for example, for all |
| 323 | phones and tablets), there's nothing you need to do. By default, an application with <a |
| 324 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code |
| 325 | android:minSdkVersion}</a> set to {@code "4"} or higher will resize to fit any screen size.</p> |
| 326 | |
| 327 | <p>If your application is <em>only</em> for <em>xlarge</em> screens, include the <a |
| 328 | href="{@docRoot}guide/topics/manifest/supports-screens-element.html">{@code |
| 329 | <supports-screens>}</a> element in your manifest and declare that the application supports |
| 330 | only <em>xlarge</em> screens, by declaring all other sizes {@code "false"}. For example:</p> |
| 331 | |
| 332 | <pre> |
| 333 | <manifest ... > |
| 334 | ... |
| 335 | <supports-screens android:smallScreens="false" |
| 336 | android:normalScreens="false" |
| 337 | android:largeScreens="false" |
| 338 | android:xlargeScreens="true" /> |
| 339 | <application ... > |
| 340 | ... |
| 341 | <application> |
| 342 | </manifest> |
| 343 | </pre> |
| 344 | |
| 345 | <p>With this declaration, you indicate that your application does not support any screen size except |
| 346 | extra large. External services such as Android Market may then use this information to filter your |
| 347 | application from devices that do not have an extra large screen.</p> |
| 348 | |
| 349 | |
| 350 | |
| 351 | <h3>Look at some samples</h3> |
| 352 | |
| 353 | <p>Many of the new features and APIs that are described in the <a |
| 354 | href="{@docRoot}sdk/android-3.0.html#api">Android 3.0 Platform Preview</a> also have accompanying |
| 355 | samples that can help you understand how to use them. To get the samples, download them from the SDK |
| 356 | repository using the Android SDK Manager. After downloading the samples ("Samples for SDK API 11"), |
| 357 | you can find them in <code><sdk_root>/samples/android-11/</code>. The links below can help you |
| 358 | find samples for the features you are interested in:</p> |
| 359 | |
| 360 | <ul> |
| 361 | <li><a href="{@docRoot}resources/samples/HoneycombGallery/index.html">Honeycomb Gallery</a>: |
| 362 | Demonstrates many new APIs in Android 3.0, including fragments, the action bar, drag and drop, and |
| 363 | animations.</li> |
| 364 | <li><a |
| 365 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Fragment"> |
| 366 | Fragments</a>: Various samples that demonstrate fragment layouts, back stack, restoring state, and |
| 367 | more.</li> |
| 368 | <li><a |
| 369 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/ActionBarMechanics.html" |
| 370 | >Action Bar</a>: Samples that demonstrate various Action Bar features, such as tabs, logos, and |
| 371 | action items.</li> |
| 372 | <li><a |
| 373 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/content/ClipboardSample. |
| 374 | html">Clipboard</a>: An example of how to use the clipboard for copy and paste operations.</li> |
| 375 | <li><a |
| 376 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/DragAndDropDemo.html"> |
| 377 | Drag and Drop</a>: An example of how to perform drag and drop with new View events.</li> |
| 378 | <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/List15.html"> |
| 379 | Multi-choice List</a>: An example of how to provide multiple-choice selection for ListView and |
| 380 | GridView.</li> |
| 381 | <li><a |
| 382 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html"> |
| 383 | Content Loaders</a>: An example using new Loader APIs to asynchronously load data.</li> |
| 384 | <li><a |
| 385 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/animation/index.html"> |
| 386 | Property Animation</a>: Several samples using the new animation APIs to animate object |
| 387 | properties.</li> |
| 388 | <li><a |
| 389 | href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/SearchViewActionBar. |
| 390 | html">Search View Widget</a>: Example using the new search widget in the Action Bar (as an |
| 391 | "action view").</li> |
| 392 | <li><a |
| 393 | href="{@docRoot}resources/samples/Renderscript/index.html">Renderscript</a>: Contains several |
| 394 | different applications that demonstrate using renderscript APIs for computations and 3D |
| 395 | graphics.</li> |
| 396 | </ul> |
| 397 | |