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