blob: d949267c1e0fbf474d460249dd061d6b46e7c65c [file] [log] [blame]
Scott Maind9ee0d72012-04-19 15:52:38 -07001page.title=Using the Support Library
Scott Main7d711b12013-03-11 13:52:35 -07002page.tags="support library"
Scott Maind9ee0d72012-04-19 15:52:38 -07003
4trainingnavtop=true
Scott Maind9ee0d72012-04-19 15:52:38 -07005
6@jd:body
7
8<div id="tb-wrapper">
9 <div id="tb">
10 <h2>This lesson teaches you to</h2>
11 <ol>
Katie McCormicke06b0832013-01-03 15:50:40 -080012 <li><a href="#Setup">Set Up Your Project with the Support Library</a></li>
Scott Maind9ee0d72012-04-19 15:52:38 -070013 <li><a href="#Apis">Import the Support Library APIs</a></li>
14 </ol>
15 <h2>You should also read</h2>
16 <ul>
Scott Main4e2c9dc2013-07-23 19:35:17 -070017 <li><a href="{@docRoot}tools/support-library/index.html">Support Library</a></li>
Scott Maind9ee0d72012-04-19 15:52:38 -070018 </ul>
19 </div>
20</div>
21
Scott Main4e2c9dc2013-07-23 19:35:17 -070022<p>The Android <a href="{@docRoot}tools/support-library/index.html">Support Library</a> provides a JAR
Katie McCormicke06b0832013-01-03 15:50:40 -080023file with an API library that allows you to use some of the more recent Android APIs in your app
Scott Maind9ee0d72012-04-19 15:52:38 -070024while running on earlier versions of Android. For instance, the Support Library provides a version
25of the {@link android.app.Fragment} APIs that you can use on Android 1.6 (API level 4) and
26higher.</p>
27
28<p>This lesson shows how to set up your app to use the Support Library in order to use fragments
29to build a dynamic app UI.</p>
30
31
Katie McCormicke06b0832013-01-03 15:50:40 -080032<h2 id="Setup">Set Up Your Project with the Support Library</h2>
Scott Maind9ee0d72012-04-19 15:52:38 -070033
34<div class="figure" style="width:527px">
35<img src="{@docRoot}images/training/basics/sdk-manager.png" alt="" />
36<p class="img-caption"><strong>Figure 1.</strong> The Android SDK Manager with the
37Android Support package selected.</p>
38</div>
39
40<p>To set up your project:</p>
41
42<ol>
Katie McCormicke06b0832013-01-03 15:50:40 -080043 <li>Download the Android Support package using the SDK Manager.</li>
Scott Maind9ee0d72012-04-19 15:52:38 -070044
45 <li>Create a <code>libs</code> directory at the top level of your Android project.</li>
46 <li>Locate the JAR file for the library you want to use and copy it into the <code>libs/</code>
47directory.
48<p>For example, the library that supports API level 4 and up is located at
49<code>&lt;sdk>/extras/android/support/v4/android-support-v4.jar</code>.</p></li>
50 <li>Update your manifest file to set the minimum API level to <code>4</code> and the target
51API level to the latest release:
52 <pre>&lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /></pre>
53 </li>
54</ol>
55
56
57<h2 id="Apis">Import the Support Library APIs</h2>
58
59<p>The Support Library includes a variety of APIs that were either added in recent versions of
60Android or don't exist in the platform at all and merely provide additional support to you when
61developing specific application features.</p>
62
Scott Main71427ba2012-04-20 11:01:21 -070063<p>You can find all the API reference documentation for the Support Library in the
64platform docs at {@link android.support.v4.app android.support.v4.*}.</p>
Scott Maind9ee0d72012-04-19 15:52:38 -070065
66<div class="warning"><p><strong>Warning:</strong> To be sure that you don't accidentally use new
67APIs on an older system version, be certain that you import the {@link
68android.support.v4.app.Fragment} class and related APIs from the {@link android.support.v4.app}
69package:</p>
70<pre>
71import android.support.v4.app.Fragment;
72import android.support.v4.app.FragmentManager;
73...
74</pre>
75</div>
76
77
78<p>When creating an activity that hosts fragments while using the Support Library, you must also
79extend the {@link android.support.v4.app.FragmentActivity} class instead of the traditional {@link
80android.app.Activity} class. You'll see sample code for the fragment and activity in the next
81lesson.</p>
82