1 page.title=Supporting Different Platform Versions 2 page.metaDescription=Training on how to declare support for minimum and target API levels. 3 parent.title=Supporting Different Devices 4 parent.link=index.html 5 6 trainingnavtop=true 7 previous.title=Supporting Different Screens 8 previous.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 27 href="{@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 33 to support older versions of Android until more devices get updated. This 34 lesson shows you how to take advantage of the latest APIs while continuing to support older 35 versions as well.</p> 36 37 <p>The dashboard for <a 38 href="http://developer.android.com/about/dashboards/index.html">Platform Versions</a> 39 is updated regularly to show the distribution of active 40 devices running each version of Android, based on the number of devices that visit the Google Play 41 Store. Generally, its a good practice to support about 90% of the active devices, while 42 targeting your app to the latest version.</p> 43 44 <p class="note"><strong>Tip:</strong> In order to provide the best features and 45 functionality across several Android versions, you should use the <a 46 href="{@docRoot}tools/support-library/index.html">Android Support Library</a> in your app, 47 which 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 54 describes details about your app and 55 identifies which versions of Android it supports. Specifically, the <code>minSdkVersion</code> 56 and <code>targetSdkVersion</code> attributes for the <a 57 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code <uses-sdk}</a> element 58 identify the lowest API level with which your app is compatible and the highest API level against 59 which youve designed and tested your app.</p> 60 61 <p>For example:</p> 62 63 <pre> 64 <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > 65 <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /> 66 ... 67 </manifest> 68 </pre> 69 70 <p>As new versions of Android are released, some style and behaviors may change. 71 To allow your app to take advantage of these changes and ensure that your app fits the style of 72 each user's device, you should set the 73 <a 74 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> 75 value to match the latest Android version 76 available.</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} 83 constants class. Use these codes within your app to build conditions that ensure the code that 84 depends on higher API levels is executed only when those APIs are available on the system.</p> 85 86 <pre> 87 private 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 99 attributes that arent supported by the current device. So you can safely use XML attributes that 100 are only supported by newer versions without worrying about older versions breaking when they 101 encounter that code. For example, if you set the 102 <code>targetSdkVersion="11"</code>, your app includes the {@link android.app.ActionBar} by default 103 on 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 105 in 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 107 version 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 114 underlying operating system. These themes can be applied to your app within the 115 manifest file. By using these built in styles and themes, your app will 116 naturally 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><activity android:theme="@android:style/Theme.Dialog"></pre> 121 122 <p>To make your activity have a transparent background:</p> 123 124 <pre><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><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> 131 attribute 132 to the <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code 133 <application>}</a> element:</p> 134 135 <pre><application android:theme="@style/CustomTheme"></pre> 136 137 <p>For more about creating and using themes, read the <a 138 href="{@docRoot}guide/topics/ui/themes.html">Styles and Themes</a> guide.</p> 139 140