blob: 080eede2edf5395324f445c2c9536aac68f9e246 [file] [log] [blame]
Scott Mainaecaf932011-07-14 10:01:42 -07001page.title=Compatibility Package
Scott Main28fb09e2011-05-23 17:27:17 -07002
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 Mainaecaf932011-07-14 10:01:42 -070011 <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 Main28fb09e2011-05-23 17:27:17 -070015 <li><a href="#Samples">Samples</a></li>
16</ol>
17
18<h2>See also</h2>
19<ol>
20 <li><a
21href="{@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 Mainaecaf932011-07-14 10:01:42 -070030<p>The Compatibility Package includes static libraries you can add to your Android
31application in order to use APIs that are either not available for older platform versions or that
32offer "utility" APIs that aren't a part of the framework APIs. The goal is to simplify your
33development by offering even more APIs that you can bundle with your application so you can
34worry less about platform versions.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070035
Scott Mainaecaf932011-07-14 10:01:42 -070036<p class="note"><strong>Note:</strong> The Compatibility Package includes more than one library.
37Each one has a different <em>minimum API level</em>. For example, one library requires API
38level 4 or higher, while another requires API level 13 or higher. The minimum version is
39indicated by the directory name, such as {@code v4/} and {@code v13/}. You can use more than one
40library in your application as long as your <a
41href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>
42meets the requirements of each library.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070043
44
45<h2 id="Notes">Revisions</h2>
46
47<p>The sections below provide notes about successive releases of
Scott Mainaecaf932011-07-14 10:01:42 -070048the Compatibility Package, as denoted by revision number.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070049
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 Mainaecaf932011-07-14 10:01:42 -070055 Compatibility Package, revision 3 (July 2011)
Scott Main28fb09e2011-05-23 17:27:17 -070056 </a></p>
57
58 <div class="toggle-content-toggleme" style="padding-left:2em">
59 <dl>
Scott Mainaecaf932011-07-14 10:01:42 -070060 <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
65android.view.MotionEvent} APIs</li>
66 <li>Adds {@code VelocityTrackerCompat} to support a newer {@link
67android.view.VelocityTracker} APIs</li>
68 <li>Adds {@code ViewConfigurationCompat} to support a newer {@link
69android.view.ViewConfiguration} APIs</li>
70 <li>All new APIs (available only in the support library) that allow you to create UIs
71with 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
74layout for the child views, which the user can swipe between.</li>
75 <li>{@code PagerAdapter}: An adapter that populates the {@code ViewPager} with the
76views that represent each page.</li>
77 <li>{@code FragmentPagerAdapter}: An extension of {@code PagerAdapter} for flipping
78between fragments.</li>
79 <li>{@code FragmentPagerAdapter}: An extension of {@code PagerAdapter} for flipping
80between 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}
89to support the horizontal paging.
90 <p>These are exactly the same as the APIs added to the v4 support library, but rely on
91other platform components in Android 3.2. Use this library instead of v4 if you're developing for
92Android 3.2 and higher (all other APIs in the v4 library are already available with API level
9313).</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 Main28fb09e2011-05-23 17:27:17 -0700113 <dd>
114 <ul>
Scott Mainaecaf932011-07-14 10:01:42 -0700115 <li>Support for fragment animations</li>
116 <li>Fix {@code Fragment.onActivityResult()} bug</li>
Scott Main28fb09e2011-05-23 17:27:17 -0700117 </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 Mainaecaf932011-07-14 10:01:42 -0700129 Compatibility Package, revision 1 (March 2011)
Scott Main28fb09e2011-05-23 17:27:17 -0700130 </a></p>
131
132 <div class="toggle-content-toggleme" style="padding-left:2em">
Scott Mainaecaf932011-07-14 10:01:42 -0700133 <p>Initial release with the v4 library.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700134 </div>
135
136</div>
137
138
139
Scott Mainaecaf932011-07-14 10:01:42 -0700140<h2 id="Downloading">Downloading the Compatibility Package</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700141
Scott Mainaecaf932011-07-14 10:01:42 -0700142<p>The Compatibility Package is provided as a downloadable package from the Android SDK and AVD
143Manager. To install:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700144
145<ol>
146 <li>Launch the SDK and AVD Manager.
147 <p>From Eclipse, you can select <strong>Window</strong>
148&gt; <strong>Android SDK and AVD Manager</strong>. Or, launch {@code SDK Manager.exe} from
149the {@code &lt;sdk&gt;/} directory (on Windows only) or {@code android} from the {@code
150&lt;sdk&gt;/tools/} directory.</p></li>
151 <li>Expand the Android Repository, check <strong>Android Compatibility package</strong>
152and click <strong>Install selected</strong>.</li>
153 <li>Proceed to install the package.</li>
154</ol>
155
Scott Mainaecaf932011-07-14 10:01:42 -0700156<p>When done, all files (including source code, samples, and the {@code .jar} files) are saved
157into the <code>&lt;sdk&gt;/extras/android/compatibility/</code> directory. This directory contains
158each of the different support libraries, such as the library for API level 4 and up and the library
159for API level 13 and up, each named with the respective version (such as {@code v4/}).</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700160
161
Scott Mainaecaf932011-07-14 10:01:42 -0700162<h2 id="SettingUp">Setting Up a Project to Use a Library</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700163
Scott Mainaecaf932011-07-14 10:01:42 -0700164<p>To add one of the libraries to your Android project:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700165<ol>
166 <li>In your Android project, create a directory named {@code libs} at the root of your
167project (next to {@code src/}, {@code res/}, etc.)</li>
Scott Mainaecaf932011-07-14 10:01:42 -0700168 <li>Locate the JAR file for the library you want to use and copy it into the {@code
169libs/} directory.
170 <p>For example, the library that supports API level 4 and up is located at {@code
171&lt;sdk&gt;/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
175Path</strong> &gt; <strong>Add to Build Path</strong>. You should then see the JAR file appear in a
176new directory called Referenced Libraries.</p>
177 </li>
Scott Main28fb09e2011-05-23 17:27:17 -0700178</ol>
179
Scott Mainaecaf932011-07-14 10:01:42 -0700180<p>Your application is now ready to use the library APIs. All the
181provided APIs are available in the {@code android.support} package (for
182example, {@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
185apps in {@code extras/android/compatibility/&lt;version&gt;/samples/}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700186
187<p class="warning"><strong>Warning:</strong> Be certain that you not confuse the standard
Scott Mainaecaf932011-07-14 10:01:42 -0700188{@code android} packages with those in {@code android.support} library. Some code completion tools
189might
Scott Main28fb09e2011-05-23 17:27:17 -0700190get this wrong, especially if you're building against recent versions of the platform. To be safe,
191keep your build target set to the same version as you have defined for your <a
192href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>
Scott Mainaecaf932011-07-14 10:01:42 -0700193and double check the import statements for classes that also exist in the support library, such as
194{@code SimpleCursorAdapter}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700195
196
Scott Mainaecaf932011-07-14 10:01:42 -0700197<h2 id="Using">Using the v4 Library APIs</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700198
Scott Mainaecaf932011-07-14 10:01:42 -0700199<p>The support library for v4 provides access to several classes introduced with Android 3.0 and
200beyond, plus some updated version of existing classes, and even some APIs that currently don't
201exist in the Android platform. Some of the most useful and notable classes that have
202counterparts in the v4 support library are:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700203
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 Mainaecaf932011-07-14 10:01:42 -0700217as the counterparts in the latest Android platform. Thus, you can usually refer to
218the online documentation for information about the supported APIs. There are some
Scott Main28fb09e2011-05-23 17:27:17 -0700219differences, 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}
224class).</li>
225 <li>To manage your fragments and loaders, you must use the methods {@code
226FragmentActivity.getSupportFragmentManager()} and {@code
227FragmentActivity.getSupportLoaderManager()} (instead of the {@link
228android.app.Activity#getFragmentManager()} and {@link android.app.Activity#getLoaderManager()}
229methods).</li>
230 <li>The {@link android.app.ActionBar} is <strong>not supported</strong> by the library.
231However, when creating your <a href="{@docRoot}guide/topics/ui/menus.html#options-menu">Options
232Menu</a>, you can declare which items should be added to the Action Bar when it's available (on
233Android 3.0 or later). You can do so with the {@code MenuCompat.setShowAsAction()} method. For
234example:
235<pre>
236public 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 Main28fb09e2011-05-23 17:27:17 -0700246<div class="note"><p><strong>Tip:</strong> To enable the Holographic theme on devices
247running Android 3.0 or higher, declare in your manifest file that your application targets
248API level 11. For example:</p>
249<pre>
250&lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /&gt;
251</pre>
252<p>This way, your application automatically receives the Holographic theme and the Action Bar for
253each 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
257Android-powered devices, read <a href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing
258Apps for Android 3.0</a>.</p>
259
260
Scott Mainaecaf932011-07-14 10:01:42 -0700261<h2 id="Docs">Reference Docs</h2>
262
263<p>The libraries currently do not provide reference documentation for the included APIs. To generate
264your own set using the {@code javadoc} tool, perform the following from a command (as appropriate
265for the library version you're using). In this example, documentation is generated for the v4
266library:</p>
267
268<pre class="no-pretty-print">
269cd &lt;sdk&gt;/extras/android/compatibility/v4/
270mkdir docs
271javadoc -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 Main28fb09e2011-05-23 17:27:17 -0700276<h2 id="Samples">Samples</h2>
277
Scott Mainaecaf932011-07-14 10:01:42 -0700278<p>If you want to see some code that uses the support libraries, samples are included with the
279Compatibility Package, inside each support library directory. For example, at {@code
280extras/android/compatibility/v4/samples/}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700281
282<p>Additionally, the <a href="http://code.google.com/p/iosched/">Google I/O App</a> is a complete
Scott Mainaecaf932011-07-14 10:01:42 -0700283application that uses the v4 support library to provide a single APK for both handsets and tablets
284and also demonstrates some of Android's best practices in Android UI design.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700285
286
287
288