Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 1 | page.title=Compatibility Package |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 2 | |
| 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | |
| 8 | <h2>In this document</h2> |
| 9 | <ol> |
| 10 | <li><a href="#Notes">Revisions</a></li> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 11 | <li><a href="#Downloading">Downloading the Compatibility Package</a></li> |
| 12 | <li><a href="#SettingUp">Setting Up a Project to Use a Library</a></li> |
| 13 | <li><a href="#Using">Using the v4 Library APIs</a></li> |
| 14 | <li><a href="#Docs">Reference Docs</a></li> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 15 | <li><a href="#Samples">Samples</a></li> |
| 16 | </ol> |
| 17 | |
| 18 | <h2>See also</h2> |
| 19 | <ol> |
| 20 | <li><a |
| 21 | href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing Apps for Android 3.0</a></li> |
| 22 | <li><a href="http://code.google.com/p/iosched/">Google I/O App source code</a></li> |
| 23 | </ol> |
| 24 | |
| 25 | </div> |
| 26 | </div> |
| 27 | |
| 28 | <p><em>Minimum API level supported:</em> <b>4</b></p> |
| 29 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 30 | <p>The Compatibility Package includes static libraries you can add to your Android |
| 31 | application in order to use APIs that are either not available for older platform versions or that |
| 32 | offer "utility" APIs that aren't a part of the framework APIs. The goal is to simplify your |
| 33 | development by offering even more APIs that you can bundle with your application so you can |
| 34 | worry less about platform versions.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 35 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 36 | <p class="note"><strong>Note:</strong> The Compatibility Package includes more than one library. |
| 37 | Each one has a different <em>minimum API level</em>. For example, one library requires API |
| 38 | level 4 or higher, while another requires API level 13 or higher. The minimum version is |
| 39 | indicated by the directory name, such as {@code v4/} and {@code v13/}. You can use more than one |
| 40 | library in your application as long as your <a |
| 41 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a> |
| 42 | meets the requirements of each library.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | <h2 id="Notes">Revisions</h2> |
| 46 | |
| 47 | <p>The sections below provide notes about successive releases of |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 48 | the Compatibility Package, as denoted by revision number.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | <div class="toggle-content open"> |
| 52 | |
| 53 | <p><a href="#" onclick="return toggleContent(this)"> |
| 54 | <img src="{@docRoot}assets/images/triangle-opened.png" class="toggle-content-img" /> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 55 | Compatibility Package, revision 3 (July 2011) |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 56 | </a></p> |
| 57 | |
| 58 | <div class="toggle-content-toggleme" style="padding-left:2em"> |
| 59 | <dl> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 60 | <dt>Changes for v4 support library:</dt> |
| 61 | <dd> |
| 62 | <ul> |
| 63 | <li>Adds support for {@link android.app.Fragment.SavedState}</li> |
| 64 | <li>Adds {@code MotionEventCompat} to support newer {@link |
| 65 | android.view.MotionEvent} APIs</li> |
| 66 | <li>Adds {@code VelocityTrackerCompat} to support a newer {@link |
| 67 | android.view.VelocityTracker} APIs</li> |
| 68 | <li>Adds {@code ViewConfigurationCompat} to support a newer {@link |
| 69 | android.view.ViewConfiguration} APIs</li> |
| 70 | <li>All new APIs (available only in the support library) that allow you to create UIs |
| 71 | with horizontal paging (with or without tabs at the top). Classes to support this include: |
| 72 | <ul> |
| 73 | <li>{@code ViewPager}: A {@link android.view.ViewGroup} that manages the |
| 74 | layout for the child views, which the user can swipe between.</li> |
| 75 | <li>{@code PagerAdapter}: An adapter that populates the {@code ViewPager} with the |
| 76 | views that represent each page.</li> |
| 77 | <li>{@code FragmentPagerAdapter}: An extension of {@code PagerAdapter} for flipping |
| 78 | between fragments.</li> |
| 79 | <li>{@code FragmentPagerAdapter}: An extension of {@code PagerAdapter} for flipping |
| 80 | between fragments that uses the library's support for {@link android.app.Fragment.SavedState}.</li> |
| 81 | </ul> |
| 82 | </li> |
| 83 | </ul> |
| 84 | </dd> |
| 85 | <dt>New v13 support library:</dt> |
| 86 | <dd> |
| 87 | <ul> |
| 88 | <li>Includes the {@code FragmentPagerAdapter} and {@code FragmentStatePagerAdapter} |
| 89 | to support the horizontal paging. |
| 90 | <p>These are exactly the same as the APIs added to the v4 support library, but rely on |
| 91 | other platform components in Android 3.2. Use this library instead of v4 if you're developing for |
| 92 | Android 3.2 and higher (all other APIs in the v4 library are already available with API level |
| 93 | 13).</p> |
| 94 | </li> |
| 95 | </ul> |
| 96 | </dd> |
| 97 | </dl> |
| 98 | </div> |
| 99 | |
| 100 | </div> |
| 101 | |
| 102 | |
| 103 | <div class="toggle-content closed"> |
| 104 | |
| 105 | <p><a href="#" onclick="return toggleContent(this)"> |
| 106 | <img src="{@docRoot}assets/images/triangle-closed.png" class="toggle-content-img" /> |
| 107 | Compatibility Package, revision 2 (May 2011) |
| 108 | </a></p> |
| 109 | |
| 110 | <div class="toggle-content-toggleme" style="padding-left:2em"> |
| 111 | <dl> |
| 112 | <dt>Changes for v4 library:</dt> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 113 | <dd> |
| 114 | <ul> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 115 | <li>Support for fragment animations</li> |
| 116 | <li>Fix {@code Fragment.onActivityResult()} bug</li> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 117 | </ul> |
| 118 | </dd> |
| 119 | </dl> |
| 120 | </div> |
| 121 | |
| 122 | </div> |
| 123 | |
| 124 | |
| 125 | <div class="toggle-content closed"> |
| 126 | |
| 127 | <p><a href="#" onclick="return toggleContent(this)"> |
| 128 | <img src="{@docRoot}assets/images/triangle-closed.png" class="toggle-content-img" /> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 129 | Compatibility Package, revision 1 (March 2011) |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 130 | </a></p> |
| 131 | |
| 132 | <div class="toggle-content-toggleme" style="padding-left:2em"> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 133 | <p>Initial release with the v4 library.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 134 | </div> |
| 135 | |
| 136 | </div> |
| 137 | |
| 138 | |
| 139 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 140 | <h2 id="Downloading">Downloading the Compatibility Package</h2> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 141 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 142 | <p>The Compatibility Package is provided as a downloadable package from the Android SDK and AVD |
| 143 | Manager. To install:</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 144 | |
| 145 | <ol> |
| 146 | <li>Launch the SDK and AVD Manager. |
| 147 | <p>From Eclipse, you can select <strong>Window</strong> |
| 148 | > <strong>Android SDK and AVD Manager</strong>. Or, launch {@code SDK Manager.exe} from |
| 149 | the {@code <sdk>/} directory (on Windows only) or {@code android} from the {@code |
| 150 | <sdk>/tools/} directory.</p></li> |
| 151 | <li>Expand the Android Repository, check <strong>Android Compatibility package</strong> |
| 152 | and click <strong>Install selected</strong>.</li> |
| 153 | <li>Proceed to install the package.</li> |
| 154 | </ol> |
| 155 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 156 | <p>When done, all files (including source code, samples, and the {@code .jar} files) are saved |
| 157 | into the <code><sdk>/extras/android/compatibility/</code> directory. This directory contains |
| 158 | each of the different support libraries, such as the library for API level 4 and up and the library |
| 159 | for API level 13 and up, each named with the respective version (such as {@code v4/}).</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 160 | |
| 161 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 162 | <h2 id="SettingUp">Setting Up a Project to Use a Library</h2> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 163 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 164 | <p>To add one of the libraries to your Android project:</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 165 | <ol> |
| 166 | <li>In your Android project, create a directory named {@code libs} at the root of your |
| 167 | project (next to {@code src/}, {@code res/}, etc.)</li> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 168 | <li>Locate the JAR file for the library you want to use and copy it into the {@code |
| 169 | libs/} directory. |
| 170 | <p>For example, the library that supports API level 4 and up is located at {@code |
| 171 | <sdk>/extras/android/compatibility/v4/android-support-v4.jar}.</p> |
| 172 | </li> |
| 173 | <li>Add the JAR to your project build path. |
| 174 | <p>In Eclipse, right-click the JAR file in the Package Explorer, select <strong>Build |
| 175 | Path</strong> > <strong>Add to Build Path</strong>. You should then see the JAR file appear in a |
| 176 | new directory called Referenced Libraries.</p> |
| 177 | </li> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 178 | </ol> |
| 179 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 180 | <p>Your application is now ready to use the library APIs. All the |
| 181 | provided APIs are available in the {@code android.support} package (for |
| 182 | example, {@code android.support.v4}).</p> |
| 183 | |
| 184 | <p class="note"><strong>Tip:</strong> To see the library APIs in action, take a look at the sample |
| 185 | apps in {@code extras/android/compatibility/<version>/samples/}.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 186 | |
| 187 | <p class="warning"><strong>Warning:</strong> Be certain that you not confuse the standard |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 188 | {@code android} packages with those in {@code android.support} library. Some code completion tools |
| 189 | might |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 190 | get this wrong, especially if you're building against recent versions of the platform. To be safe, |
| 191 | keep your build target set to the same version as you have defined for your <a |
| 192 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a> |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 193 | and double check the import statements for classes that also exist in the support library, such as |
| 194 | {@code SimpleCursorAdapter}.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 195 | |
| 196 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 197 | <h2 id="Using">Using the v4 Library APIs</h2> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 198 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 199 | <p>The support library for v4 provides access to several classes introduced with Android 3.0 and |
| 200 | beyond, plus some updated version of existing classes, and even some APIs that currently don't |
| 201 | exist in the Android platform. Some of the most useful and notable classes that have |
| 202 | counterparts in the v4 support library are:</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 203 | |
| 204 | <ul> |
| 205 | <li>{@link android.app.Fragment}</li> |
| 206 | <li>{@link android.app.FragmentManager}</li> |
| 207 | <li>{@link android.app.FragmentTransaction}</li> |
| 208 | <li>{@link android.app.ListFragment}</li> |
| 209 | <li>{@link android.app.DialogFragment}</li> |
| 210 | <li>{@link android.app.LoaderManager}</li> |
| 211 | <li>{@link android.content.Loader}</li> |
| 212 | <li>{@link android.content.AsyncTaskLoader}</li> |
| 213 | <li>{@link android.content.CursorLoader}</li> |
| 214 | </ul> |
| 215 | |
| 216 | <p>For each of the classes above (and others not listed), the APIs work almost exactly the same |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 217 | as the counterparts in the latest Android platform. Thus, you can usually refer to |
| 218 | the online documentation for information about the supported APIs. There are some |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 219 | differences, however. Most notably:</p> |
| 220 | |
| 221 | <ul> |
| 222 | <li>When creating an activity to use fragments, you must declare your activity to extend the |
| 223 | {@code FragmentActivity} class (instead of the traditional {@link android.app.Activity} |
| 224 | class).</li> |
| 225 | <li>To manage your fragments and loaders, you must use the methods {@code |
| 226 | FragmentActivity.getSupportFragmentManager()} and {@code |
| 227 | FragmentActivity.getSupportLoaderManager()} (instead of the {@link |
| 228 | android.app.Activity#getFragmentManager()} and {@link android.app.Activity#getLoaderManager()} |
| 229 | methods).</li> |
| 230 | <li>The {@link android.app.ActionBar} is <strong>not supported</strong> by the library. |
| 231 | However, when creating your <a href="{@docRoot}guide/topics/ui/menus.html#options-menu">Options |
| 232 | Menu</a>, you can declare which items should be added to the Action Bar when it's available (on |
| 233 | Android 3.0 or later). You can do so with the {@code MenuCompat.setShowAsAction()} method. For |
| 234 | example: |
| 235 | <pre> |
| 236 | public boolean onCreateOptionsMenu(Menu menu) { |
| 237 | MenuInflater inflater = getMenuInflater(); |
| 238 | inflater.inflate(R.menu.options, menu); |
| 239 | MenuCompat.setShowAsAction(menu.findItem(R.id.action_search), 1); |
| 240 | return true; |
| 241 | } |
| 242 | </pre> |
| 243 | </li> |
| 244 | </ul> |
| 245 | |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 246 | <div class="note"><p><strong>Tip:</strong> To enable the Holographic theme on devices |
| 247 | running Android 3.0 or higher, declare in your manifest file that your application targets |
| 248 | API level 11. For example:</p> |
| 249 | <pre> |
| 250 | <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /> |
| 251 | </pre> |
| 252 | <p>This way, your application automatically receives the Holographic theme and the Action Bar for |
| 253 | each activity when running on Android 3.0 and higher.</p> |
| 254 | </div> |
| 255 | |
| 256 | <p>For more information about how you can optimize your application for the latest |
| 257 | Android-powered devices, read <a href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing |
| 258 | Apps for Android 3.0</a>.</p> |
| 259 | |
| 260 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 261 | <h2 id="Docs">Reference Docs</h2> |
| 262 | |
| 263 | <p>The libraries currently do not provide reference documentation for the included APIs. To generate |
| 264 | your own set using the {@code javadoc} tool, perform the following from a command (as appropriate |
| 265 | for the library version you're using). In this example, documentation is generated for the v4 |
| 266 | library:</p> |
| 267 | |
| 268 | <pre class="no-pretty-print"> |
| 269 | cd <sdk>/extras/android/compatibility/v4/ |
| 270 | mkdir docs |
| 271 | javadoc -sourcepath src/java/ -subpackages android.support.v4 -d docs |
| 272 | </pre> |
| 273 | <p>Open the {@code docs/index.html} file to begin browsing the generated documentation.</p> |
| 274 | |
| 275 | |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 276 | <h2 id="Samples">Samples</h2> |
| 277 | |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 278 | <p>If you want to see some code that uses the support libraries, samples are included with the |
| 279 | Compatibility Package, inside each support library directory. For example, at {@code |
| 280 | extras/android/compatibility/v4/samples/}.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 281 | |
| 282 | <p>Additionally, the <a href="http://code.google.com/p/iosched/">Google I/O App</a> is a complete |
Scott Main | aecaf93 | 2011-07-14 10:01:42 -0700 | [diff] [blame^] | 283 | application that uses the v4 support library to provide a single APK for both handsets and tablets |
| 284 | and also demonstrates some of Android's best practices in Android UI design.</p> |
Scott Main | 28fb09e | 2011-05-23 17:27:17 -0700 | [diff] [blame] | 285 | |
| 286 | |
| 287 | |
| 288 | |