blob: c8cd5b24b07f89c140194bb881f44d31db6ac7ac [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 Mainffcbe482011-07-15 09:50:49 -070030<p>The Compatibility Package includes static "support libraries" that you can add to your Android
Scott Mainaecaf932011-07-14 10:01:42 -070031application 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
Scott Mainffcbe482011-07-15 09:50:49 -070033development by offering more APIs that you can bundle with your application so you can
Scott Mainaecaf932011-07-14 10:01:42 -070034worry less about platform versions.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070035
Scott Mainffcbe482011-07-15 09:50:49 -070036<p class="note"><strong>Note:</strong> The Compatibility Package includes more than one support
37library. Each 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 indicated
39by the directory name, such as {@code v4/} and {@code v13/}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070040
41
42<h2 id="Notes">Revisions</h2>
43
44<p>The sections below provide notes about successive releases of
Scott Mainaecaf932011-07-14 10:01:42 -070045the Compatibility Package, as denoted by revision number.</p>
Scott Main28fb09e2011-05-23 17:27:17 -070046
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 Mainaecaf932011-07-14 10:01:42 -070052 Compatibility Package, revision 3 (July 2011)
Scott Main28fb09e2011-05-23 17:27:17 -070053 </a></p>
54
55 <div class="toggle-content-toggleme" style="padding-left:2em">
56 <dl>
Scott Mainaecaf932011-07-14 10:01:42 -070057 <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
62android.view.MotionEvent} APIs</li>
63 <li>Adds {@code VelocityTrackerCompat} to support a newer {@link
64android.view.VelocityTracker} APIs</li>
65 <li>Adds {@code ViewConfigurationCompat} to support a newer {@link
66android.view.ViewConfiguration} APIs</li>
67 <li>All new APIs (available only in the support library) that allow you to create UIs
Scott Mainffcbe482011-07-15 09:50:49 -070068with horizontal paging, allowing users to swipe left and right between content views. Classes to
69support this include:
Scott Mainaecaf932011-07-14 10:01:42 -070070 <ul>
71 <li>{@code ViewPager}: A {@link android.view.ViewGroup} that manages the
72layout for the child views, which the user can swipe between.</li>
73 <li>{@code PagerAdapter}: An adapter that populates the {@code ViewPager} with the
74views that represent each page.</li>
75 <li>{@code FragmentPagerAdapter}: An extension of {@code PagerAdapter} for flipping
76between fragments.</li>
Scott Mainffcbe482011-07-15 09:50:49 -070077 <li>{@code FragmentStatePagerAdapter}: An extension of {@code PagerAdapter} for
78flipping between fragments that uses the library's support for {@link
79android.app.Fragment.SavedState}.</li>
Scott Mainaecaf932011-07-14 10:01:42 -070080 </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}
88to support the horizontal paging.
89 <p>These are exactly the same as the APIs added to the v4 support library, but rely on
90other platform components in Android 3.2. Use this library instead of v4 if you're developing for
91Android 3.2 and higher (all other APIs in the v4 library are already available with API level
9213).</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 Main28fb09e2011-05-23 17:27:17 -0700112 <dd>
113 <ul>
Scott Mainaecaf932011-07-14 10:01:42 -0700114 <li>Support for fragment animations</li>
115 <li>Fix {@code Fragment.onActivityResult()} bug</li>
Scott Main28fb09e2011-05-23 17:27:17 -0700116 </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 Mainaecaf932011-07-14 10:01:42 -0700128 Compatibility Package, revision 1 (March 2011)
Scott Main28fb09e2011-05-23 17:27:17 -0700129 </a></p>
130
131 <div class="toggle-content-toggleme" style="padding-left:2em">
Scott Mainaecaf932011-07-14 10:01:42 -0700132 <p>Initial release with the v4 library.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700133 </div>
134
135</div>
136
137
138
Scott Mainaecaf932011-07-14 10:01:42 -0700139<h2 id="Downloading">Downloading the Compatibility Package</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700140
Scott Mainaecaf932011-07-14 10:01:42 -0700141<p>The Compatibility Package is provided as a downloadable package from the Android SDK and AVD
142Manager. To install:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700143
144<ol>
145 <li>Launch the SDK and AVD Manager.
146 <p>From Eclipse, you can select <strong>Window</strong>
147&gt; <strong>Android SDK and AVD Manager</strong>. Or, launch {@code SDK Manager.exe} from
148the {@code &lt;sdk&gt;/} directory (on Windows only) or {@code android} from the {@code
149&lt;sdk&gt;/tools/} directory.</p></li>
150 <li>Expand the Android Repository, check <strong>Android Compatibility package</strong>
151and click <strong>Install selected</strong>.</li>
152 <li>Proceed to install the package.</li>
153</ol>
154
Scott Mainaecaf932011-07-14 10:01:42 -0700155<p>When done, all files (including source code, samples, and the {@code .jar} files) are saved
156into the <code>&lt;sdk&gt;/extras/android/compatibility/</code> directory. This directory contains
157each of the different support libraries, such as the library for API level 4 and up and the library
158for API level 13 and up, each named with the respective version (such as {@code v4/}).</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700159
160
Scott Mainaecaf932011-07-14 10:01:42 -0700161<h2 id="SettingUp">Setting Up a Project to Use a Library</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700162
Scott Mainaecaf932011-07-14 10:01:42 -0700163<p>To add one of the libraries to your Android project:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700164<ol>
165 <li>In your Android project, create a directory named {@code libs} at the root of your
166project (next to {@code src/}, {@code res/}, etc.)</li>
Scott Mainaecaf932011-07-14 10:01:42 -0700167 <li>Locate the JAR file for the library you want to use and copy it into the {@code
168libs/} directory.
169 <p>For example, the library that supports API level 4 and up is located at {@code
170&lt;sdk&gt;/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
174Path</strong> &gt; <strong>Add to Build Path</strong>. You should then see the JAR file appear in a
175new directory called Referenced Libraries.</p>
176 </li>
Scott Main28fb09e2011-05-23 17:27:17 -0700177</ol>
178
Scott Mainaecaf932011-07-14 10:01:42 -0700179<p>Your application is now ready to use the library APIs. All the
180provided APIs are available in the {@code android.support} package (for
181example, {@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
184apps in {@code extras/android/compatibility/&lt;version&gt;/samples/}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700185
186<p class="warning"><strong>Warning:</strong> Be certain that you not confuse the standard
Scott Mainaecaf932011-07-14 10:01:42 -0700187{@code android} packages with those in {@code android.support} library. Some code completion tools
188might
Scott Main28fb09e2011-05-23 17:27:17 -0700189get this wrong, especially if you're building against recent versions of the platform. To be safe,
190keep your build target set to the same version as you have defined for your <a
191href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>
Scott Mainaecaf932011-07-14 10:01:42 -0700192and double check the import statements for classes that also exist in the support library, such as
193{@code SimpleCursorAdapter}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700194
195
Scott Mainaecaf932011-07-14 10:01:42 -0700196<h2 id="Using">Using the v4 Library APIs</h2>
Scott Main28fb09e2011-05-23 17:27:17 -0700197
Scott Mainaecaf932011-07-14 10:01:42 -0700198<p>The support library for v4 provides access to several classes introduced with Android 3.0 and
199beyond, plus some updated version of existing classes, and even some APIs that currently don't
200exist in the Android platform. Some of the most useful and notable classes that have
201counterparts in the v4 support library are:</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700202
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 Mainaecaf932011-07-14 10:01:42 -0700216as the counterparts in the latest Android platform. Thus, you can usually refer to
217the online documentation for information about the supported APIs. There are some
Scott Main28fb09e2011-05-23 17:27:17 -0700218differences, 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}
223class).</li>
224 <li>To manage your fragments and loaders, you must use the methods {@code
225FragmentActivity.getSupportFragmentManager()} and {@code
226FragmentActivity.getSupportLoaderManager()} (instead of the {@link
227android.app.Activity#getFragmentManager()} and {@link android.app.Activity#getLoaderManager()}
228methods).</li>
229 <li>The {@link android.app.ActionBar} is <strong>not supported</strong> by the library.
230However, when creating your <a href="{@docRoot}guide/topics/ui/menus.html#options-menu">Options
231Menu</a>, you can declare which items should be added to the Action Bar when it's available (on
232Android 3.0 or later). You can do so with the {@code MenuCompat.setShowAsAction()} method. For
233example:
234<pre>
235public 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 Main28fb09e2011-05-23 17:27:17 -0700245<div class="note"><p><strong>Tip:</strong> To enable the Holographic theme on devices
246running Android 3.0 or higher, declare in your manifest file that your application targets
247API level 11. For example:</p>
248<pre>
249&lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /&gt;
250</pre>
251<p>This way, your application automatically receives the Holographic theme and the Action Bar for
252each 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
256Android-powered devices, read <a href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing
257Apps for Android 3.0</a>.</p>
258
259
Scott Mainaecaf932011-07-14 10:01:42 -0700260<h2 id="Docs">Reference Docs</h2>
261
262<p>The libraries currently do not provide reference documentation for the included APIs. To generate
263your own set using the {@code javadoc} tool, perform the following from a command (as appropriate
264for the library version you're using). In this example, documentation is generated for the v4
265library:</p>
266
267<pre class="no-pretty-print">
268cd &lt;sdk&gt;/extras/android/compatibility/v4/
269mkdir docs
270javadoc -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 Main28fb09e2011-05-23 17:27:17 -0700275<h2 id="Samples">Samples</h2>
276
Scott Mainaecaf932011-07-14 10:01:42 -0700277<p>If you want to see some code that uses the support libraries, samples are included with the
278Compatibility Package, inside each support library directory. For example, at {@code
279extras/android/compatibility/v4/samples/}.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700280
281<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 -0700282application that uses the v4 support library to provide a single APK for both handsets and tablets
283and also demonstrates some of Android's best practices in Android UI design.</p>
Scott Main28fb09e2011-05-23 17:27:17 -0700284
285
286
287