summaryrefslogtreecommitdiffstats
path: root/docs/html/training/basics/supporting-devices/platforms.jd
blob: 60aaf6a517e47002ca6e46d53836e989306c4376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
page.title=Supporting Different Platform Versions
page.metaDescription=Training on how to declare support for minimum and target API levels.
parent.title=Supporting Different Devices
parent.link=index.html

trainingnavtop=true
previous.title=Supporting Different Screens
previous.link=screens.html

@jd:body


<div id="tb-wrapper">
  <div id="tb">
    
    <h2>This lesson teaches you to</h2>
    <ol>
      <li><a href="#sdk-versions">Specify Minimum and Target API Levels</a></li>
      <li><a href="#version-codes">Check System Version at Runtime</a></li>
      <li><a href="#style-themes">Use Platform Styles and Themes</a></li>
    </ol>
    
    <h2>You should also read</h2>
    <ul>
      <li><a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">Android API Levels</a></li>
      <li><a
href="{@docRoot}tools/support-library/index.html">Android Support Library</a></li>
    </ul>
  </div>
</div>

<p>While the latest versions of Android often provide great APIs for your app, you should continue 
to support older versions of Android until more devices get updated. This 
lesson shows you how to take advantage of the latest APIs while continuing to support older 
versions as well.</p>

<p>The dashboard for <a
href="http://developer.android.com/about/dashboards/index.html">Platform Versions</a>
is updated regularly to show the distribution of active 
devices running each version of Android, based on the number of devices that visit the Google Play 
Store.  Generally, it’s a good practice to support about 90% of the active devices, while 
targeting your app to the latest version.</p>

<p class="note"><strong>Tip:</strong> In order to provide the best features and 
functionality across several Android versions, you should use the <a
href="{@docRoot}tools/support-library/index.html">Android Support Library</a> in your app,
which allows you to use several recent platform APIs on older versions.</p>



<h2 id="sdk-versions">Specify Minimum and Target API Levels</h2>

<p>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a> file
describes details about your app and 
identifies which versions of Android it supports.   Specifically, the <code>minSdkVersion</code> 
and <code>targetSdkVersion</code> attributes for the <a
href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code &lt;uses-sdk}</a> element
identify the lowest API level with which your app is compatible and the highest API level against
which you’ve designed and tested your app.</p>

<p>For example:</p>

<pre>
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
    &lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
    ...
&lt;/manifest>
</pre>

<p>As new versions of Android are released, some style and behaviors may change. 
To allow your app to take advantage of these changes and ensure that your app fits the style of
each user's device, you should set the 
<a
href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
value to match the latest Android version
available.</p>



<h2 id="version-codes">Check System Version at Runtime</h2>

<p>Android provides a unique code for each platform version in the {@link android.os.Build}
constants class. Use these codes within your app to build conditions that ensure the code that
depends on higher API levels is executed only when those APIs are available on the system.</p>

<pre>
private void setUpActionBar() {
    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
</pre>



<p class="note"><strong>Note:</strong> When parsing XML resources, Android ignores XML 
attributes that aren’t supported by the current device. So you can safely use XML attributes that
are only supported by newer versions without worrying about older versions breaking when they
encounter that code. For example, if you set the 
<code>targetSdkVersion="11"</code>, your app includes the {@link android.app.ActionBar} by default
on Android 3.0 and higher. To then add menu items to the action bar, you need to set 
<code>android:showAsAction="ifRoom"</code> in your menu resource XML. It's safe to do this 
in a cross-version XML file, because the older versions of Android simply ignore the 
<code>showAsAction</code> attribute (that is, you <em>do not</em> need a separate 
version in <code>res/menu-v11/</code>).</p>



<h2 id="style-themes">Use Platform Styles and Themes</h2> 

<p>Android provides user experience themes that give apps the look and feel of the 
underlying operating system.  These themes can be applied to your app within the 
manifest file.  By using these built in styles and themes, your app will 
naturally follow the latest look and feel of Android with each new release.</p>

<p>To make your activity look like a dialog box:</p>

<pre>&lt;activity android:theme="@android:style/Theme.Dialog"></pre>

<p>To make your activity have a transparent background:</p>

<pre>&lt;activity android:theme="@android:style/Theme.Translucent"></pre>

<p>To apply your own custom theme defined in <code>/res/values/styles.xml</code>:</p>

<pre>&lt;activity android:theme="@style/CustomTheme"></pre>

<p>To apply a theme to your entire app (all activities), add the <code>android:theme</code>
attribute 
to the <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code
&lt;application>}</a> element:</p>

<pre>&lt;application android:theme="@style/CustomTheme"></pre>

<p>For more about creating and using themes, read the <a
href="{@docRoot}guide/topics/ui/themes.html">Styles and Themes</a> guide.</p>