Home | History | Annotate | Download | only in fragments
      1 page.title=Using the Support Library
      2 page.tags="support library"
      3 
      4 trainingnavtop=true
      5 
      6 @jd:body
      7 
      8 <div id="tb-wrapper">
      9   <div id="tb"> 
     10     <h2>This lesson teaches you to</h2>
     11     <ol>
     12       <li><a href="#Setup">Set Up Your Project with the Support Library</a></li>
     13       <li><a href="#Apis">Import the Support Library APIs</a></li>
     14     </ol>
     15     <h2>You should also read</h2>
     16     <ul>
     17       <li><a href="{@docRoot}tools/support-library/index.html">Support Library</a></li>
     18     </ul>
     19   </div>
     20 </div>
     21 
     22 <p>The Android <a href="{@docRoot}tools/support-library/index.html">Support Library</a> provides a JAR
     23 file with an API library that allows you to use some of the more recent Android APIs in your app
     24 while running on earlier versions of Android. For instance, the Support Library provides a version
     25 of the {@link android.app.Fragment} APIs that you can use on Android 1.6 (API level 4) and
     26 higher.</p>
     27 
     28 <p>This lesson shows how to set up your app to use the Support Library in order to use fragments
     29 to build a dynamic app UI.</p>
     30 
     31 
     32 <h2 id="Setup">Set Up Your Project with the Support Library</h2>
     33 
     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
     37 Android Support package selected.</p>
     38 </div>
     39 
     40 <p>To set up your project:</p>
     41 
     42 <ol>
     43   <li>Download the Android Support package using the SDK Manager.</li>
     44 
     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>
     47 directory.
     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
     51 API 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
     60 Android or don't exist in the platform at all and merely provide additional support to you when
     61 developing specific application features.</p>
     62 
     63 <p>You can find all the API reference documentation for the Support Library in the
     64 platform docs at {@link android.support.v4.app android.support.v4.*}.</p>
     65 
     66 <div class="warning"><p><strong>Warning:</strong> To be sure that you don't accidentally use new
     67 APIs on an older system version, be certain that you import the {@link
     68 android.support.v4.app.Fragment} class and related APIs from the {@link android.support.v4.app}
     69 package:</p>
     70 <pre>
     71 import android.support.v4.app.Fragment;
     72 import 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
     79 extend the {@link android.support.v4.app.FragmentActivity} class instead of the traditional {@link
     80 android.app.Activity} class. You'll see sample code for the fragment and activity in the next
     81 lesson.</p>
     82 
     83