actionbar.jd revision 18439bee6f8b525abe3f1fac69bc4cea184e1565
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>
1118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>A replacement for the title bar for displaying global actions for the activity</li>
1218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>Provides toolbar actions and modes of navigating around the application</li>
1318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>Switches to contextual menu options when one or more items are selected</li>
1418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>Requires API Level HONEYCOMB</li>
1518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </ul>
1618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
1718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <h2>In this document</h2>
1818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <ol>
1918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="#Adding">Adding the Action Bar</a></li>
2018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="#ActionItems">Adding Action Items</a>
2118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main      <ol>
2218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        <li><a href="#Home">Using the application icon as an action item</a></li>
2318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main      </ol>
2418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    </li>
2518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="#Tabs">Adding Tabs</a></li>
2618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="#Dropdown">Adding Drop-down Navigation</a></li>
2718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="#Search">Adding Search</a></li>
2818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </ol>
2918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
3018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <h2>Key classes</h2>
3118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <ol>
3218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>{@link android.app.ActionBar}</li>
3318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li>{@link android.view.Menu}</li>
3418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </ol>
3518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
3618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <h2>See also</h2>
3718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <ol>
3818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <li><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a></li>
3918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </ol>
4018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div>
4118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div>
4218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
4318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>The action bar is a widget for activities that replaces the traditional title bar at
4418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe top of an activity. By default, the action bar includes the application logo on the left side,
4518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainfollowed by the activity title. The action bar offers several useful features for
4618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainapplications&mdash;especially those targeted to tablet devices. The action bar features include
4718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe ability to:</p>
4818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
4918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<ul>
5018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Display menu items from the <a
5118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/ui/menus.html#OptionsMenu">options menu</a> as "action
5218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainitems"&mdash;providing instant access to key user actions.</li>
5318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Provide tabs for navigating between <a
5418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/fragments/index.html">fragments</a>.</li>
5518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Provide drop-down navigation items.</li>
5618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Embed a {@link android.widget.SearchView} for instant searching.</li>
5718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Use the application logo as a "return home" or "up" navigation action.</li>
5818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</ul>
5918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
6018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<img src="{@docRoot}images/ui/actionbar.png" height="36" alt="" />
6118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="img-caption"><strong>Figure 1.</strong> A screenshot of the action bar in the NotePad
6218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainsample application, containing action items to save and delete the note.</p>
6318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
6418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
6518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="Adding">Adding the Action Bar</h2>
6618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
6718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>To add the Action Bar to your activity, apply the holographic theme&mdash;{@code
6818439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainTheme.Holo}&mdash;or the action bar theme&mdash;{@code Theme.WithActionBar}&mdash;in your manifest
6918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainfile. For example:</p>
7018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
7118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
7218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&lt;activity android:theme="@android:style/Theme.Holo" &gt;
7318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
7418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
7518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>The activity now appears with the action bar in place of the traditional title bar.</p>
7618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
7718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
7818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="ActionItems">Adding Action Items</h2>
7918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
8018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For each action item you want to add to the action bar, you must add a menu item to the
8118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainactivity's <a href="{@docRoot}guide/topics/ui/menus.html#OptionsMenu">options menu</a> and declare
8218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat the item be shown as an action, using the {@code android:showAsAction} attribute in the menu
8318439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainXML or with {@link android.view.MenuItem#setShowAsAction setShowAsAction()} on the {@link
8418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.view.MenuItem}.</p>
8518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
8618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div class="figure" style="width:359px">
8718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <img src="{@docRoot}images/ui/actionbar-item-withtext.png" height="57" alt="" />
8818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <p class="img-caption"><strong>Figure 2.</strong> A screenshot from an action bar with two
8918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainaction items.</p>
9018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div>
9118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
9218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>You can specify a menu item to appear as an action item in the action bar&mdash;if there is room
9318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainfor it&mdash;from the <a href="{@docRoot}guide/topics/resources/menu-resource.html">menu
9418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainresource</a> by declaring {@code
9518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid:showAsAction="ifRoom"} for the {@code &lt;item&gt;} element. This way, the item will display
9618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainin the action bar for quick access only if there is room available for it&mdash;if there's not
9718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainenough room, the item is placed the options menu (revealed by the drop-down icon on the right side
9818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainof the action bar). From your application code, you can specify the item to appear as an action item
9918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainby calling {@link android.view.MenuItem#setShowAsAction setShowAsAction()} on the {@link
10018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.view.MenuItem} and passing {@link android.view.MenuItem#SHOW_AS_ACTION_IF_ROOM}.</p>
10118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
10218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>If your item supplies both a title and an icon, then the action item shows only
10318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe icon by defult. If you want to include the text with the action item, add the <em>with text</em>
10418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainflag&mdash;in XML, add {@code withText} to the {@code android:showAsAction} attribute or, in
10518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainyour application code, use the {@link android.view.MenuItem#SHOW_AS_ACTION_WITH_TEXT} flag when
10618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincalling {@link android.view.MenuItem#setShowAsAction setShowAsAction()}. Figure 2 shows a screenshot
10718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainof an action bar with two action items that include text.</p>
10818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
10918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>Here's an example of how you can declare a menu item as an action item in a <a
11018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/resources/menu-resource.html">menu resource</a> file:</p>
11118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
11218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&lt;?xml version="1.0" encoding="utf-8"?&gt;
11318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&lt;menu xmlns:android="http://schemas.android.com/apk/res/android">
11418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    &lt;item android:id="@+id/menu_add"
11518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main          android:icon="@drawable/ic_menu_save"
11618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main          android:title="@string/menu_save"
11718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main          <b>android:showAsAction="ifRoom|withText"</b> /&gt;
11818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&lt;/menu&gt;
11918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
12018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
12118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>In this case, both the {@code ifRoom} and {@code withText} flags are set, so that when this
12218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainitem appears as an action item, it includes the title text along with the icon.</p>
12318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
12418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>A menu item placed in the action bar triggers the same callback methods as other items in the
12518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainoptions menu. When the user selects an item in the action bar, your activity receives a call to
12618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()}, passing the
12718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainitem ID. (If you added the item from a fragment, then the respective {@link
12818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Fragment#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} method is called
12918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainfor that fragment.)</p>
13018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
13118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="note"><strong>Note:</strong> Even menu items that are contained in the options menu
13218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main(and not shown as action items) will show an icon, so when using the action bar, you should
13318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprovide an icon for every item in the options menu.</p>
13418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
13518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>You can also declare an item to <em>always</em> appear as an action item,  but you should avoid
13618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maindoing so. Most of the time, there will be enough room for several action items and they will appear
13718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainin the order you declare them. If you set items to always appear as action
13818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainitems  (instead of <em>if room</em>), then they are added without discrimination and there is a risk
13918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat they will collide with other elements in the action bar, such as tabs or custom views.</p>
14018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
14118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For more information about menus, see the <a
14218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/ui/menus.html#options-menu">Creating Menus</a> developer guide.</p>
14318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
14418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
14518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h3 id="Home">Using the application icon as an action item</h3>
14618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
14718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>By default, the application icon appears in the action bar on the left side, but does nothing
14818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainwhen tapped. To use the application icon as an action item when tapped, you simply need to add a
14918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincondition to your {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()} method
15018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat performs an action when the {@link android.view.MenuItem} ID is {@code android.R.id.home}.
15118439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainThis ID is delivered every time the user taps the application icon.</p>
15218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
15318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For example, here's an implementation of {@link android.app.Activity#onOptionsItemSelected
15418439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainonOptionsItemSelected()} that returns to the application's "home" activity:</p>
15518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
15618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
15718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&#64;Override
15818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainpublic boolean onOptionsItemSelected(MenuItem item) {
15918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    switch (item.getItemId()) {
16018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        case android.R.id.home:
16118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            // app icon in action bar clicked; go home
16218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            Intent intent = new Intent(this, HomeActivity.class);
16318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            startActivity(intent);
16418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            break;
16518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    }
16618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    return super.onOptionsItemSelected(item);
16718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main}
16818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
16918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
17018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<div class="figure" style="width:144px">
17118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <img src="{@docRoot}images/ui/actionbar-logo.png" height="140" alt="" />
17218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <p class="img-caption"><strong>Figure 3.</strong> The standard icon for the Email application
17318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main(top) and the "up navigation" icon (bottom).</p>
17418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</div>
17518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
17618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>You can also use the application icon to provide "up" navigation. The way you handle the event
17718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainwhen a user taps the icon is the same, but if the user experience for the event is to <em>navigate
17818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainup to the parent activity</em>, then you should indicate this behavior by setting the
17918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainaction bar to "show home as up." You can do so by calling {@link
18018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#setDisplayOptions setDisplayOptions()} on your activity's {@link
18118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar}, and passing the {@link
18218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#DISPLAY_HOME_AS_UP} display option.</p>
18318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
18418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>To get the {@link android.app.ActionBar}, call {@link android.app.Activity#getActionBar} from
18518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainyour {@link android.app.Activity} during {@link android.app.Activity#onCreate onCreate()} (but be
18618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainsure you do so <em>after</em> you've called {@link android.app.Activity#setContentView
18718439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetContentView()}).</p>
18818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
18918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For example, here's how you can change the action bar display mode to show the application
19018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainicon as an "up" action:</p>
19118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
19218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
19318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&#64;Override
19418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprotected void onStart() {
19518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  super.onStart();
19618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  ActionBar actionBar = this.getActionBar();
19718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
19818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main}
19918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
20018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
20118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="caution"><strong>Caution:</strong> If your activity does not have an action bar (if you
20218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maindid not set the theme of your activity or application to the holographic or action bar theme), then
20318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.Activity#getActionBar} returns null.</p>
20418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
20518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
20618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<h2 id="Tabs">Adding Tabs</h2>
20718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
20818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>The action bar can display tabs that allow the user navigate between different fragments in the
20918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainactivity. Each tab can include a title and/or an icon.</p>
21018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
21118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>To begin, your layout must include a {@link android.view.View} in which each {@link
21218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Fragment} associated with a tab is displayed. Be sure the view has an ID that you
21318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincan use to reference it from your code.</p>
21418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
21518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>To add tabs to the action bar:</p>
21618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<ol>
21718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Create an implementation of {@link android.app.ActionBar.TabListener} to handle the
21818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maininteraction events on the action bar tabs. You must implement all methods: {@link
21918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabSelected onTabSelected()}, {@link
22018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabUnselected onTabUnselected()}, and {@link
22118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener#onTabReselected onTabReselected()}.
22218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <p>Each callback method passes the {@link android.app.ActionBar.Tab} that received the
22318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainevent and a {@link android.app.FragmentTransaction} for you to perform the fragment
22418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maintransactions (add or remove fragments).</p>
22518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <p>For example:</p>
22618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
22718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprivate class MyTabListener implements ActionBar.TabListener {
22818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    private TabContentFragment mFragment;
22918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
23018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    // Called to create an instance of the listener when adding a new tab
23118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    public TabListener(TabContentFragment fragment) {
23218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        mFragment = fragment;
23318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    }
23418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
23518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    &#64;Override
23618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    public void onTabSelected(Tab tab, FragmentTransaction ft) {
23718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        ft.add(R.id.fragment_content, mFragment, null);
23818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    }
23918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
24018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    &#64;Override
24118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
24218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        ft.remove(mFragment);
24318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    }
24418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
24518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    &#64;Override
24618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    public void onTabReselected(Tab tab, FragmentTransaction ft) {
24718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        // do nothing
24818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    }
24918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
25018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main}
25118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
25218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <p>This implementation of {@link android.app.ActionBar.TabListener} adds a constructor
25318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat saves the {@link android.app.Fragment} associated with a tab so that each callback
25418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maincan add or remove that fragment.</p>
25518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </li>
25618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Get the {@link android.app.ActionBar} for your activity by calling {@link
25718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Activity#getActionBar} from your {@link android.app.Activity}, during {@link
25818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.Activity#onCreate onCreate()} (but be sure you do so <em>after</em> you've called
25918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.Activity#setContentView setContentView()}).</li>
26018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Call {@link android.app.ActionBar#setNavigationMode(int)
26118439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetNavigationMode(NAVIGATION_MODE_TABS)} to enable tab mode for the {@link
26218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar}.</li>
26318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Create each tab for the action bar:
26418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <ol>
26518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main      <li>Create a new {@link android.app.ActionBar.Tab} by calling {@link
26618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#newTab()} on the {@link android.app.ActionBar}.</li>
26718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main      <li>Add title text and/or an icon for the tab by calling {@link
26818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.Tab#setText setText()} and/or {@link android.app.ActionBar.Tab#setIcon
26918439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetIcon()}.
27018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main        <p class="note"><strong>Tip:</strong> These methods return the same {@link
27118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.Tab} instance, so you can chain the calls together.</p></li>
27218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main      <li>Declare the {@link android.app.ActionBar.TabListener} to use for the tab by passing an
27318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maininstance of your implementation to {@link android.app.ActionBar.Tab#setTabListener
27418439bee6f8b525abe3f1fac69bc4cea184e1565Scott MainsetTabListener()}.
27518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    </ol>
27618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  </li>
27718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main  <li>Add each {@link android.app.ActionBar.Tab} to the action bar by calling {@link
27818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#addTab addTab()} on the {@link android.app.ActionBar} and passing the
27918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.ActionBar.Tab}.</li>
28018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</ol>
28118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>For example, the following code combines steps 2 - 5 to create two tabs and add them to
28218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe action bar:</p>
28318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<pre>
28418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main&#64;Override
28518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainprotected void onCreate(Bundle savedInstanceState) {
28618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    super.onCreate(savedInstanceState);
28718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    setContentView(R.layout.main);
28818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
28918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    // setup action bar for tabs
29018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    final ActionBar actionBar = getActionBar();
29118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
29218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    // remove the activity title to make space for tabs
29318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
29418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
29518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    // instantiate fragment for the tab
29618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    Fragment artistsFragment = new ArtistsFragment();
29718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    // add a new tab and set its title text and tab listener
29818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    bar.addTab(bar.newTab().setText(R.string.tab_artists)
29918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            .setTabListener(new TabListener(artistsFragment)));
30018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
30118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    Fragment albumsFragment = new AlbumsFragment();
30218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    bar.addTab(bar.newTab().setText(R.string.tab_albums)
30318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main            .setTabListener(new TabListener(albumsFragment)));
30418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main}
30518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main</pre>
30618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
30718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>All the behaviors that occur when a tab is selected must be defined by your {@link
30818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar.TabListener} callback methods. When a tab is selected, it receives a call to
30918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main{@link android.app.ActionBar.TabListener#onTabSelected onTabSelected()} and that's where you should
31018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainadd the appropriate fragment to the designated view in your layout, using {@link
31118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction#add add()} with the provided {@link
31218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction}. Likewise, when a tab is deselected (because another tab becomes
31318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainselected), you should remove that fragment from the layout, using {@link
31418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.FragmentTransaction#remove remove()}.</p>
31518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
31618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="note"><strong>Note:</strong> You <strong>do not</strong> need
31718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainto call {@link android.app.FragmentTransaction#commit} for these transactions. You also
31818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<strong>cannot</strong> add these fragment transactions to the back stack.</p>
31918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
32018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p>If your activity is stopped, you should retain the currently selected tab with the saved state so
32118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthat when the user returns to your application, you can open the tab. When it's time to save the
32218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainstate, you can query the currently selected tab with {@link
32318439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainandroid.app.ActionBar#getSelectedNavigationItem()}. This returns the index position of the selected
32418439bee6f8b525abe3f1fac69bc4cea184e1565Scott Maintab.</p>
32518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
32618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="caution"><strong>Caution:</strong> It's important that you save
32718439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe state of each fragment as necessary, so when the user switches fragments with the tabs,
32818439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthen returns to a previous fragment, it appears the way they left. For information about saving
32918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainthe state of your fragment, see the <a
33018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainhref="{@docRoot}guide/topics/fragments/index.html">Fragments</a> developer guide.</p>
33118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
33218439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
333