blob: a22e69af2076a36b391c6ab2ce4bd28ec3974131 [file] [log] [blame]
Scott Mainadaf5a72011-02-16 15:52:38 -08001page.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
11Devices</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
18you to enhance your user's experience on tablets and similar devices. Any application you've already
19published is compatible with devices running Android 3.0, by default, because Android applications
20are forward-compatible. However, there are some simple changes you should make to optimize your
21application for tablet-type devices.</p>
22
23<p>This document shows how you can optimize your existing application for Android 3.0 and
24maintain 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
35older 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
48SDK 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
51SDK starter package now</a>.)</p>
52
53<ol>
54 <li><a href="{@docRoot}sdk/adding-components.html#launching">Launch the Android SDK and AVD
55Manager</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
65device:
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
73and the WXGA screen is significantly larger than a typical virtual device, emulator performance is
74much slower than a real device.</p>
75
76<p>In particular, initializing the emulator can be slow and can take several minutes, depending on
77your hardware. When the emulator is booting, there is limited user feedback, so please be patient
78and 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
81application&mdash;typically you only need to boot at the start of a session and keep it running.
82Also see the tip below for information about using a snapshot to drastically reduce startup time
83after the first initialization. </p>
84
85<p>We're working hard to resolve the performance issues and it will improve in future tools
86releases. For the time being, the emulator is still best way to evaluate your application's
87appearance 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
90for the AVD when you create it with the SDK and AVD Manager (there's a checkbox in the AVD creator
91to <strong>Enable</strong> snapshots). Then, start the AVD from the AVD manager and check <b>Launch
92from snapshot</b> and <b>Save to snapshot</b>. This way, when you close the emulator, a snapshot of
93the AVD state is saved and used to quickly relaunch the AVD next time. However, when you choose to
94save a snapshot, the emulator will be slow to close, so you might want to disable <b>Save to
95snapshot</b> after you've acquired an initial snapshot (after you close the AVD for the first
96time).</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
103things you can do to optimize it for a tablet-style experience on Android 3.0 without changing the
104minimum version required (you don't need to change your manifest's <a
105href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
106android:minSdkVersion}</a>).</p>
107
108<p class="note"><strong>Note:</strong> All Android applications are forward-compatible, so
109there's nothing you <em>have to</em> do&mdash;if your application is a good citizen of the Android
110APIs, your app should work fine on devices running Android 3.0. However, in order to provide users
111a better experience when using your app on an Android 3.0 tablet or similar-size device, you
112should update your application to inherit the new system theme and provide some optimizations for
113larger screens.</p>
114
115<p>Here are a few things you can do to optimize your application for devices running Android
1163.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
130href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code &lt;uses-sdk&gt;}</a> element to
131set <a
132href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code
133android:targetSdkVersion}</a> to {@code "11"}. For example:
134<pre>
135&lt;manifest ... >
136 &lt;uses-sdk android:minSdkVersion="4"
137 android:targetSdkVersion="11" /&gt;
138 &lt;application ... >
139 ...
140 &lt;application>
141&lt;/manifest>
142</pre>
143 <p>By targeting the Android 3.0 platform, the system automatically applies the holographic theme
144to each activity when your application runs on an Android 3.0 device. The holographic theme
145provides a new design for widgets, such as buttons and text boxes, and restyles other
146visual elements. This is the standard theme in applications built for Android 3.0, so your
147application will look more at home by enabling the theme.</p>
148 <p>Additionally, the holographic theme enables the <a
149href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> in your activities when running on an
150Android 3.0 device. The Action Bar replaces the traditional title bar at the top of the activity
151window 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
154href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>,
155but install it on the Android 3.0 AVD. Repeat your tests to be sure that your user interface works
156well with the holographic theme.
157 <p class="note"><strong>Note:</strong> If you have applied other themes directly to your
158activities, they will override the inherited holographic theme. To resolve this, you can use
159the <a href="{@docRoot}guide/topics/resources/providing-resources.html#VersionQualifier">system
160version qualifier</a> to provide an alternative theme for Android 3.0 devices that's based on the
161holographic theme. For more information, read how to <a
162href="{@docRoot}guide/topics/ui/themes.html#SelectATheme">select a theme based on platform
163version</a>.</p>
164 </ol>
165 </li>
166
167 <li><b>Supply alternative layout resources for xlarge screens</b>
168 <p>By providing <a
169href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">alternative
170resources</a> when running on extra large screens (using the <code>xlarge</code> resource
171qualifier), you can improve the user experience of your application on tablet-type devices without
172using new APIs.</p>
173 <p>For example, here are some things to consider when creating a new layout for extra large
174screens:</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
178viewing area. <p>You can specify landscape resources with the <code>land</code> resource
179qualifier, but if you want alternative resources for an extra large landscape screen, you
180should use both <code>xlarge</code> and <code>land</code> qualifiers. For example, {@code
181res/layout-xlarge-land/}. The order of the qualifier names is important; see <a
182href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">
183Providing 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
185easily accessible while holding a tablet with two hands.</li>
186 <li>Font sizes: Be sure your application uses {@code sp} units when setting font
187sizes. This alone should ensure a readable experience on tablet-style devices. In some cases,
188however, 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
191href="{@docRoot}guide/practices/screens_support.html#screen-independence">Best Practices
192for 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
203Android 3.0, then you need to use new APIs in Android 3.0. This section introduces some of
204the 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
210href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>
211to {@code "11"}. For example:</p>
212
213<pre>
214&lt;manifest ... >
215 &lt;uses-sdk android:minSdkVersion="11" /&gt;
216 &lt;application ... >
217 ...
218 &lt;application>
219&lt;/manifest>
220</pre>
221
222<p>By targeting the Android 3.0 platform, the system automatically applies the new holographic theme
223to 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
231the screen. By default, the Action Bar includes the application logo on the left side, followed by
232the 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
236href="{@docRoot}guide/topics/resources/menu-resource.html">menu resource</a>. You can also add
237navigation features to the Action Bar, such as tabs, and use the application icon to navigate to
238your 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
241Action 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
248multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple
249activities. You can think of a fragment as a modular section of an activity, which has its own
250lifecycle, receives its own input events, and which you can add or remove while the activity is
251running.</p>
252
253<p>For example, a news application can use one fragment to show a list of articles on the left and
254another fragment to display an article on the right&mdash;both fragments appear in one activity,
255side by side, and each fragment has its own set of lifecycle callback methods and handles its own
256input events. Thus, instead of using one activity to select an article and another activity to
257read 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
260href="{@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
268color values, by default. That is, when an object has a property field for one of these types, you
269can 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
272framework, allowing you to easily apply 2D and 3D transformations to views in your activity layout.
273New transformations are made possible with a set of object properties that define the view's layout
274position, orientation, transparency and more.</p>
275
276<p>For more information, read the <a
277href="{@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
283android:hardwareAccelerated="true"} in your manifest's <a
284href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application&gt;}</a>
285element or for individual <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
286&lt;activity&gt;}</a> elements. Hardware acceleration results in smoother animations, smoother
287scrolling, and overall better performance and response to user interaction. When enabled, be sure
288that 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
294screen and interact with ongoing services (such as preview their email and control music playback).
295Android 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
297android.widget.StackView}. These widgets allow you to create more interactive app
298widgets, such as one with a scrolling list, and can automatically update their data through a {@link
299android.widget.RemoteViewsService}.</p>
300
301<p>Additionally, you should create a preview image of your app widget using the Widget Preview
302application (pre-installed in an Android 3.0 AVD) and reference it with the {@link
303android.appwidget.AppWidgetProviderInfo#previewImage android:previewImage} attribute, so that users
304can 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
310application, such as drag and drop APIs, new Bluetooth APIs, a system-wide clipboard framework, a
311new graphics engine called Renderscript, and more.</p>
312
313<p>To learn more about the APIs mentioned above and more, see the <a
314href="{@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
320tablet-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
323phones and tablets), there's nothing you need to do. By default, an application with <a
324href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code
325android: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
328href="{@docRoot}guide/topics/manifest/supports-screens-element.html">{@code
329&lt;supports-screens&gt;}</a> element in your manifest and declare that the application supports
330only <em>xlarge</em> screens, by declaring all other sizes {@code "false"}. For example:</p>
331
332<pre>
333&lt;manifest ... >
334 ...
335 &lt;supports-screens android:smallScreens="false"
336 android:normalScreens="false"
337 android:largeScreens="false"
338 android:xlargeScreens="true" /&gt;
339 &lt;application ... >
340 ...
341 &lt;application>
342&lt;/manifest>
343</pre>
344
345<p>With this declaration, you indicate that your application does not support any screen size except
346extra large. External services such as Android Market may then use this information to filter your
347application 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
354href="{@docRoot}sdk/android-3.0.html#api">Android 3.0 Platform Preview</a> also have accompanying
355samples that can help you understand how to use them. To get the samples, download them from the SDK
356repository using the Android SDK Manager. After downloading the samples ("Samples for SDK API 11"),
357you can find them in <code>&lt;sdk_root&gt;/samples/android-11/</code>. The links below can help you
358find 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>:
362Demonstrates many new APIs in Android 3.0, including fragments, the action bar, drag and drop, and
363animations.</li>
364 <li><a
365href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Fragment">
366Fragments</a>: Various samples that demonstrate fragment layouts, back stack, restoring state, and
367more.</li>
368 <li><a
369href="{@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
371action items.</li>
372 <li><a
373href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/content/ClipboardSample.
374html">Clipboard</a>: An example of how to use the clipboard for copy and paste operations.</li>
375 <li><a
376href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/DragAndDropDemo.html">
377Drag 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">
379Multi-choice List</a>: An example of how to provide multiple-choice selection for ListView and
380GridView.</li>
381 <li><a
382href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html">
383Content Loaders</a>: An example using new Loader APIs to asynchronously load data.</li>
384 <li><a
385href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/animation/index.html">
386Property Animation</a>: Several samples using the new animation APIs to animate object
387properties.</li>
388 <li><a
389href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/SearchViewActionBar.
390html">Search View Widget</a>: Example using the new search widget in the Action Bar (as an
391"action view").</li>
392 <li><a
393href="{@docRoot}resources/samples/Renderscript/index.html">Renderscript</a>: Contains several
394different applications that demonstrate using renderscript APIs for computations and 3D
395graphics.</li>
396</ul>
397