• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Supporting Different Platform Versions
2page.metaDescription=Training on how to declare support for minimum and target API levels.
3parent.title=Supporting Different Devices
4parent.link=index.html
5
6trainingnavtop=true
7previous.title=Supporting Different Screens
8previous.link=screens.html
9
10@jd:body
11
12
13<div id="tb-wrapper">
14  <div id="tb">
15
16    <h2>This lesson teaches you to</h2>
17    <ol>
18      <li><a href="#sdk-versions">Specify Minimum and Target API Levels</a></li>
19      <li><a href="#version-codes">Check System Version at Runtime</a></li>
20      <li><a href="#style-themes">Use Platform Styles and Themes</a></li>
21    </ol>
22
23    <h2>You should also read</h2>
24    <ul>
25      <li><a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">Android API Levels</a></li>
26      <li><a
27href="{@docRoot}tools/support-library/index.html">Android Support Library</a></li>
28    </ul>
29  </div>
30</div>
31
32<p>While the latest versions of Android often provide great APIs for your app, you should continue
33to support older versions of Android until more devices get updated. This
34lesson shows you how to take advantage of the latest APIs while continuing to support older
35versions as well.</p>
36
37<p>The dashboard for <a
38href="http://developer.android.com/about/dashboards/index.html">Platform Versions</a>
39is updated regularly to show the distribution of active
40devices running each version of Android, based on the number of devices that visit the Google Play
41Store.  Generally, it’s a good practice to support about 90% of the active devices, while
42targeting your app to the latest version.</p>
43
44<p class="note"><strong>Tip:</strong> In order to provide the best features and
45functionality across several Android versions, you should use the <a
46href="{@docRoot}tools/support-library/index.html">Android Support Library</a> in your app,
47which allows you to use several recent platform APIs on older versions.</p>
48
49
50
51<h2 id="sdk-versions">Specify Minimum and Target API Levels</h2>
52
53<p>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a> file
54describes details about your app and
55identifies which versions of Android it supports.   Specifically, the <code>minSdkVersion</code>
56and <code>targetSdkVersion</code> attributes for the <a
57href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code &lt;uses-sdk}</a> element
58identify the lowest API level with which your app is compatible and the highest API level against
59which you’ve designed and tested your app.</p>
60
61<p>For example:</p>
62
63<pre>
64&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
65    &lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
66    ...
67&lt;/manifest>
68</pre>
69
70<p>As new versions of Android are released, some style and behaviors may change.
71To allow your app to take advantage of these changes and ensure that your app fits the style of
72each user's device, you should set the
73<a
74href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
75value to match the latest Android version
76available.</p>
77
78
79
80<h2 id="version-codes">Check System Version at Runtime</h2>
81
82<p>Android provides a unique code for each platform version in the {@link android.os.Build}
83constants class. Use these codes within your app to build conditions that ensure the code that
84depends on higher API levels is executed only when those APIs are available on the system.</p>
85
86<pre>
87private void setUpActionBar() {
88    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
89    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
90        ActionBar actionBar = getActionBar();
91        actionBar.setDisplayHomeAsUpEnabled(true);
92    }
93}
94</pre>
95
96
97
98<p class="note"><strong>Note:</strong> When parsing XML resources, Android ignores XML
99attributes that aren’t supported by the current device. So you can safely use XML attributes that
100are only supported by newer versions without worrying about older versions breaking when they
101encounter that code. For example, if you set the
102<code>targetSdkVersion="11"</code>, your app includes the {@link android.app.ActionBar} by default
103on Android 3.0 and higher. To then add menu items to the action bar, you need to set
104<code>android:showAsAction="ifRoom"</code> in your menu resource XML. It's safe to do this
105in a cross-version XML file, because the older versions of Android simply ignore the
106<code>showAsAction</code> attribute (that is, you <em>do not</em> need a separate
107version in <code>res/menu-v11/</code>).</p>
108
109
110
111<h2 id="style-themes">Use Platform Styles and Themes</h2>
112
113<p>Android provides user experience themes that give apps the look and feel of the
114underlying operating system.  These themes can be applied to your app within the
115manifest file.  By using these built in styles and themes, your app will
116naturally follow the latest look and feel of Android with each new release.</p>
117
118<p>To make your activity look like a dialog box:</p>
119
120<pre>&lt;activity android:theme="@android:style/Theme.Dialog"></pre>
121
122<p>To make your activity have a transparent background:</p>
123
124<pre>&lt;activity android:theme="@android:style/Theme.Translucent"></pre>
125
126<p>To apply your own custom theme defined in <code>/res/values/styles.xml</code>:</p>
127
128<pre>&lt;activity android:theme="@style/CustomTheme"></pre>
129
130<p>To apply a theme to your entire app (all activities), add the <code>android:theme</code>
131attribute
132to the <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code
133&lt;application>}</a> element:</p>
134
135<pre>&lt;application android:theme="@style/CustomTheme"></pre>
136
137<p>For more about creating and using themes, read the <a
138href="{@docRoot}guide/topics/ui/themes.html">Styles and Themes</a> guide.</p>
139
140