blob: d4669e50d993d6ebe3512359418166c448aadb71 [file] [log] [blame]
Scott Main28fb09e2011-05-23 17:27:17 -07001page.title=Compatibility Library
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>
11 <li><a href="#Installing">Installing the Compatibility Library</a></li>
12 <li><a href="#SettingUp">Setting Up a Project to Use the Library</a></li>
13 <li><a href="#Using">Using Some of the Library APIs</a></li>
14 <li><a href="#Samples">Samples</a></li>
15</ol>
16
17<h2>See also</h2>
18<ol>
19 <li><a
20href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing Apps for Android 3.0</a></li>
21 <li><a href="http://code.google.com/p/iosched/">Google I/O App source code</a></li>
22</ol>
23
24</div>
25</div>
26
27<p><em>Minimum API level supported:</em> <b>4</b></p>
28
29<p>The Compatibility Library is a static library you can add to your Android application in order to
30use APIs not available in older versions of the Android platform. The primary goal of the library is
31to provide APIs introduced in Andriod 3.0 for older versions of Android so that all applications can
32use them.</p>
33
34<p>If you're not able to use APIs introduced in Android 3.0 directly, because you want to remain
35backward-compatible, the Compatibility Library provides your application access to self-contained
36versions of some of the latest APIs that you can use with older versions of Android. Most
37importantly, the library provides implementations of the {@link android.app.Fragment} and {@link
38android.content.Loader} APIs, so you can use them in a way that's compatible with devices running
39Android 1.6 (API level 4) and higher. Thus, you can more easily create a single APK that supports a
40majority of devices and provide larger devices (such as tablets) a fully optimized experience by
41using <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> in your activity
42design.</p>
43
44
45<h2 id="Notes">Revisions</h2>
46
47<p>The sections below provide notes about successive releases of
48the Compatibility Library, as denoted by revision number.</p>
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" />
55 Compatibility Library, revision 2 (May 2011)
56 </a></p>
57
58 <div class="toggle-content-toggleme" style="padding-left:2em">
59 <dl>
60 <dt>Changes:</dt>
61 <dd>
62 <ul>
63 <li>Support for fragment animations.</li>
64 <li>Fix {@code Fragment.onActivityResult()} bug.</li>
65 </ul>
66 </dd>
67 </dl>
68 </div>
69
70</div>
71
72
73<div class="toggle-content closed">
74
75 <p><a href="#" onclick="return toggleContent(this)">
76 <img src="{@docRoot}assets/images/triangle-closed.png" class="toggle-content-img" />
77 Compatibility Library, revision 1 (March 2011)
78 </a></p>
79
80 <div class="toggle-content-toggleme" style="padding-left:2em">
81 <p>Initial release of the library.</p>
82 </div>
83
84</div>
85
86
87
88<h2 id="Installing">Installing the Compatibility Library</h2>
89
90<p>The Compatibility Library is provided as a downloadable package from the Android SDK and AVD
91Manager. To install the library:</p>
92
93<ol>
94 <li>Launch the SDK and AVD Manager.
95 <p>From Eclipse, you can select <strong>Window</strong>
96&gt; <strong>Android SDK and AVD Manager</strong>. Or, launch {@code SDK Manager.exe} from
97the {@code &lt;sdk&gt;/} directory (on Windows only) or {@code android} from the {@code
98&lt;sdk&gt;/tools/} directory.</p></li>
99 <li>Expand the Android Repository, check <strong>Android Compatibility package</strong>
100and click <strong>Install selected</strong>.</li>
101 <li>Proceed to install the package.</li>
102</ol>
103
104<p>When done, all files (including source code, samples, and the {@code .jar} file) are saved
105into the <code>&lt;sdk&gt;/extras/android/compatibility/</code> directory. The next directory
106name is {@code v4}, which indicates the lowest compatible version for the library within. That
107is, the code in {@code v4/} supports API level 4 and above. (There may be future libraries that
108have a different minimum version, so they will be saved alongside this one.)</p>
109
110
111<h2 id="SettingUp">Setting Up a Project to Use the Library</h2>
112
113<p>To add the Compatibility Library to your Android project:</p>
114<ol>
115 <li>In your Android project, create a directory named {@code libs} at the root of your
116project (next to {@code src/}, {@code res/}, etc.)</li>
117 <li>Navigate to {@code &lt;sdk&gt;/extras/android/compatibility/v4/}.</li>
118 <li>Copy the {@code android-support-v4.jar} file into your project {@code libs/} directory.</li>
119 <li>Add the JAR to your project build path. In Eclipse, right-click the JAR file in the
120Package Explorer, select <strong>Build Path</strong> &gt; <strong>Add to Build Path</strong>.
121You should then see the JAR file appear in a new directory called Referenced Libraries.</li>
122</ol>
123
124<p>Your application is now ready to use fragments, loaders and other APIs from the library. All the
125provided APIs are in the {@code android.support.v4} package.</p>
126
127<p class="warning"><strong>Warning:</strong> Be certain that you not confuse the standard
128{@code android} packages with those in {@code android.support.v4}. Some code completion tools might
129get this wrong, especially if you're building against recent versions of the platform. To be safe,
130keep your build target set to the same version as you have defined for your <a
131href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code android:minSdkVersion}</a>
132and double check the import statements for classes that are duplicated in the Compatibility
133Library, such as {@code SimpleCursorAdapter}.</p>
134
135
136<h2 id="Using">Using Some of the Library APIs</h2>
137
138<p>The Compatibility Library provides access to several classes introduced with Android 3.0, plus
139some updated version of existing classes. Some of the most useful and notable classes in the
140library are:</p>
141
142<ul>
143 <li>{@link android.app.Fragment}</li>
144 <li>{@link android.app.FragmentManager}</li>
145 <li>{@link android.app.FragmentTransaction}</li>
146 <li>{@link android.app.ListFragment}</li>
147 <li>{@link android.app.DialogFragment}</li>
148 <li>{@link android.app.LoaderManager}</li>
149 <li>{@link android.content.Loader}</li>
150 <li>{@link android.content.AsyncTaskLoader}</li>
151 <li>{@link android.content.CursorLoader}</li>
152</ul>
153
154<p>For each of the classes above (and others not listed), the APIs work almost exactly the same
155as the counterparts in the latest version of the Android platform. Thus, you can usually refer to
156the latest reference documentation for information about the supported APIs. There are some
157differences, however. Most notably:</p>
158
159<ul>
160 <li>When creating an activity to use fragments, you must declare your activity to extend the
161{@code FragmentActivity} class (instead of the traditional {@link android.app.Activity}
162class).</li>
163 <li>To manage your fragments and loaders, you must use the methods {@code
164FragmentActivity.getSupportFragmentManager()} and {@code
165FragmentActivity.getSupportLoaderManager()} (instead of the {@link
166android.app.Activity#getFragmentManager()} and {@link android.app.Activity#getLoaderManager()}
167methods).</li>
168 <li>The {@link android.app.ActionBar} is <strong>not supported</strong> by the library.
169However, when creating your <a href="{@docRoot}guide/topics/ui/menus.html#options-menu">Options
170Menu</a>, you can declare which items should be added to the Action Bar when it's available (on
171Android 3.0 or later). You can do so with the {@code MenuCompat.setShowAsAction()} method. For
172example:
173<pre>
174public boolean onCreateOptionsMenu(Menu menu) {
175 MenuInflater inflater = getMenuInflater();
176 inflater.inflate(R.menu.options, menu);
177 MenuCompat.setShowAsAction(menu.findItem(R.id.action_search), 1);
178 return true;
179}
180</pre>
181</li>
182</ul>
183
184<p>The Compatibility Library currently does not provide reference documentation for the included
185APIs. To generate your own set, using the {@code javadoc} tool, perform the
186following from a command line:</p>
187
188<pre class="no-pretty-print">
189cd &lt;sdk&gt;/extras/android/compatibility/v4/
190mkdir docs
191javadoc -sourcepath src/java/ -subpackages android.support.v4 -d docs
192</pre>
193<p>Open the {@code docs/index.html} file to begin browsing the generated documentation.</p>
194
195
196<div class="note"><p><strong>Tip:</strong> To enable the Holographic theme on devices
197running Android 3.0 or higher, declare in your manifest file that your application targets
198API level 11. For example:</p>
199<pre>
200&lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /&gt;
201</pre>
202<p>This way, your application automatically receives the Holographic theme and the Action Bar for
203each activity when running on Android 3.0 and higher.</p>
204</div>
205
206<p>For more information about how you can optimize your application for the latest
207Android-powered devices, read <a href="{@docRoot}guide/practices/optimizing-for-3.0.html">Optimizing
208Apps for Android 3.0</a>.</p>
209
210
211<h2 id="Samples">Samples</h2>
212
213<p>If you want to see some sample code that uses the Compatibility Library, take a look at the
214<a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/support/index.html">API
215Demos</a> sample code that's included with the Samples package you can download from the AVD and SDK
216Manager.</p>
217
218<p>Additionally, the <a href="http://code.google.com/p/iosched/">Google I/O App</a> is a complete
219application that uses the library to provide a single APK for both handsets and tablets and also
220demonstrates some of Android's best practices in Android UI design.</p>
221
222
223
224