actionbar.jd revision 6ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2a
118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainpage.title=Using the Action Bar 218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainparent.title=User Interface 318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainparent.link=index.html 418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main@jd:body 518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div id="qv-wrapper"> 718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div id="qv"> 818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <h2>Quickview</h2> 1018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ul> 1150403a203357ec9e003b198728f89278c88d4f0fScott Main <li>A replacement for the title bar that includes the application icon and activity title</li> 1250403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Provides action items from the Options Menu and modes of navigating around the 1350403a203357ec9e003b198728f89278c88d4f0fScott Mainapplication</li> 1450403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Supports custom views, including an embedded search box</li> 156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li>Requires API Level 11</li> 1618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ul> 1718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <h2>In this document</h2> 1918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ol> 206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li><a href="#Adding">Adding the Action Bar</a> 216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <ol> 226ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li><a href="#Removing">Removing the Action Bar</a></li> 236ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </ol> 246ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </li> 2518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li><a href="#ActionItems">Adding Action Items</a> 2618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ol> 276ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li><a href="#Home">Using the app icon as an action item</a></li> 2818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ol> 2918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </li> 3050403a203357ec9e003b198728f89278c88d4f0fScott Main <li><a href="#ActionView">Adding an Action View</a></li> 3118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li><a href="#Tabs">Adding Tabs</a></li> 3218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li><a href="#Dropdown">Adding Drop-down Navigation</a></li> 336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li><a href="#Style">Styling the Action Bar</a></li> 3418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ol> 3518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 3618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <h2>Key classes</h2> 3718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ol> 3818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>{@link android.app.ActionBar}</li> 3918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>{@link android.view.Menu}</li> 4018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ol> 4118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 4218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <h2>See also</h2> 4318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ol> 4418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a></li> 4518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ol> 4618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div> 4718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div> 4818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 4950403a203357ec9e003b198728f89278c88d4f0fScott Main<p>The Action Bar is a widget for activities that replaces the traditional title bar at 506ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe top of the screen. By default, the Action Bar includes the application logo on the left side, 516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfollowed by the activity title, and any available items from the Options Menu on the right side. The 526ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainAction Bar offers several useful features, including the ability to:</p> 5318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 5418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<ul> 5550403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Display items from the <a 566ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/ui/menus.html#OptionsMenu">Options Menu</a> directly in the Action 576ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainBar, as "action 586ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainitems"—providing instant access to key user actions. 596ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <p>Menu items that do not appear as action items are placed in the overflow menu, revealed 606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainby a drop-down list in the Action Bar.</p></li> 6118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Provide tabs for navigating between <a 621064dd9117cb8151d301e251d6fa7cc38661817cScott Mainhref="{@docRoot}guide/topics/fundamentals/fragments.html">fragments</a>.</li> 636ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li>Provide a drop-down list for navigation.</li> 646ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li>Provide interactive "action views" in place of action items (such as a search box).</li> 6518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</ul> 6618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 6718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<img src="{@docRoot}images/ui/actionbar.png" height="36" alt="" /> 686ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 696ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="img-caption"><strong>Figure 1.</strong> A screenshot of the Action Bar in the Email 706ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication, containing action items to compose new email and refresh the inbox.</p> 7118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 7218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 7318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="Adding">Adding the Action Bar</h2> 7418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The Action Bar is included by default in all activities that target Android 3.0 or greater. More 766ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainspecifically, all activities that use the new "holographic" theme include the Action Bar, and any 776ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication that targets Android 3.0 automatically receives this theme. An application is considered 786ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainto "target" Android 3.0 when it has set either the {@code android:minSdkVersion} or {@code 796ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid:targetSdkVersion} attribute in the <a 806ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/manifest/uses-sdk-element.html">{@code <uses-sdk>}</a> element to 816ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@code "11"} or greater. For example:</p> 8218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 8318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 8450403a203357ec9e003b198728f89278c88d4f0fScott Main<manifest xmlns:android="http://schemas.android.com/apk/res/android" 8550403a203357ec9e003b198728f89278c88d4f0fScott Main package="com.example.helloworld" 8650403a203357ec9e003b198728f89278c88d4f0fScott Main android:versionCode="1" 8750403a203357ec9e003b198728f89278c88d4f0fScott Main android:versionName="1.0"> 886ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <uses-sdk android:minSdkVersion="4" 896ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <b>android:targetSdkVersion="11"</b> /> 9050403a203357ec9e003b198728f89278c88d4f0fScott Main <application ... > 9150403a203357ec9e003b198728f89278c88d4f0fScott Main ... 9250403a203357ec9e003b198728f89278c88d4f0fScott Main </application> 9350403a203357ec9e003b198728f89278c88d4f0fScott Main</manifest> 9418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 9518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>In this example, the application requires a minimum version of API 976ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainLevel 4 (Android 1.6), but it also targets API Level 11 (Android 3.0). This way, when 986ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe application is installed on a device running Android 3.0 or greater, the system applies the 996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainholographic theme to each activity, and thus, each activity includes the Action Bar.</p> 1006ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1016ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>However, if you want to use Action Bar APIs, such as to add tabs or modify Action Bar styles, 1026ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainyou need to set the {@code android:minSdkVersion} to {@code "11"}, so you can access the 1036ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.ActionBar} class.</p> 10450403a203357ec9e003b198728f89278c88d4f0fScott Main 10550403a203357ec9e003b198728f89278c88d4f0fScott Main 1066ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<h3 id="Removing">Removing the Action Bar</h3> 1076ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1086ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>If you want to remove the Action Bar for a particular activity, set the activity theme to 1096ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.R.style#Theme_Holo_NoActionBar Theme.Holo.NoActionBar}. For example:</p> 1106ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1116ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<pre> 1126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<activity android:theme="@android:style/Theme.Holo.NoActionBar"> 1136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</pre> 1146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="note"><strong>Tip:</strong> If you have a custom activity theme in which you'd like to 1166ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainremove the Action Bar, set the {@link android.R.styleable#Theme_windowActionBar 1176ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid:windowActionBar} style property {@code false}. See <a href="#Style">Styling the Action 1186ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainBar</a> for more about Action Bar styles.</p> 11950403a203357ec9e003b198728f89278c88d4f0fScott Main 1206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>You can also hide the Action Bar at runtime by calling {@link android.app.ActionBar#hide}, 1216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthen show it again by calling {@link android.app.ActionBar#show}. For example:</p> 12250403a203357ec9e003b198728f89278c88d4f0fScott Main 12350403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 1246ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainActionBar actionBar = getActionBar(); 1256ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainactionBar.hide(); 12650403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 12718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1286ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>When the Action Bar hides, the system adjusts your activity content to fill all the 1296ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainavailable screen space.</p> 1306ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1316ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="note"><strong>Note:</strong> If you remove the Action Bar using a theme, then the 1326ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainwindow will not allow the Action Bar at all, so you cannot add it at runtime—calling 1336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.Activity#getActionBar getActionBar()} will return null.</p> 1346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 13518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 13618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="ActionItems">Adding Action Items</h2> 13718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1386ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>An action item is simply a menu item from the <a 1396ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/ui/menus.html#OptionsMenu">Options Menu</a> which you declare should 1406ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainappear directly in the Action Bar. An action item can include an icon and/or text. If a menu 1416ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainitem does not appear as an action item, then the system places it in the overflow menu, which 1426ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe user can open with the menu icon on the right side of the Action Bar.</p> 14318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 14418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div class="figure" style="width:359px"> 14518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <img src="{@docRoot}images/ui/actionbar-item-withtext.png" height="57" alt="" /> 14650403a203357ec9e003b198728f89278c88d4f0fScott Main <p class="img-caption"><strong>Figure 2.</strong> A screenshot from an Action Bar with two 1476ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainaction items and the overflow menu.</p> 14818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div> 14918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1506ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>When the activity first starts, the system populates the Action Bar and overflow menu by calling 1516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.Activity#onCreateOptionsMenu onCreateOptionsMenu()} for your activity. As 1526ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindiscussed in the guide to <a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>, it's in 1536ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthis callback method that you define the Options Menu for the activity.</p> 1546ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 15550403a203357ec9e003b198728f89278c88d4f0fScott Main<p>You can specify a menu item to appear as an action item—if there is room 1566ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfor it—from your <a href="{@docRoot}guide/topics/resources/menu-resource.html">menu 15718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainresource</a> by declaring {@code 1586ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid:showAsAction="ifRoom"} for the {@code <item>} element. This way, the menu item appears 1596ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainin the Action Bar for quick access only if there is room available for it. If there's not 1606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainenough room, the item is placed the overflow menu (revealed by the menu icon on the right side 1616ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainof the Action Bar).</p> 1626ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 1636ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>You can also declare a menu item to appear as an action item from your application code, by 1646ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maincalling {@link android.view.MenuItem#setShowAsAction setShowAsAction()} on the {@link 16518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.view.MenuItem} and passing {@link android.view.MenuItem#SHOW_AS_ACTION_IF_ROOM}.</p> 16618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1676ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>If your menu item supplies both a title and an icon, then the action item shows only 1686ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe icon by defult. If you want to include the text with the action item, add the "with 1696ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maintext" flag: in XML, add {@code withText} to the {@code android:showAsAction} attribute or, in 17018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainyour application code, use the {@link android.view.MenuItem#SHOW_AS_ACTION_WITH_TEXT} flag when 1716ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maincalling {@link android.view.MenuItem#setShowAsAction setShowAsAction()}. Figure 2 shows an Action 1726ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainBar that has two action items with text and the icon for the overflow menu.</p> 17318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 17418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>Here's an example of how you can declare a menu item as an action item in a <a 17518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/resources/menu-resource.html">menu resource</a> file:</p> 17618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 17718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<?xml version="1.0" encoding="utf-8"?> 17818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<menu xmlns:android="http://schemas.android.com/apk/res/android"> 17918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <item android:id="@+id/menu_add" 18018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main android:icon="@drawable/ic_menu_save" 18118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main android:title="@string/menu_save" 18218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <b>android:showAsAction="ifRoom|withText"</b> /> 18318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</menu> 18418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 18518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 18618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>In this case, both the {@code ifRoom} and {@code withText} flags are set, so that when this 18718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainitem appears as an action item, it includes the title text along with the icon.</p> 18818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 18950403a203357ec9e003b198728f89278c88d4f0fScott Main<p>A menu item placed in the Action Bar triggers the same callback methods as other items in the 1906ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainOptions Menu. When the user selects an action item, your activity receives a call to 19118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()}, passing the 1926ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainitem ID.</p> 19318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 1946ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="note"><strong>Note:</strong> If you added the menu item from a fragment, then the 1956ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainrespective {@link 1966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Fragment#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} method is called 1976ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfor that fragment. However the activity gets a chance to handle it first, so the system calls {@link 1986ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} on the activity 1996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainbefore calling the fragment.</p> 20018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 20118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>You can also declare an item to <em>always</em> appear as an action item, but you should avoid 2026ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindoing so, because it can create a cluttered UI if there are too many action items and they might 2036ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maincollide with other elements in the Action Bar.</p> 20418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 20518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For more information about menus, see the <a 20618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/ui/menus.html#options-menu">Creating Menus</a> developer guide.</p> 20718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 20818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2096ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<h3 id="Home">Using the app icon as an action item</h3> 21018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2116ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>By default, your application icon appears in the Action Bar on the left side. It also responds 2126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainto user interaction (when the user taps it, it visually responds the same way action 2136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainitems do) and it's your responsibility to do something when the user taps it.</p> 2146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<img src="{@docRoot}images/ui/actionbar.png" height="36" alt="" /> 2166ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="img-caption"><strong>Figure 3.</strong> Email's Action Bar, with the 2176ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication icon on the left.</p> 2186ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2196ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The normal behavior should be for your application to return to the "home" activity or the 2206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maininitial state (such as when the activity hasn't changed, but fragments have changed) when the user 2216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maintaps the icon. If the user is already at home or the initial state, then you don't need to do 2226ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainanything.</p> 2236ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2246ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>When the user taps the icon, the system calls your activity's {@link 2256ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onOptionsItemSelected onOptionsItemSelected()} method with the {@code 2266ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.R.id.home} ID. So, you need to add a condition to your {@link 2276ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onOptionsItemSelected onOptionsItemSelected()} method to listen for {@code 2286ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.R.id.home} and perform the appropriate action, such as start the home activity or pop recent 2296ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfragment transactions off the stack.</p> 2306ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2316ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>If you respond to the application icon by returning to the home activity, you should include 2326ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP} flag in the {@link 2336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.content.Intent}. With this flag, if the activity you're starting already exists in the 2346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maincurrent task, then all activities on top of it are destroyed and it is brought to the front. 2356ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainYou should favor this approach, because going "home" is an action that's equivalent to "going 2366ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainback" and you should usually not create a new instance of the home activity. Otherwise, you 2376ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainmight end up with a long stack of activities in the current task.</p> 23818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 23918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For example, here's an implementation of {@link android.app.Activity#onOptionsItemSelected 24018439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainonOptionsItemSelected()} that returns to the application's "home" activity:</p> 24118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 24218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 24318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main@Override 24418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainpublic boolean onOptionsItemSelected(MenuItem item) { 24518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main switch (item.getItemId()) { 24618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main case android.R.id.home: 24750403a203357ec9e003b198728f89278c88d4f0fScott Main // app icon in Action Bar clicked; go home 24818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main Intent intent = new Intent(this, HomeActivity.class); 2496ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 25018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main startActivity(intent); 2516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main return true; 2526ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main default: 2536ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main return super.onOptionsItemSelected(item); 25418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main } 25518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main} 25618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 25718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2586ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2596ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 2616ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<h4>Using the app icon to navigate "up"</h4> 2626ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 26318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div class="figure" style="width:144px"> 26418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <img src="{@docRoot}images/ui/actionbar-logo.png" height="140" alt="" /> 2656ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <p class="img-caption"><strong>Figure 4.</strong> The standard icon for the Email application 2666ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main(top) and the "up" icon (bottom).</p> 26718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div> 26818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2696ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>You can also use the application icon to provide "up" navigation for the user. This is especially 2706ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainuseful when your application is composed of activities that generally appear in a certain order and 2716ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainyou want to facilitate the ability for the user to navigate up the activity hierarchy 2726ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main(regardless of how they entered the current activity).</p> 27318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2746ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The way you respond to this event is the same as when navigating home (as 2756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindiscussed above, except you start a different activity, based on the current activity). All you 2766ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainneed to do to indicate to the user that the behavior is different is set the Action Bar to "show 2776ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhome as up." You can do so by calling {@link android.app.ActionBar#setDisplayHomeAsUpEnabled 2786ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainsetDisplayHomeAsUpEnabled(true)} on your activity's {@link android.app.ActionBar}. When you do, the 2796ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainsystem draws your application icon with an arrow indicating the up behavior, as shown in figure 2806ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main4.</p> 28118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2826ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For example, here's how you can show the application icon as an "up" action:</p> 28318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 28418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 28518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main@Override 28618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprotected void onStart() { 2876ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main super.onStart(); 2886ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main ActionBar actionBar = this.getActionBar(); 2896ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main actionBar.setDisplayHomeAsUpEnabled(true); 29018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main} 29118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 29218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 2936ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>Then, your activity should respond to the user tapping the icon, from the {@link 2946ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onOptionsItemSelected 2956ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainonOptionsItemSelected()}, by listening for the {@code android.R.id.home} ID (as shown above). In 2966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthis case, when navigating up, it's even more important that you use the {@link 2976ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.content.Intent#FLAG_ACTIVITY_CLEAR_TOP} flag in the {@link android.content.Intent}, so that 2986ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainyou don't create a new instance of the parent activity if one already exists.</p> 2996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 30018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 30118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 30250403a203357ec9e003b198728f89278c88d4f0fScott Main 30350403a203357ec9e003b198728f89278c88d4f0fScott Main<h2 id="ActionView">Adding an Action View</h2> 30450403a203357ec9e003b198728f89278c88d4f0fScott Main 3056ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<div class="figure" style="width:429px"> 30650403a203357ec9e003b198728f89278c88d4f0fScott Main <img src="{@docRoot}images/ui/actionbar-actionview.png" alt="" /> 3076ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <p class="img-caption"><strong>Figure 5.</strong> An action view with a {@link 3086ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.widget.SearchView} widget.</p> 30950403a203357ec9e003b198728f89278c88d4f0fScott Main</div> 31050403a203357ec9e003b198728f89278c88d4f0fScott Main 3116ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>An action view is a widget that appears in the Action Bar as a substitute for an action 3126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainitem. For example, if you have an item in the Options Menu for "Search", you can add an action view 3136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfor the item that provides a {@link android.widget.SearchView} widget in the Action Bar whenever 3146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe item is enabled as an action item.</p> 3156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 3166ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>When adding an action view for a menu item, it's important that you still allow the item to 3176ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainbehave as a normal menu item when it does not appear in the Action Bar. For example, a menu item to 3186ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainperform a search should, by default, bring up the <a 3196ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/search/search-dialog.html">search dialog</a>, but if the item is 3206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainplaced in the Action Bar, the action view appears with a {@link android.widget.SearchView} 3216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainwidget. Figure 4 shows an example of the {@link android.widget.SearchView} widget in an action 3226ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainview.</p> 32350403a203357ec9e003b198728f89278c88d4f0fScott Main 32450403a203357ec9e003b198728f89278c88d4f0fScott Main<p>The best way to declare an action view for an item is in your <a 32550403a203357ec9e003b198728f89278c88d4f0fScott Mainhref="{@docRoot}guide/topics/resources/menu-resource.html">menu resource</a>, using the {@code 3266ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid:actionLayout} or {@code android:actionViewClass} attribute:</p> 32750403a203357ec9e003b198728f89278c88d4f0fScott Main 32850403a203357ec9e003b198728f89278c88d4f0fScott Main<ul> 32950403a203357ec9e003b198728f89278c88d4f0fScott Main <li>The value for {@code android:actionLayout} must be a resource pointer to a layout file. 33050403a203357ec9e003b198728f89278c88d4f0fScott MainFor example: 33150403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 3326ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<?xml version="1.0" encoding="utf-8"?> 3336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<menu xmlns:android="http://schemas.android.com/apk/res/android"> 3346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item android:id="@+id/menu_search" 3356ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:title="Search" 3366ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:icon="@drawable/ic_menu_search" 3376ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:showAsAction="ifRoom" 3386ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <b>android:actionLayout="@layout/searchview"</b> /> 3396ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</menu> 3406ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</pre> 3416ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</li> 3426ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 34350403a203357ec9e003b198728f89278c88d4f0fScott Main <li>The value for {@code android:actionViewClass} must be a fully-qualified class name for 34450403a203357ec9e003b198728f89278c88d4f0fScott Mainthe {@link android.view.View} you want to use. For example: 34550403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 3466ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<?xml version="1.0" encoding="utf-8"?> 3476ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<menu xmlns:android="http://schemas.android.com/apk/res/android"> 3486ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item android:id="@+id/menu_search" 3496ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:title="Search" 3506ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:icon="@drawable/ic_menu_search" 3516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:showAsAction="ifRoom" 3526ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <b>android:actionViewClass="android.widget.SearchView"</b> /> 3536ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</menu> 35450403a203357ec9e003b198728f89278c88d4f0fScott Main</pre></li> 35550403a203357ec9e003b198728f89278c88d4f0fScott Main</ul> 35650403a203357ec9e003b198728f89278c88d4f0fScott Main 3576ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="note">You must include {@code android:showAsAction="ifRoom"} in order for the item to 3586ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainappear as an action view when room is available. If necessary, however, you can force the item to 3596ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainalways appear as an action view by setting {@code android:showAsAction} to {@code "always"}.</p> 3606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 36150403a203357ec9e003b198728f89278c88d4f0fScott Main<p>Now, when the menu item is displayed as an action item, it's action view appears instead of 3626ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe icon and/or title text. However, if there's not enough room in the Action Bar, the item appears 3636ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainin the overflow menu as a normal menu item and you must respond to it from the {@link 3646ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onOptionsItemSelected onOptionsItemSelected()} callback method. (For a 3656ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainguide to providing search functionality, see the <a 3666ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}gudie/topics/search/index.html">Search</a> documentation.)</p> 36750403a203357ec9e003b198728f89278c88d4f0fScott Main 3686ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>When the activity first starts, the system populates the Action Bar and overflow menu by calling 36950403a203357ec9e003b198728f89278c88d4f0fScott Main{@link android.app.Activity#onCreateOptionsMenu onCreateOptionsMenu()}. 37050403a203357ec9e003b198728f89278c88d4f0fScott MainAfter you've inflated your menu in this method, you can acquire elements in an action view 37150403a203357ec9e003b198728f89278c88d4f0fScott Main(perhaps in order to attach listeners) by calling {@link android.view.Menu#findItem 37250403a203357ec9e003b198728f89278c88d4f0fScott MainfindItem()} with the ID of the menu item, then {@link android.view.MenuItem#getActionView} on 37350403a203357ec9e003b198728f89278c88d4f0fScott Mainthe returned {@link android.view.MenuItem}. For example, the search widget from the above samples is 37450403a203357ec9e003b198728f89278c88d4f0fScott Mainacquired like this:</p> 37550403a203357ec9e003b198728f89278c88d4f0fScott Main 37650403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 37750403a203357ec9e003b198728f89278c88d4f0fScott Main@Override 37850403a203357ec9e003b198728f89278c88d4f0fScott Mainpublic boolean onCreateOptionsMenu(Menu menu) { 37950403a203357ec9e003b198728f89278c88d4f0fScott Main getMenuInflater().inflate(R.menu.options, menu); 38050403a203357ec9e003b198728f89278c88d4f0fScott Main SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 38150403a203357ec9e003b198728f89278c88d4f0fScott Main // Set appropriate listeners for searchView 38250403a203357ec9e003b198728f89278c88d4f0fScott Main ... 38350403a203357ec9e003b198728f89278c88d4f0fScott Main return super.onCreateOptionsMenu(menu); 38450403a203357ec9e003b198728f89278c88d4f0fScott Main} 38550403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 38650403a203357ec9e003b198728f89278c88d4f0fScott Main 38750403a203357ec9e003b198728f89278c88d4f0fScott Main<p>For more information about enabling search in the Action Bar, see the <a 38850403a203357ec9e003b198728f89278c88d4f0fScott Mainhref="{@docRoot}guide/topics/search/index.html">Search</a> developer guide.</p> 38950403a203357ec9e003b198728f89278c88d4f0fScott Main 39050403a203357ec9e003b198728f89278c88d4f0fScott Main 39150403a203357ec9e003b198728f89278c88d4f0fScott Main 3926ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 39318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="Tabs">Adding Tabs</h2> 39418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 39518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 3966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<div class="figure" style="width:504px"> 39750403a203357ec9e003b198728f89278c88d4f0fScott Main <img src="{@docRoot}images/ui/actionbar-tabs.png" alt="" /> 3986ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <p class="img-caption"><strong>Figure 6.</strong> Screenshot of tabs in the 3996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainAction Bar, from the <a 4006ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}resources/samples/Honeycomb-Gallery/index.html">Honeycomb Gallery</a> sample 4016ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication.</p> 40250403a203357ec9e003b198728f89278c88d4f0fScott Main</div> 4036ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 4046ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The Action Bar can display tabs that allow the user navigate between different fragments in the 4056ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainactivity. Each tab can include a title and/or an icon.</p> 40650403a203357ec9e003b198728f89278c88d4f0fScott Main 40718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>To begin, your layout must include a {@link android.view.View} in which each {@link 40818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Fragment} associated with a tab is displayed. Be sure the view has an ID that you 40918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincan use to reference it from your code.</p> 41018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 41150403a203357ec9e003b198728f89278c88d4f0fScott Main<p>To add tabs to the Action Bar:</p> 41218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<ol> 41318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Create an implementation of {@link android.app.ActionBar.TabListener} to handle the 41450403a203357ec9e003b198728f89278c88d4f0fScott Maininteraction events on the Action Bar tabs. You must implement all methods: {@link 41518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabSelected onTabSelected()}, {@link 41618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabUnselected onTabUnselected()}, and {@link 41718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabReselected onTabReselected()}. 41818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <p>Each callback method passes the {@link android.app.ActionBar.Tab} that received the 41918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainevent and a {@link android.app.FragmentTransaction} for you to perform the fragment 42018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maintransactions (add or remove fragments).</p> 42118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <p>For example:</p> 42218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 42318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprivate class MyTabListener implements ActionBar.TabListener { 42418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main private TabContentFragment mFragment; 42518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 42618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main // Called to create an instance of the listener when adding a new tab 42718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main public TabListener(TabContentFragment fragment) { 42818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main mFragment = fragment; 42918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main } 43018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 43118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main @Override 43218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main public void onTabSelected(Tab tab, FragmentTransaction ft) { 43318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main ft.add(R.id.fragment_content, mFragment, null); 43418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main } 43518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 43618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main @Override 43718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main public void onTabUnselected(Tab tab, FragmentTransaction ft) { 43818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main ft.remove(mFragment); 43918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main } 44018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 44118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main @Override 44218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main public void onTabReselected(Tab tab, FragmentTransaction ft) { 44318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main // do nothing 44418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main } 44518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 44618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main} 44718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 44818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <p>This implementation of {@link android.app.ActionBar.TabListener} adds a constructor 44918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat saves the {@link android.app.Fragment} associated with a tab so that each callback 45018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincan add or remove that fragment.</p> 45118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </li> 45218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Get the {@link android.app.ActionBar} for your activity by calling {@link 45318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Activity#getActionBar} from your {@link android.app.Activity}, during {@link 45418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Activity#onCreate onCreate()} (but be sure you do so <em>after</em> you've called 45518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.Activity#setContentView setContentView()}).</li> 45618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Call {@link android.app.ActionBar#setNavigationMode(int) 45718439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetNavigationMode(NAVIGATION_MODE_TABS)} to enable tab mode for the {@link 45818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar}.</li> 45950403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Create each tab for the Action Bar: 46018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <ol> 46118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Create a new {@link android.app.ActionBar.Tab} by calling {@link 46218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#newTab()} on the {@link android.app.ActionBar}.</li> 46318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Add title text and/or an icon for the tab by calling {@link 46418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.Tab#setText setText()} and/or {@link android.app.ActionBar.Tab#setIcon 46518439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetIcon()}. 46618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <p class="note"><strong>Tip:</strong> These methods return the same {@link 46718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.Tab} instance, so you can chain the calls together.</p></li> 46818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main <li>Declare the {@link android.app.ActionBar.TabListener} to use for the tab by passing an 46918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maininstance of your implementation to {@link android.app.ActionBar.Tab#setTabListener 47018439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetTabListener()}. 47118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </ol> 47218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main </li> 47350403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Add each {@link android.app.ActionBar.Tab} to the Action Bar by calling {@link 47418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#addTab addTab()} on the {@link android.app.ActionBar} and passing the 4756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.ActionBar.Tab}.</li> 47618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</ol> 47718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For example, the following code combines steps 2 - 5 to create two tabs and add them to 47850403a203357ec9e003b198728f89278c88d4f0fScott Mainthe Action Bar:</p> 47918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre> 48018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main@Override 48118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprotected void onCreate(Bundle savedInstanceState) { 48218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main super.onCreate(savedInstanceState); 48318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main setContentView(R.layout.main); 48418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 48550403a203357ec9e003b198728f89278c88d4f0fScott Main // setup Action Bar for tabs 48618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main final ActionBar actionBar = getActionBar(); 48718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 48818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main // remove the activity title to make space for tabs 4896ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main actionBar.setDisplayShowTitleEnabled(false); 49018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 49118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main // instantiate fragment for the tab 49218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main Fragment artistsFragment = new ArtistsFragment(); 49318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main // add a new tab and set its title text and tab listener 49450403a203357ec9e003b198728f89278c88d4f0fScott Main actionBar.addTab(actionBar.newTab().setText(R.string.tab_artists) 49518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main .setTabListener(new TabListener(artistsFragment))); 49618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 49718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main Fragment albumsFragment = new AlbumsFragment(); 49850403a203357ec9e003b198728f89278c88d4f0fScott Main actionBar.addTab(actionBar.newTab().setText(R.string.tab_albums) 49918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main .setTabListener(new TabListener(albumsFragment))); 50018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main} 50118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre> 50218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 50318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>All the behaviors that occur when a tab is selected must be defined by your {@link 50418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener} callback methods. When a tab is selected, it receives a call to 50518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.ActionBar.TabListener#onTabSelected onTabSelected()} and that's where you should 50618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainadd the appropriate fragment to the designated view in your layout, using {@link 50718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction#add add()} with the provided {@link 50818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction}. Likewise, when a tab is deselected (because another tab becomes 50918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainselected), you should remove that fragment from the layout, using {@link 51018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction#remove remove()}.</p> 51118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 5126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="caution"><strong>Caution:</strong> You <strong>must not</strong> call {@link 5136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.FragmentTransaction#commit} for these transactions—the system calls it for you 5146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainand it may throw an exception if you call it yourself. You also <strong>cannot</strong> add these 5156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfragment transactions to the back stack.</p> 51618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 51718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>If your activity is stopped, you should retain the currently selected tab with the saved state so 51818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat when the user returns to your application, you can open the tab. When it's time to save the 51918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainstate, you can query the currently selected tab with {@link 5201264c33163146d6088675d197551a63b3f9d360bAdam Powellandroid.app.ActionBar#getSelectedNavigationIndex()}. This returns the index position of the selected 52118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maintab.</p> 52218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 52318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="caution"><strong>Caution:</strong> It's important that you save 52418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe state of each fragment as necessary, so when the user switches fragments with the tabs, 52518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthen returns to a previous fragment, it appears the way they left. For information about saving 52618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe state of your fragment, see the <a 5271064dd9117cb8151d301e251d6fa7cc38661817cScott Mainhref="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> 52818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 52918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main 53050403a203357ec9e003b198728f89278c88d4f0fScott Main 5316ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 53250403a203357ec9e003b198728f89278c88d4f0fScott Main<h2 id="Dropdown">Adding Drop-down Navigation</h2> 53350403a203357ec9e003b198728f89278c88d4f0fScott Main 53450403a203357ec9e003b198728f89278c88d4f0fScott Main<p>As another mode of navigation within your activity, you can provide a drop-down list in the 53550403a203357ec9e003b198728f89278c88d4f0fScott MainAction Bar. For example, the drop-down list can provide alternative modes for sorting the content in 53650403a203357ec9e003b198728f89278c88d4f0fScott Mainthe activity or switching the user's account.</p> 53750403a203357ec9e003b198728f89278c88d4f0fScott Main 53850403a203357ec9e003b198728f89278c88d4f0fScott Main<!-- 53950403a203357ec9e003b198728f89278c88d4f0fScott Main<div class="figure" style="width:135px"> 54050403a203357ec9e003b198728f89278c88d4f0fScott Main <img src="{@docRoot}images/ui/actionbar-dropdown.png" alt="" /> 54150403a203357ec9e003b198728f89278c88d4f0fScott Main <p class="img-caption"><strong>Figure 5.</strong> Screenshot of a drop-down navigation list in the 54250403a203357ec9e003b198728f89278c88d4f0fScott MainAction Bar.</p> 54350403a203357ec9e003b198728f89278c88d4f0fScott Main</div> 54450403a203357ec9e003b198728f89278c88d4f0fScott Main--> 54550403a203357ec9e003b198728f89278c88d4f0fScott Main 5466ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>Here's a quick list of steps to enable drop-down navigation:</p> 54750403a203357ec9e003b198728f89278c88d4f0fScott Main 54850403a203357ec9e003b198728f89278c88d4f0fScott Main<ol> 54950403a203357ec9e003b198728f89278c88d4f0fScott Main <li>Create a {@link android.widget.SpinnerAdapter} that provides the 5506ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainlist of selectable items for the drop-down and the layout to use when drawing each item in the 5516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainlist.</li> 5528515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam Powell <li>Implement {@link android.app.ActionBar.OnNavigationListener} to define the behavior when the 55350403a203357ec9e003b198728f89278c88d4f0fScott Mainuser selects an item from the list.</li> 5546ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li>Enable navigation mode for the Action Bar with {@link 55550403a203357ec9e003b198728f89278c88d4f0fScott Mainandroid.app.ActionBar#setNavigationMode setNavigationMode()}. For example: 55650403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 55750403a203357ec9e003b198728f89278c88d4f0fScott MainActionBar actionBar = getActionBar(); 55850403a203357ec9e003b198728f89278c88d4f0fScott MainactionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 55950403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 5606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <p class="note"><strong>Note:</strong> You should perform this during your activity's {@link 5616ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Activity#onCreate 56250403a203357ec9e003b198728f89278c88d4f0fScott MainonCreate()} method.</p> 56350403a203357ec9e003b198728f89278c88d4f0fScott Main </li> 5646ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <li>Then, set the callback for the drop-down list with {@link 56550403a203357ec9e003b198728f89278c88d4f0fScott Mainandroid.app.ActionBar#setListNavigationCallbacks setListNavigationCallbacks()}. For example: 56650403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 56750403a203357ec9e003b198728f89278c88d4f0fScott MainactionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback); 56850403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 56950403a203357ec9e003b198728f89278c88d4f0fScott Main<p>This method takes your {@link android.widget.SpinnerAdapter} and {@link 5708515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam Powellandroid.app.ActionBar.OnNavigationListener}. More about these next.</p> 57150403a203357ec9e003b198728f89278c88d4f0fScott Main</li> 57250403a203357ec9e003b198728f89278c88d4f0fScott Main</ol> 57350403a203357ec9e003b198728f89278c88d4f0fScott Main 5746ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>That's the basic setup. However, implementing the {@link android.widget.SpinnerAdapter} and 5756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.ActionBar.OnNavigationListener} is where most of the work is done. There are many 5766ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainways you can implement these to define the functionality for your drop-down navigation and 5776ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainimplementing various types of {@link android.widget.SpinnerAdapter} is beyond the scope of this 5786ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindocument (you should refer to the {@link android.widget.SpinnerAdapter} class reference for more 5796ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maininformation). However, below is a simple example for a {@link android.widget.SpinnerAdapter} and 5806ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.ActionBar.OnNavigationListener} to get you started (click the title to 5816ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainreveal the sample).</p> 5826ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 5836ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 5846ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 5856ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<div class="toggle-content closed"> 58650403a203357ec9e003b198728f89278c88d4f0fScott Main 5876ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <h3 id="Spinner"><a href="#" onclick="return toggleContent(this)"> 5886ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <img src="{@docRoot}assets/images/triangle-closed.png" class="toggle-content-img" alt="" /> 5896ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main Example SpinnerAdapter and OnNavigationListener 5906ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </a></h3> 59150403a203357ec9e003b198728f89278c88d4f0fScott Main 5926ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <div class="toggle-content-toggleme"> 59350403a203357ec9e003b198728f89278c88d4f0fScott Main 5946ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>{@link android.widget.SpinnerAdapter} is an adapter that provides data for a spinner widget, 5956ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainsuch as the drop-down list in the Action Bar. {@link android.widget.SpinnerAdapter} is an interface 5966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthat you can implement, but Android includes some useful implementations that you can extend, such 5976ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainas {@link android.widget.ArrayAdapter} and {@link 59850403a203357ec9e003b198728f89278c88d4f0fScott Mainandroid.widget.SimpleCursorAdapter}. For example, here's an easy way to create a {@link 5996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.widget.SpinnerAdapter} by using {@link android.widget.ArrayAdapter} implementation, which 6006ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainuses a string array as the data source:</p> 60150403a203357ec9e003b198728f89278c88d4f0fScott Main 60250403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 60350403a203357ec9e003b198728f89278c88d4f0fScott MainSpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, 60450403a203357ec9e003b198728f89278c88d4f0fScott Main android.R.layout.simple_spinner_dropdown_item); 60550403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 60650403a203357ec9e003b198728f89278c88d4f0fScott Main 6076ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The {@link android.widget.ArrayAdapter#createFromResource createFromResource()} method takes 6086ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthree parameters: the application {@link android.content.Context}, the resource ID for the string 6096ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainarray, and the layout to use for each list item.</p> 61050403a203357ec9e003b198728f89278c88d4f0fScott Main 61150403a203357ec9e003b198728f89278c88d4f0fScott Main<p>A <a href="{@docRoot}guide/topics/resources/string-resource.html#StringArray">string array</a> 6126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindefined in a resource looks like this:</p> 61350403a203357ec9e003b198728f89278c88d4f0fScott Main 61450403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 61550403a203357ec9e003b198728f89278c88d4f0fScott Main<?xml version="1.0" encoding="utf-8"?> 61650403a203357ec9e003b198728f89278c88d4f0fScott Main<resources> 61750403a203357ec9e003b198728f89278c88d4f0fScott Main <string-array name="action_list"> 61850403a203357ec9e003b198728f89278c88d4f0fScott Main <item>Mercury</item> 61950403a203357ec9e003b198728f89278c88d4f0fScott Main <item>Venus</item> 62050403a203357ec9e003b198728f89278c88d4f0fScott Main <item>Earth</item> 62150403a203357ec9e003b198728f89278c88d4f0fScott Main </string-array> 62250403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 62350403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 62450403a203357ec9e003b198728f89278c88d4f0fScott Main 6256ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The {@link android.widget.ArrayAdapter} returned by {@link 6266ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.widget.ArrayAdapter#createFromResource createFromResource()} is complete and ready for you 6276ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainto pass it to {@link android.app.ActionBar#setListNavigationCallbacks setListNavigationCallbacks()} 6286ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main(in step 4 from above). Before you do, though, you need to create the {@link 6296ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.ActionBar.OnNavigationListener OnNavigationListener}.</p> 63050403a203357ec9e003b198728f89278c88d4f0fScott Main 63150403a203357ec9e003b198728f89278c88d4f0fScott Main 6328515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam Powell<p>Your implementation of {@link android.app.ActionBar.OnNavigationListener} is where you handle 63350403a203357ec9e003b198728f89278c88d4f0fScott Mainfragment changes or other modifications to your activity when the user selects an item from the 6346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindrop-down list. There's only one callback method to implement in the listener: {@link 6358515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam Powellandroid.app.ActionBar.OnNavigationListener#onNavigationItemSelected onNavigationItemSelected()}.</p> 63650403a203357ec9e003b198728f89278c88d4f0fScott Main 63750403a203357ec9e003b198728f89278c88d4f0fScott Main<p>The {@link 6388515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam Powellandroid.app.ActionBar.OnNavigationListener#onNavigationItemSelected onNavigationItemSelected()} 6396ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainmethod receives the position of the item in the list and a unique item ID provided by the {@link 64050403a203357ec9e003b198728f89278c88d4f0fScott Mainandroid.widget.SpinnerAdapter}.</p> 64150403a203357ec9e003b198728f89278c88d4f0fScott Main 64250403a203357ec9e003b198728f89278c88d4f0fScott Main<p>Here's an example that instantiates an anonymous implementation of {@link 6436ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.ActionBar.OnNavigationListener OnNavigationListener}, which inserts a {@link 6446ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.app.Fragment} into the 64550403a203357ec9e003b198728f89278c88d4f0fScott Mainlayout container identified by {@code R.id.fragment_container}:</p> 64650403a203357ec9e003b198728f89278c88d4f0fScott Main 64750403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 6488515ee846bd76aee86ec5ddfcc4dd1e626dd999cAdam PowellmOnNavigationListener = new OnNavigationListener() { 64950403a203357ec9e003b198728f89278c88d4f0fScott Main // Get the same strings provided for the drop-down's ArrayAdapter 65050403a203357ec9e003b198728f89278c88d4f0fScott Main String[] strings = getResources().getStringArray(R.array.action_list); 65150403a203357ec9e003b198728f89278c88d4f0fScott Main 65250403a203357ec9e003b198728f89278c88d4f0fScott Main @Override 65350403a203357ec9e003b198728f89278c88d4f0fScott Main public boolean onNavigationItemSelected(int position, long itemId) { 65450403a203357ec9e003b198728f89278c88d4f0fScott Main // Create new fragment from our own Fragment class 65550403a203357ec9e003b198728f89278c88d4f0fScott Main ListContentFragment newFragment = new ListContentFragment(); 65650403a203357ec9e003b198728f89278c88d4f0fScott Main FragmentTransaction ft = openFragmentTransaction(); 65750403a203357ec9e003b198728f89278c88d4f0fScott Main // Replace whatever is in the fragment container with this fragment 65850403a203357ec9e003b198728f89278c88d4f0fScott Main // and give the fragment a tag name equal to the string at the position selected 65950403a203357ec9e003b198728f89278c88d4f0fScott Main ft.replace(R.id.fragment_container, newFragment, strings[position]); 66050403a203357ec9e003b198728f89278c88d4f0fScott Main // Apply changes 66150403a203357ec9e003b198728f89278c88d4f0fScott Main ft.commit(); 66250403a203357ec9e003b198728f89278c88d4f0fScott Main return true; 66350403a203357ec9e003b198728f89278c88d4f0fScott Main } 66450403a203357ec9e003b198728f89278c88d4f0fScott Main}; 66550403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 66650403a203357ec9e003b198728f89278c88d4f0fScott Main 6676ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>This instance of {@link android.app.ActionBar.OnNavigationListener OnNavigationListener} is 6686ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maincomplete and you can now call {@link android.app.ActionBar#setListNavigationCallbacks 6696ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainsetListNavigationCallbacks()} (in step 4), passing the {@link android.widget.ArrayAdapter} and this 6706ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main{@link android.app.ActionBar.OnNavigationListener OnNavigationListener}.</p> 67150403a203357ec9e003b198728f89278c88d4f0fScott Main 6726ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>In this example, when the user selects an item from the drop-down list, a fragment is added to 6736ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe layout (replacing the current fragment in the {@code R.id.fragment_container} view). The 6746ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainfragment added is given a tag that uniquely identifies it, which is the same string used to 6756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainidentify the fragment in the drop-down list.</p> 6766ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 6776ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>Here's a look at the {@code ListContentFragment} class that defines each fragment in this 6786ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainexample:</p> 67950403a203357ec9e003b198728f89278c88d4f0fScott Main 68050403a203357ec9e003b198728f89278c88d4f0fScott Main<pre> 68150403a203357ec9e003b198728f89278c88d4f0fScott Mainpublic class ListContentFragment extends Fragment { 68250403a203357ec9e003b198728f89278c88d4f0fScott Main private String mText; 68350403a203357ec9e003b198728f89278c88d4f0fScott Main 68450403a203357ec9e003b198728f89278c88d4f0fScott Main @Override 68550403a203357ec9e003b198728f89278c88d4f0fScott Main public void onAttach(Activity activity) { 68650403a203357ec9e003b198728f89278c88d4f0fScott Main // This is the first callback received; here we can set the text for 68750403a203357ec9e003b198728f89278c88d4f0fScott Main // the fragment as defined by the tag specified during the fragment transaction 68850403a203357ec9e003b198728f89278c88d4f0fScott Main super.onAttach(activity); 68950403a203357ec9e003b198728f89278c88d4f0fScott Main mText = getTag(); 69050403a203357ec9e003b198728f89278c88d4f0fScott Main } 69150403a203357ec9e003b198728f89278c88d4f0fScott Main 69250403a203357ec9e003b198728f89278c88d4f0fScott Main @Override 69350403a203357ec9e003b198728f89278c88d4f0fScott Main public View onCreateView(LayoutInflater inflater, ViewGroup container, 69450403a203357ec9e003b198728f89278c88d4f0fScott Main Bundle savedInstanceState) { 69550403a203357ec9e003b198728f89278c88d4f0fScott Main // This is called to define the layout for the fragment; 69650403a203357ec9e003b198728f89278c88d4f0fScott Main // we just create a TextView and set its text to be the fragment tag 69750403a203357ec9e003b198728f89278c88d4f0fScott Main TextView text = new TextView(getActivity()); 69850403a203357ec9e003b198728f89278c88d4f0fScott Main text.setText(mText); 69950403a203357ec9e003b198728f89278c88d4f0fScott Main return text; 70050403a203357ec9e003b198728f89278c88d4f0fScott Main } 70150403a203357ec9e003b198728f89278c88d4f0fScott Main} 70250403a203357ec9e003b198728f89278c88d4f0fScott Main</pre> 70350403a203357ec9e003b198728f89278c88d4f0fScott Main 7046ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </div><!-- end toggle-content-toggleme --> 7056ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7066ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</div><!-- end toggle-content --> 7076ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7086ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7096ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7106ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7116ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<h2 id="Style">Styling the Action Bar</h2> 7136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The Action Bar is the heading for your application and a primary interaction point for users, 7156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainso you might want to modify some of its design in order to make it feel more integrated with your 7166ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication design. There are several ways you can do this if you wish.</p> 7176ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7186ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For simple modifications to the {@link android.app.ActionBar}, you can use the following 7196ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainmethods:</p> 7206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<dl> 7226ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.app.ActionBar#setBackgroundDrawable setBackgroundDrawable()}</dt> 7236ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Sets a drawable to use as the Action Bar's background. The drawable should be a <a 7246ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">Nine-patch</a> image, a <a 7256ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/resources/drawable-resource.html#Shape">shape</a>, or a <a 7266ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/resources/more-resources.html#Color">solid color</a>, so the system can 7276ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainresize the drawable based on the size of the Action Bar (you should <em>not</em> use a fixed-size 7286ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainbitmap image).</dd> 7296ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7306ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.app.ActionBar#setDisplayUseLogoEnabled setDisplayUseLogoEnabled()}</dt> 7316ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Enables the use of an alternative image (a "logo") in the Action Bar, instead of the default 7326ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainapplication icon. A logo is often a wider, more detailed image that represents the application. 7336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainWhen this is enabled, the system uses the logo image defined for the application (or the 7346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainindividual activity) in the manifest file, with the <a 7356ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/manifest/application-element.html#logo">{@code android:logo}</a> 7366ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainattribute. The logo will be resized as necessary to fit the height of the Action Bar. (Best 7376ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainpractice is to design the logo at the same size as your application icon.)</dd> 7386ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</dl> 7396ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7406ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7416ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For more complex customizations, you can use Android's <a 7426ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/ui/themes.html">style and theme</a> framework to restyle your Action 7436ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainBar in several ways.</p> 7446ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7456ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>The Action Bar has two standard themes, "dark" and "light". The dark theme is applied with 7466ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe default holographic theme, as specified by the {@link android.R.style#Theme_Holo Theme.Holo} 7476ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maintheme. If you want a white background with dark text, instead, you can apply the {@link 7486ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainandroid.R.style#Theme_Holo_Light Theme.Holo.Light} theme to the activity in the manifest file. For 7496ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainexample:</p> 7506ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7516ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<pre> 7526ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<activity android:name=".ExampleActivity" 7536ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:theme="@android:style/Theme.Holo.Light" /> 7546ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</pre> 7556ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7566ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For more control, you can override either the {@link android.R.style#Theme_Holo 7576ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainTheme.Holo} or {@link android.R.style#Theme_Holo_Light Theme.Holo.Light} theme and apply custom 7586ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainstyles to certain aspects of the Action Bar. Some of the Action Bar properties you can customize 7596ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maininclude the following:</p> 7606ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7616ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<dl> 7626ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_actionBarTabStyle 7636ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:actionBarTabStyle}</dt> 7646ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Style for tabs in the Action Bar.</dd> 7656ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7666ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_actionBarTabBarStyle 7676ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:actionBarTabBarStyle}</dt> 7686ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Style for the bar that appears below tabs in the Action Bar.</dd> 7696ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7706ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_actionBarTabTextStyle 7716ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:actionBarTabTextStyle}</dt> 7726ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Style for the text in the tabs.</dd> 7736ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7746ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_actionDropDownStyle 7756ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:actionDropDownStyle}</dt> 7766ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Style for the drop-down list used for the overflow menu and drop-down navigation.</dd> 7776ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7786ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_actionButtonStyle 7796ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:actionButtonStyle}</dt> 7806ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Style for the background image used for buttons in the Action Bar.</dd> 7816ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7826ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</dl> 7836ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7846ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For example, here's a resource file that defines a custom theme for the Action Bar, based on 7856ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainthe standard {@link android.R.style#Theme_Holo Theme.Holo} theme:</p> 7866ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7876ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<pre> 7886ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<?xml version="1.0" encoding="utf-8"?> 7896ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<resources> 7906ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <!-- the theme applied to the application or activity --> 7916ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <style name="CustomActionBar" parent="android:style/Theme.Holo.Light"> 7926ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:actionBarTabTextStyle">@style/customActionBarTabTextStyle</item> 7936ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:actionBarTabStyle">@style/customActionBarTabStyle</item> 7946ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:actionBarTabBarStyle">@style/customActionBarTabBarStyle</item> 7956ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </style> 7966ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 7976ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <!-- style for the tab text --> 7986ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <style name="customActionBarTabTextStyle"> 7996ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:textColor">#2966c2</item> 8006ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:textSize">20sp</item> 8016ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:typeface">sans</item> 8026ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </style> 8036ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8046ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <!-- style for the tabs --> 8056ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <style name="customActionBarTabStyle"> 8066ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:background">@drawable/actionbar_tab_bg</item> 8076ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:paddingLeft">20dp</item> 8086ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:paddingRight">20dp</item> 8096ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </style> 8106ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8116ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <!-- style for the tab bar --> 8126ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <style name="customActionBarTabBarStyle"> 8136ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <item name="android:background">@drawable/actionbar_tab_bar</item> 8146ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main </style> 8156ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</resources> 8166ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</pre> 8176ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8186ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p class="note"><strong>Note:</strong> In order for the tab background image to change, 8196ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maindepending on the current tab state (selected, pressed, unselected), the drawable resource used 8206ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainmust be a <a href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">state 8216ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainlist drawable</a>. Also be certain that your theme declares a parent theme, from which it 8226ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Maininherits all styles not explicitly declared in your theme.</p> 8236ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8246ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>You can apply your custom theme to the entire application or to individual activities in your 8256ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainmanifest file, like this:</p> 8266ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8276ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<pre> 8286ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<application android:theme="@style/CustomActionBar" 8296ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main ... /> 8306ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</pre> 8316ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8326ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>Additionally, if you want to create a custom theme for your activity that removes the Action 8336ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott MainBar completely, use the following style attributes:</p> 8346ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8356ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<dl> 8366ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_windowActionBar 8376ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:windowActionBar}</dt> 8386ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Set this style property {@code false} to remove the Action Bar.</dd> 8396ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8406ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dt>{@link android.R.styleable#Theme_windowNoTitle 8416ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main android:windowNoTitle}</dt> 8426ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main <dd>Set this style property {@code true} to also remove the traditional title bar.</dd> 8436ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main</dl> 8446ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 8456ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main<p>For more information about using themes in your application, read <a 8466ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Mainhref="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>.</p> 8476ea0d2c0455ea43ffaf90c1e239cb7d6a3d49d2aScott Main 84850403a203357ec9e003b198728f89278c88d4f0fScott Main 84950403a203357ec9e003b198728f89278c88d4f0fScott Main 85050403a203357ec9e003b198728f89278c88d4f0fScott Main 851