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