1page.title=Setting Up the Action Bar 2 3trainingnavtop=true 4 5@jd:body 6 7<div id="tb-wrapper"> 8 <div id="tb"> 9 10<h2>This lesson teaches you to</h2> 11<ol> 12 <li><a href="#ApiLevel11">Support Android 3.0 and Above Only</a></li> 13 <li><a href="#ApiLevel7">Support Android 2.1 and Above</a></li> 14</ol> 15 16<h2>You should also read</h2> 17<ul> 18 <li><a href="{@docRoot}tools/support-library/setup.html" 19>Setting Up the Support Library</a></li> 20</ul> 21 </div> 22</div> 23 24 25<p>In its most basic form, the action bar displays the title for the activity 26and the app icon on the left. Even in this simple form, the action bar 27is useful for all activities to inform 28users about where they are and to maintain a consistent identity for your app.</p> 29 30<img src="{@docRoot}images/training/basics/actionbar-basic.png" height="100" alt=""/> 31<p class="img-caption"><strong>Figure 1.</strong> An action bar with the app icon and 32activity title.</a> 33 34<p>Setting up a basic action bar requires that your app use an activity theme that enables 35the action bar. How to request such a theme depends on which version of Android is the 36lowest supported by your app. So this 37lesson is divided into two sections depending on which Android 38version is your lowest supported.</p> 39 40 41<h2 id="ApiLevel11">Support Android 3.0 and Above Only</h2> 42 43<p>Beginning with Android 3.0 (API level 11), the action bar is included in all 44activities that use the {@link android.R.style#Theme_Holo Theme.Holo} theme (or one of its 45descendants), which is the default theme when either the <a 46href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> or 47<a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> 48attribute is set to <code>"11"</code> or greater.</p> 49 50<p>So to add the action bar to your activities, simply set either attribute to 51{@code 11} or higher. For example:</p> 52 53<pre> 54<manifest ... > 55 <uses-sdk android:minSdkVersion="11" ... /> 56 ... 57</manifest> 58</pre> 59 60<p class="note"><strong>Note:</strong> If you've created a custom theme, be sure it uses one 61of the {@link android.R.style#Theme_Holo Theme.Holo} themes as its parent. For details, 62see <a href="{@docRoot}training/basics/actionbar/styling.html">Styling the Action Bar</a>.</p> 63 64<p>Now the {@link android.R.style#Theme_Holo Theme.Holo} theme is applied to your app and 65all activities show the action bar. That's it.</p> 66 67 68 69<h2 id="ApiLevel7">Support Android 2.1 and Above</h2> 70 71<p>Adding the action bar when running on versions older than Android 3.0 (down to Android 2.1) 72requires that you include the Android Support Library in your application.</p> 73 74<p>To get started, read the <a href="{@docRoot}tools/support-library/setup.html" 75>Support Library Setup</a> document and set up the <strong>v7 appcompat</strong> 76library (once you've downloaded the library package, follow the instructions for <a 77href="{@docRoot}tools/support-library/setup.html#libs-with-res">Adding libraries with 78resources</a>).</p> 79 80<p>Once you have the Support Library integrated with your app project:</p> 81 82<ol> 83 <li>Update your activity so that it extends {@link android.support.v7.app.ActionBarActivity}. 84 For example: 85<pre> 86public class MainActivity extends ActionBarActivity { ... } 87</pre> 88 </li> 89 <li>In your manifest file, update either the <a 90 href="{@docRoot}guide/topics/manifest/application-element.html">{@code 91 <application>}</a> element or individual 92 <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <activity>}</a> 93 elements to use one of the {@link android.support.v7.appcompat.R.style#Theme_AppCompat 94 Theme.AppCompat} themes. For example: 95 <pre><activity android:theme="@style/Theme.AppCompat.Light" ... ></pre> 96 <p class="note"><strong>Note:</strong> If you've created a custom theme, be sure it uses one 97of the {@link android.support.v7.appcompat.R.style#Theme_AppCompat Theme.AppCompat} themes as 98its parent. For details, see <a href="{@docRoot}training/basics/actionbar/styling.html">Styling 99the Action Bar</a>.</p> 100 </li> 101</ol> 102 103<p>Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher. 104</p> 105 106<p>Remember to properly set your app's API level support in the manifest:</p> 107<pre> 108<manifest ... > 109 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> 110 ... 111</manifest> 112</pre>