• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.v7.app;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.drawable.Drawable;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentManager;
24 import android.support.v4.app.FragmentTransaction;
25 import android.support.v7.appcompat.R;
26 import android.util.AttributeSet;
27 import android.view.Gravity;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.ViewGroup.MarginLayoutParams;
31 import android.view.Window;
32 import android.widget.SpinnerAdapter;
33 
34 /**
35  * A window feature at the top of the activity that may display the activity title, navigation
36  * modes, and other interactive items.
37  *
38  * <p class="note"><strong>Note:</strong> This class is included in the <a
39  * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
40  * with API level 7 and higher. If you're developing your app for API level 11 and higher
41  * <em>only</em>, you should instead use the framework {@link android.app.ActionBar} class.</p>
42  *
43  * <p>When using the support library, you can add the action bar to the top of your activity
44  * window by extending the {@link ActionBarActivity} class and setting the activity theme to
45  * {@link android.support.v7.appcompat.R.style#Theme_AppCompat Theme.AppCompat} or a similar theme.
46  *
47  * <p>By default, the action bar shows the application icon on
48  * the left, followed by the activity title. If your activity has an options menu, you can make
49  * select items accessible directly from the action bar as "action items". You can also
50  * modify various characteristics of the action bar or remove it completely.</p>
51  *
52  * <p>From your activity, you can retrieve an instance of {@link ActionBar} by calling {@link
53  * android.support.v7.app.ActionBarActivity#getSupportActionBar}.</p>
54  *
55  * <p>In some cases, the action bar may be overlayed by another bar that enables contextual actions,
56  * using an {@link android.support.v7.view.ActionMode}. For example, when the user selects one or
57  * more items in your activity, you can enable an action mode that offers actions specific to the
58  * selected items, with a UI that temporarily replaces the action bar. Although the UI may occupy
59  * the same space, the {@link android.support.v7.view.ActionMode} APIs are distinct and independent
60  * from those for {@link ActionBar}.
61  *
62  * <div class="special reference">
63  * <h3>Developer Guides</h3>
64  *
65  * <p>For information about how to use the action bar, including how to add action items, navigation
66  * modes and more, read the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action
67  * Bar</a> API guide.</p>
68  * </div>
69  */
70 public abstract class ActionBar {
71 
72     /**
73      * Standard navigation mode. Consists of either a logo or icon and title text with an optional
74      * subtitle. Clicking any of these elements will dispatch onOptionsItemSelected to the host
75      * Activity with a MenuItem with item ID android.R.id.home.
76      */
77     public static final int NAVIGATION_MODE_STANDARD = 0;
78 
79     /**
80      * List navigation mode. Instead of static title text this mode presents a list menu for
81      * navigation within the activity. e.g. this might be presented to the user as a dropdown list.
82      */
83     public static final int NAVIGATION_MODE_LIST = 1;
84 
85     /**
86      * Tab navigation mode. Instead of static title text this mode presents a series of tabs for
87      * navigation within the activity.
88      */
89     public static final int NAVIGATION_MODE_TABS = 2;
90 
91     /**
92      * Use logo instead of icon if available. This flag will cause appropriate navigation modes to
93      * use a wider logo in place of the standard icon.
94      *
95      * @see #setDisplayOptions(int)
96      * @see #setDisplayOptions(int, int)
97      */
98     public static final int DISPLAY_USE_LOGO = 0x1;
99 
100     /**
101      * Show 'home' elements in this action bar, leaving more space for other navigation elements.
102      * This includes logo and icon.
103      *
104      * @see #setDisplayOptions(int)
105      * @see #setDisplayOptions(int, int)
106      */
107     public static final int DISPLAY_SHOW_HOME = 0x2;
108 
109     /**
110      * Display the 'home' element such that it appears as an 'up' affordance. e.g. show an arrow to
111      * the left indicating the action that will be taken.
112      *
113      * Set this flag if selecting the 'home' button in the action bar to return up by a single level
114      * in your UI rather than back to the top level or front page.
115      *
116      * <p>Setting this option will implicitly enable interaction with the home/up button. See {@link
117      * #setHomeButtonEnabled(boolean)}.
118      *
119      * @see #setDisplayOptions(int)
120      * @see #setDisplayOptions(int, int)
121      */
122     public static final int DISPLAY_HOME_AS_UP = 0x4;
123 
124     /**
125      * Show the activity title and subtitle, if present.
126      *
127      * @see #setTitle(CharSequence)
128      * @see #setTitle(int)
129      * @see #setSubtitle(CharSequence)
130      * @see #setSubtitle(int)
131      * @see #setDisplayOptions(int)
132      * @see #setDisplayOptions(int, int)
133      */
134     public static final int DISPLAY_SHOW_TITLE = 0x8;
135 
136     /**
137      * Show the custom view if one has been set.
138      *
139      * @see #setCustomView(View)
140      * @see #setDisplayOptions(int)
141      * @see #setDisplayOptions(int, int)
142      */
143     public static final int DISPLAY_SHOW_CUSTOM = 0x10;
144 
145     /**
146      * Set the action bar into custom navigation mode, supplying a view for custom navigation.
147      *
148      * Custom navigation views appear between the application icon and any action buttons and may
149      * use any space available there. Common use cases for custom navigation views might include an
150      * auto-suggesting address bar for a browser or other navigation mechanisms that do not
151      * translate well to provided navigation modes.
152      *
153      * @param view Custom navigation view to place in the ActionBar.
154      */
setCustomView(View view)155     public abstract void setCustomView(View view);
156 
157     /**
158      * Set the action bar into custom navigation mode, supplying a view for custom navigation.
159      *
160      * <p>Custom navigation views appear between the application icon and any action buttons and may
161      * use any space available there. Common use cases for custom navigation views might include an
162      * auto-suggesting address bar for a browser or other navigation mechanisms that do not
163      * translate well to provided navigation modes.</p>
164      *
165      * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for the custom view to be
166      * displayed.</p>
167      *
168      * @param view         Custom navigation view to place in the ActionBar.
169      * @param layoutParams How this custom view should layout in the bar.
170      * @see #setDisplayOptions(int, int)
171      */
setCustomView(View view, LayoutParams layoutParams)172     public abstract void setCustomView(View view, LayoutParams layoutParams);
173 
174     /**
175      * Set the action bar into custom navigation mode, supplying a view for custom navigation.
176      *
177      * <p>Custom navigation views appear between the application icon and any action buttons and may
178      * use any space available there. Common use cases for custom navigation views might include an
179      * auto-suggesting address bar for a browser or other navigation mechanisms that do not
180      * translate well to provided navigation modes.</p>
181      *
182      * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for the custom view to be
183      * displayed.</p>
184      *
185      * @param resId Resource ID of a layout to inflate into the ActionBar.
186      * @see #setDisplayOptions(int, int)
187      */
setCustomView(int resId)188     public abstract void setCustomView(int resId);
189 
190     /**
191      * Set the icon to display in the 'home' section of the action bar. The action bar will use an
192      * icon specified by its style or the activity icon by default.
193      *
194      * Whether the home section shows an icon or logo is controlled by the display option {@link
195      * #DISPLAY_USE_LOGO}.
196      *
197      * @param resId Resource ID of a drawable to show as an icon.
198      * @see #setDisplayUseLogoEnabled(boolean)
199      * @see #setDisplayShowHomeEnabled(boolean)
200      */
setIcon(int resId)201     public abstract void setIcon(int resId);
202 
203     /**
204      * Set the icon to display in the 'home' section of the action bar. The action bar will use an
205      * icon specified by its style or the activity icon by default.
206      *
207      * Whether the home section shows an icon or logo is controlled by the display option {@link
208      * #DISPLAY_USE_LOGO}.
209      *
210      * @param icon Drawable to show as an icon.
211      * @see #setDisplayUseLogoEnabled(boolean)
212      * @see #setDisplayShowHomeEnabled(boolean)
213      */
setIcon(Drawable icon)214     public abstract void setIcon(Drawable icon);
215 
216     /**
217      * Set the logo to display in the 'home' section of the action bar. The action bar will use a
218      * logo specified by its style or the activity logo by default.
219      *
220      * Whether the home section shows an icon or logo is controlled by the display option {@link
221      * #DISPLAY_USE_LOGO}.
222      *
223      * @param resId Resource ID of a drawable to show as a logo.
224      * @see #setDisplayUseLogoEnabled(boolean)
225      * @see #setDisplayShowHomeEnabled(boolean)
226      */
setLogo(int resId)227     public abstract void setLogo(int resId);
228 
229     /**
230      * Set the logo to display in the 'home' section of the action bar. The action bar will use a
231      * logo specified by its style or the activity logo by default.
232      *
233      * Whether the home section shows an icon or logo is controlled by the display option {@link
234      * #DISPLAY_USE_LOGO}.
235      *
236      * @param logo Drawable to show as a logo.
237      * @see #setDisplayUseLogoEnabled(boolean)
238      * @see #setDisplayShowHomeEnabled(boolean)
239      */
setLogo(Drawable logo)240     public abstract void setLogo(Drawable logo);
241 
242     /**
243      * Set the adapter and navigation callback for list navigation mode.
244      *
245      * The supplied adapter will provide views for the expanded list as well as the currently
246      * selected item. (These may be displayed differently.)
247      *
248      * The supplied OnNavigationListener will alert the application when the user changes the
249      * current list selection.
250      *
251      * @param adapter  An adapter that will provide views both to display the current navigation
252      *                 selection and populate views within the dropdown navigation menu.
253      * @param callback An OnNavigationListener that will receive events when the user selects a
254      *                 navigation item.
255      */
setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback)256     public abstract void setListNavigationCallbacks(SpinnerAdapter adapter,
257             OnNavigationListener callback);
258 
259     /**
260      * Set the selected navigation item in list or tabbed navigation modes.
261      *
262      * @param position Position of the item to select.
263      */
setSelectedNavigationItem(int position)264     public abstract void setSelectedNavigationItem(int position);
265 
266     /**
267      * Get the position of the selected navigation item in list or tabbed navigation modes.
268      *
269      * @return Position of the selected item.
270      */
getSelectedNavigationIndex()271     public abstract int getSelectedNavigationIndex();
272 
273     /**
274      * Get the number of navigation items present in the current navigation mode.
275      *
276      * @return Number of navigation items.
277      */
getNavigationItemCount()278     public abstract int getNavigationItemCount();
279 
280     /**
281      * Set the action bar's title. This will only be displayed if {@link #DISPLAY_SHOW_TITLE} is
282      * set.
283      *
284      * @param title Title to set
285      * @see #setTitle(int)
286      * @see #setDisplayOptions(int, int)
287      */
setTitle(CharSequence title)288     public abstract void setTitle(CharSequence title);
289 
290     /**
291      * Set the action bar's title. This will only be displayed if {@link #DISPLAY_SHOW_TITLE} is
292      * set.
293      *
294      * @param resId Resource ID of title string to set
295      * @see #setTitle(CharSequence)
296      * @see #setDisplayOptions(int, int)
297      */
setTitle(int resId)298     public abstract void setTitle(int resId);
299 
300     /**
301      * Set the action bar's subtitle. This will only be displayed if {@link #DISPLAY_SHOW_TITLE} is
302      * set. Set to null to disable the subtitle entirely.
303      *
304      * @param subtitle Subtitle to set
305      * @see #setSubtitle(int)
306      * @see #setDisplayOptions(int, int)
307      */
setSubtitle(CharSequence subtitle)308     public abstract void setSubtitle(CharSequence subtitle);
309 
310     /**
311      * Set the action bar's subtitle. This will only be displayed if {@link #DISPLAY_SHOW_TITLE} is
312      * set.
313      *
314      * @param resId Resource ID of subtitle string to set
315      * @see #setSubtitle(CharSequence)
316      * @see #setDisplayOptions(int, int)
317      */
setSubtitle(int resId)318     public abstract void setSubtitle(int resId);
319 
320     /**
321      * Set display options. This changes all display option bits at once. To change a limited subset
322      * of display options, see {@link #setDisplayOptions(int, int)}.
323      *
324      * @param options A combination of the bits defined by the DISPLAY_ constants defined in
325      *                ActionBar.
326      */
setDisplayOptions(int options)327     public abstract void setDisplayOptions(int options);
328 
329     /**
330      * Set selected display options. Only the options specified by mask will be changed. To change
331      * all display option bits at once, see {@link #setDisplayOptions(int)}.
332      *
333      * <p>Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the {@link
334      * #DISPLAY_SHOW_HOME} option. setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME |
335      * DISPLAY_USE_LOGO) will enable {@link #DISPLAY_SHOW_HOME} and disable {@link
336      * #DISPLAY_USE_LOGO}.
337      *
338      * @param options A combination of the bits defined by the DISPLAY_ constants defined in
339      *                ActionBar.
340      * @param mask    A bit mask declaring which display options should be changed.
341      */
setDisplayOptions(int options, int mask)342     public abstract void setDisplayOptions(int options, int mask);
343 
344     /**
345      * Set whether to display the activity logo rather than the activity icon. A logo is often a
346      * wider, more detailed image.
347      *
348      * <p>To set several display options at once, see the setDisplayOptions methods.
349      *
350      * @param useLogo true to use the activity logo, false to use the activity icon.
351      * @see #setDisplayOptions(int)
352      * @see #setDisplayOptions(int, int)
353      */
setDisplayUseLogoEnabled(boolean useLogo)354     public abstract void setDisplayUseLogoEnabled(boolean useLogo);
355 
356     /**
357      * Set whether to include the application home affordance in the action bar. Home is presented
358      * as either an activity icon or logo.
359      *
360      * <p>To set several display options at once, see the setDisplayOptions methods.
361      *
362      * @param showHome true to show home, false otherwise.
363      * @see #setDisplayOptions(int)
364      * @see #setDisplayOptions(int, int)
365      */
setDisplayShowHomeEnabled(boolean showHome)366     public abstract void setDisplayShowHomeEnabled(boolean showHome);
367 
368     /**
369      * Set whether home should be displayed as an "up" affordance. Set this to true if selecting
370      * "home" returns up by a single level in your UI rather than back to the top level or front
371      * page.
372      *
373      * <p>To set several display options at once, see the setDisplayOptions methods.
374      *
375      * @param showHomeAsUp true to show the user that selecting home will return one level up rather
376      *                     than to the top level of the app.
377      * @see #setDisplayOptions(int)
378      * @see #setDisplayOptions(int, int)
379      */
setDisplayHomeAsUpEnabled(boolean showHomeAsUp)380     public abstract void setDisplayHomeAsUpEnabled(boolean showHomeAsUp);
381 
382     /**
383      * Set whether an activity title/subtitle should be displayed.
384      *
385      * <p>To set several display options at once, see the setDisplayOptions methods.
386      *
387      * @param showTitle true to display a title/subtitle if present.
388      * @see #setDisplayOptions(int)
389      * @see #setDisplayOptions(int, int)
390      */
setDisplayShowTitleEnabled(boolean showTitle)391     public abstract void setDisplayShowTitleEnabled(boolean showTitle);
392 
393     /**
394      * Set whether a custom view should be displayed, if set.
395      *
396      * <p>To set several display options at once, see the setDisplayOptions methods.
397      *
398      * @param showCustom true if the currently set custom view should be displayed, false
399      *                   otherwise.
400      * @see #setDisplayOptions(int)
401      * @see #setDisplayOptions(int, int)
402      */
setDisplayShowCustomEnabled(boolean showCustom)403     public abstract void setDisplayShowCustomEnabled(boolean showCustom);
404 
405     /**
406      * Set the ActionBar's background. This will be used for the primary action bar.
407      *
408      * @param d Background drawable
409      * @see #setStackedBackgroundDrawable(Drawable)
410      * @see #setSplitBackgroundDrawable(Drawable)
411      */
setBackgroundDrawable(Drawable d)412     public abstract void setBackgroundDrawable(Drawable d);
413 
414     /**
415      * Set the ActionBar's stacked background. This will appear in the second row/stacked bar on
416      * some devices and configurations.
417      *
418      * @param d Background drawable for the stacked row
419      */
setStackedBackgroundDrawable(Drawable d)420     public void setStackedBackgroundDrawable(Drawable d) {
421     }
422 
423     /**
424      * Set the ActionBar's split background. This will appear in the split action bar containing
425      * menu-provided action buttons on some devices and configurations
426      *
427      * <p>You can enable split action bar with {@link android.R.attr#uiOptions}
428      *
429      * @param d Background drawable for the split bar
430      */
setSplitBackgroundDrawable(Drawable d)431     public void setSplitBackgroundDrawable(Drawable d) {
432     }
433 
434     /**
435      * @return The current custom view.
436      */
getCustomView()437     public abstract View getCustomView();
438 
439     /**
440      * Returns the current ActionBar title in standard mode. Returns null if {@link
441      * #getNavigationMode()} would not return {@link #NAVIGATION_MODE_STANDARD}.
442      *
443      * @return The current ActionBar title or null.
444      */
getTitle()445     public abstract CharSequence getTitle();
446 
447     /**
448      * Returns the current ActionBar subtitle in standard mode. Returns null if {@link
449      * #getNavigationMode()} would not return {@link #NAVIGATION_MODE_STANDARD}.
450      *
451      * @return The current ActionBar subtitle or null.
452      */
getSubtitle()453     public abstract CharSequence getSubtitle();
454 
455     /**
456      * Returns the current navigation mode. The result will be one of:
457      *
458      * <ul><li>{@link
459      * #NAVIGATION_MODE_STANDARD}</li>
460      *
461      * <li>{@link #NAVIGATION_MODE_LIST}</li>
462      *
463      * <li>{@link #NAVIGATION_MODE_TABS}</li></ul>
464      *
465      * @return The current navigation mode.
466      */
getNavigationMode()467     public abstract int getNavigationMode();
468 
469     /**
470      * Set the current navigation mode.
471      *
472      * @param mode The new mode to set.
473      * @see #NAVIGATION_MODE_STANDARD
474      * @see #NAVIGATION_MODE_LIST
475      * @see #NAVIGATION_MODE_TABS
476      */
setNavigationMode(int mode)477     public abstract void setNavigationMode(int mode);
478 
479     /**
480      * @return The current set of display options.
481      */
getDisplayOptions()482     public abstract int getDisplayOptions();
483 
484     /**
485      * Create and return a new {@link Tab}. This tab will not be included in the action bar until it
486      * is added.
487      *
488      * <p>Very often tabs will be used to switch between {@link Fragment} objects.  Here is a
489      * typical implementation of such tabs:</p>
490      *
491      * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.java
492      * complete}
493      *
494      * @return A new Tab
495      * @see #addTab(Tab)
496      */
newTab()497     public abstract Tab newTab();
498 
499     /**
500      * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list. If
501      * this is the first tab to be added it will become the selected tab.
502      *
503      * @param tab Tab to add
504      */
addTab(Tab tab)505     public abstract void addTab(Tab tab);
506 
507     /**
508      * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
509      *
510      * @param tab         Tab to add
511      * @param setSelected True if the added tab should become the selected tab.
512      */
addTab(Tab tab, boolean setSelected)513     public abstract void addTab(Tab tab, boolean setSelected);
514 
515     /**
516      * Add a tab for use in tabbed navigation mode. The tab will be inserted at
517      * <code>position</code>. If this is the first tab to be added it will become the selected tab.
518      *
519      * @param tab      The tab to add
520      * @param position The new position of the tab
521      */
addTab(Tab tab, int position)522     public abstract void addTab(Tab tab, int position);
523 
524     /**
525      * Add a tab for use in tabbed navigation mode. The tab will be insterted at
526      * <code>position</code>.
527      *
528      * @param tab         The tab to add
529      * @param position    The new position of the tab
530      * @param setSelected True if the added tab should become the selected tab.
531      */
addTab(Tab tab, int position, boolean setSelected)532     public abstract void addTab(Tab tab, int position, boolean setSelected);
533 
534     /**
535      * Remove a tab from the action bar. If the removed tab was selected it will be deselected and
536      * another tab will be selected if present.
537      *
538      * @param tab The tab to remove
539      */
removeTab(Tab tab)540     public abstract void removeTab(Tab tab);
541 
542     /**
543      * Remove a tab from the action bar. If the removed tab was selected it will be deselected and
544      * another tab will be selected if present.
545      *
546      * @param position Position of the tab to remove
547      */
removeTabAt(int position)548     public abstract void removeTabAt(int position);
549 
550     /**
551      * Remove all tabs from the action bar and deselect the current tab.
552      */
removeAllTabs()553     public abstract void removeAllTabs();
554 
555     /**
556      * Select the specified tab. If it is not a child of this action bar it will be added.
557      *
558      * <p>Note: If you want to select by index, use {@link #setSelectedNavigationItem(int)}.</p>
559      *
560      * @param tab Tab to select
561      */
selectTab(Tab tab)562     public abstract void selectTab(Tab tab);
563 
564     /**
565      * Returns the currently selected tab if in tabbed navigation mode and there is at least one tab
566      * present.
567      *
568      * @return The currently selected tab or null
569      */
getSelectedTab()570     public abstract Tab getSelectedTab();
571 
572     /**
573      * Returns the tab at the specified index.
574      *
575      * @param index Index value in the range 0-get
576      */
getTabAt(int index)577     public abstract Tab getTabAt(int index);
578 
579     /**
580      * Returns the number of tabs currently registered with the action bar.
581      *
582      * @return Tab count
583      */
getTabCount()584     public abstract int getTabCount();
585 
586     /**
587      * Retrieve the current height of the ActionBar.
588      *
589      * @return The ActionBar's height
590      */
getHeight()591     public abstract int getHeight();
592 
593     /**
594      * Show the ActionBar if it is not currently showing. If the window hosting the ActionBar does
595      * not have the feature {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application
596      * content to fit the new space available.
597      *
598      * <p>If you are hiding the ActionBar through {@link View#SYSTEM_UI_FLAG_FULLSCREEN
599      * View.SYSTEM_UI_FLAG_FULLSCREEN}, you should not call this function directly.
600      */
show()601     public abstract void show();
602 
603     /**
604      * Hide the ActionBar if it is currently showing. If the window hosting the ActionBar does not
605      * have the feature {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application content
606      * to fit the new space available.
607      *
608      * <p>Instead of calling this function directly, you can also cause an ActionBar using the
609      * overlay feature to hide through {@link View#SYSTEM_UI_FLAG_FULLSCREEN
610      * View.SYSTEM_UI_FLAG_FULLSCREEN}. Hiding the ActionBar through this system UI flag allows you
611      * to more seamlessly hide it in conjunction with other screen decorations.
612      */
hide()613     public abstract void hide();
614 
615     /**
616      * @return <code>true</code> if the ActionBar is showing, <code>false</code> otherwise.
617      */
isShowing()618     public abstract boolean isShowing();
619 
620     /**
621      * Add a listener that will respond to menu visibility change events.
622      *
623      * @param listener The new listener to add
624      */
addOnMenuVisibilityListener(OnMenuVisibilityListener listener)625     public abstract void addOnMenuVisibilityListener(OnMenuVisibilityListener listener);
626 
627     /**
628      * Remove a menu visibility listener. This listener will no longer receive menu visibility
629      * change events.
630      *
631      * @param listener A listener to remove that was previously added
632      */
removeOnMenuVisibilityListener(OnMenuVisibilityListener listener)633     public abstract void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener);
634 
635     /**
636      * Enable or disable the "home" button in the corner of the action bar. (Note that this is the
637      * application home/up affordance on the action bar, not the systemwide home button.)
638      *
639      * <p>This defaults to true for packages targeting &lt; API 14. For packages targeting API 14 or
640      * greater, the application should call this method to enable interaction with the home/up
641      * affordance.
642      *
643      * <p>Setting the {@link #DISPLAY_HOME_AS_UP} display option will automatically enable the home
644      * button.
645      *
646      * @param enabled true to enable the home button, false to disable the home button.
647      */
setHomeButtonEnabled(boolean enabled)648     public void setHomeButtonEnabled(boolean enabled) {
649     }
650 
651     /**
652      * Returns a {@link Context} with an appropriate theme for creating views that will appear in
653      * the action bar. If you are inflating or instantiating custom views that will appear in an
654      * action bar, you should use the Context returned by this method. (This includes adapters used
655      * for list navigation mode.) This will ensure that views contrast properly against the action
656      * bar.
657      *
658      * @return A themed Context for creating views
659      */
getThemedContext()660     public Context getThemedContext() {
661         return null;
662     }
663 
664     /**
665      * Listener for receiving {@link ActionBar} navigation events.
666      *
667      * <p class="note"><strong>Note:</strong> This interface is included in the <a
668      * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
669      * with API level 7 and higher. If you're developing your app for API level 11 and higher
670      * <em>only</em>, you should instead use the framework {@link
671      * android.app.ActionBar.OnNavigationListener} interface.</p>
672      */
673     public interface OnNavigationListener {
674 
675         /**
676          * This method is called whenever a navigation item in your action bar is selected.
677          *
678          * @param itemPosition Position of the item clicked.
679          * @param itemId       ID of the item clicked.
680          * @return True if the event was handled, false otherwise.
681          */
onNavigationItemSelected(int itemPosition, long itemId)682         public boolean onNavigationItemSelected(int itemPosition, long itemId);
683     }
684 
685     /**
686      * Listener for receiving events when {@link ActionBar} items are shown or hidden.
687      *
688      * <p class="note"><strong>Note:</strong> This interface is included in the <a
689      * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
690      * with API level 7 and higher. If you're developing your app for API level 11 and higher
691      * <em>only</em>, you should instead use the framework {@link
692      * android.app.ActionBar.OnMenuVisibilityListener} interface.</p>
693      */
694     public interface OnMenuVisibilityListener {
695 
696         /**
697          * Called when an action bar menu is shown or hidden. Applications may want to use this to
698          * tune auto-hiding behavior for the action bar or pause/resume video playback, gameplay, or
699          * other activity within the main content area.
700          *
701          * @param isVisible True if an action bar menu is now visible, false if no action bar menus
702          *                  are visible.
703          */
onMenuVisibilityChanged(boolean isVisible)704         public void onMenuVisibilityChanged(boolean isVisible);
705     }
706 
707     /**
708      * A tab in the action bar that manages the hiding and showing of {@link Fragment}s.
709      *
710      * <p class="note"><strong>Note:</strong> This class is included in the <a
711      * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
712      * with API level 7 and higher. If you're developing your app for API level 11 and higher
713      * <em>only</em>, you should instead use the framework {@link android.app.ActionBar.Tab}
714      * class.</p>
715      *
716      * <div class="special reference">
717      * <h3>Developer Guides</h3>
718      *
719      * <p>For information about how to use action bar tabs,
720      * read the <a href="{@docRoot}guide/topics/ui/actionbar.html#Tabs">Action
721      * Bar</a> API guide.</p>
722      * </div>
723      */
724     public static abstract class Tab {
725 
726         /**
727          * An invalid position for a tab.
728          *
729          * @see #getPosition()
730          */
731         public static final int INVALID_POSITION = -1;
732 
733         /**
734          * Return the current position of this tab in the action bar.
735          *
736          * @return Current position, or {@link #INVALID_POSITION} if this tab is not currently in
737          *         the action bar.
738          */
getPosition()739         public abstract int getPosition();
740 
741         /**
742          * Return the icon associated with this tab.
743          *
744          * @return The tab's icon
745          */
getIcon()746         public abstract Drawable getIcon();
747 
748         /**
749          * Return the text of this tab.
750          *
751          * @return The tab's text
752          */
getText()753         public abstract CharSequence getText();
754 
755         /**
756          * Set the icon displayed on this tab.
757          *
758          * @param icon The drawable to use as an icon
759          * @return The current instance for call chaining
760          */
setIcon(Drawable icon)761         public abstract Tab setIcon(Drawable icon);
762 
763         /**
764          * Set the icon displayed on this tab.
765          *
766          * @param resId Resource ID referring to the drawable to use as an icon
767          * @return The current instance for call chaining
768          */
setIcon(int resId)769         public abstract Tab setIcon(int resId);
770 
771         /**
772          * Set the text displayed on this tab. Text may be truncated if there is not room to display
773          * the entire string.
774          *
775          * @param text The text to display
776          * @return The current instance for call chaining
777          */
setText(CharSequence text)778         public abstract Tab setText(CharSequence text);
779 
780         /**
781          * Set the text displayed on this tab. Text may be truncated if there is not room to display
782          * the entire string.
783          *
784          * @param resId A resource ID referring to the text that should be displayed
785          * @return The current instance for call chaining
786          */
setText(int resId)787         public abstract Tab setText(int resId);
788 
789         /**
790          * Set a custom view to be used for this tab. This overrides values set by {@link
791          * #setText(CharSequence)} and {@link #setIcon(Drawable)}.
792          *
793          * @param view Custom view to be used as a tab.
794          * @return The current instance for call chaining
795          */
setCustomView(View view)796         public abstract Tab setCustomView(View view);
797 
798         /**
799          * Set a custom view to be used for this tab. This overrides values set by {@link
800          * #setText(CharSequence)} and {@link #setIcon(Drawable)}.
801          *
802          * @param layoutResId A layout resource to inflate and use as a custom tab view
803          * @return The current instance for call chaining
804          */
setCustomView(int layoutResId)805         public abstract Tab setCustomView(int layoutResId);
806 
807         /**
808          * Retrieve a previously set custom view for this tab.
809          *
810          * @return The custom view set by {@link #setCustomView(View)}.
811          */
getCustomView()812         public abstract View getCustomView();
813 
814         /**
815          * Give this Tab an arbitrary object to hold for later use.
816          *
817          * @param obj Object to store
818          * @return The current instance for call chaining
819          */
setTag(Object obj)820         public abstract Tab setTag(Object obj);
821 
822         /**
823          * @return This Tab's tag object.
824          */
getTag()825         public abstract Object getTag();
826 
827         /**
828          * Set the {@link TabListener} that will handle switching to and from this tab. All tabs
829          * must have a TabListener set before being added to the ActionBar.
830          *
831          * @param listener Listener to handle tab selection events
832          * @return The current instance for call chaining
833          */
setTabListener(TabListener listener)834         public abstract Tab setTabListener(TabListener listener);
835 
836         /**
837          * Select this tab. Only valid if the tab has been added to the action bar.
838          */
select()839         public abstract void select();
840 
841         /**
842          * Set a description of this tab's content for use in accessibility support. If no content
843          * description is provided the title will be used.
844          *
845          * @param resId A resource ID referring to the description text
846          * @return The current instance for call chaining
847          * @see #setContentDescription(CharSequence)
848          * @see #getContentDescription()
849          */
setContentDescription(int resId)850         public abstract Tab setContentDescription(int resId);
851 
852         /**
853          * Set a description of this tab's content for use in accessibility support. If no content
854          * description is provided the title will be used.
855          *
856          * @param contentDesc Description of this tab's content
857          * @return The current instance for call chaining
858          * @see #setContentDescription(int)
859          * @see #getContentDescription()
860          */
setContentDescription(CharSequence contentDesc)861         public abstract Tab setContentDescription(CharSequence contentDesc);
862 
863         /**
864          * Gets a brief description of this tab's content for use in accessibility support.
865          *
866          * @return Description of this tab's content
867          * @see #setContentDescription(CharSequence)
868          * @see #setContentDescription(int)
869          */
getContentDescription()870         public abstract CharSequence getContentDescription();
871     }
872 
873     /**
874      * Callback interface invoked when an {@link ActionBar.Tab} is focused, unfocused, added, or
875      * removed.
876      *
877      * <p class="note"><strong>Note:</strong> This interface is included in the <a
878      * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
879      * with API level 7 and higher. If you're developing your app for API level 11 and higher
880      * <em>only</em>, you should instead use the framework {@link android.app.ActionBar.TabListener}
881      * interface.</p>
882      *
883      * <div class="special reference">
884      * <h3>Developer Guides</h3>
885      *
886      * <p>For information about how to use action bar tabs,
887      * read the <a href="{@docRoot}guide/topics/ui/actionbar.html#Tabs">Action
888      * Bar</a> API guide.</p>
889      * </div>
890      */
891     public interface TabListener {
892 
893         /**
894          * Called when a tab enters the selected state.
895          *
896          * @param tab The tab that was selected
897          * @param ft  A {@link FragmentTransaction} for queuing fragment operations to execute
898          *            during a tab switch. The previous tab's unselect and this tab's select will be
899          *            executed in a single transaction. This FragmentTransaction does not support
900          *            being added to the back stack.
901          */
onTabSelected(Tab tab, FragmentTransaction ft)902         public void onTabSelected(Tab tab, FragmentTransaction ft);
903 
904         /**
905          * Called when a tab exits the selected state.
906          *
907          * @param tab The tab that was unselected
908          * @param ft  A {@link FragmentTransaction} for queuing fragment operations to execute
909          *            during a tab switch. This tab's unselect and the newly selected tab's select
910          *            will be executed in a single transaction. This FragmentTransaction does not
911          *            support being added to the back stack.
912          */
onTabUnselected(Tab tab, FragmentTransaction ft)913         public void onTabUnselected(Tab tab, FragmentTransaction ft);
914 
915         /**
916          * Called when a tab that is already selected is chosen again by the user. Some applications
917          * may use this action to return to the top level of a category.
918          *
919          * @param tab The tab that was reselected.
920          * @param ft  A {@link FragmentTransaction} for queuing fragment operations to execute once
921          *            this method returns. This FragmentTransaction does not support being added to
922          *            the back stack.
923          */
onTabReselected(Tab tab, FragmentTransaction ft)924         public void onTabReselected(Tab tab, FragmentTransaction ft);
925     }
926 
927     /**
928      * Per-child layout information associated with action bar custom views.
929      *
930      * @attr ref android.R.styleable#ActionBar_LayoutParams_layout_gravity
931      */
932     public static class LayoutParams extends MarginLayoutParams {
933 
934         /**
935          * Gravity for the view associated with these LayoutParams.
936          *
937          * @see android.view.Gravity
938          */
939         public int gravity = -1;
940 
LayoutParams(Context c, AttributeSet attrs)941         public LayoutParams(Context c, AttributeSet attrs) {
942             super(c, attrs);
943 
944             TypedArray a = c.obtainStyledAttributes(attrs,
945                     R.styleable.ActionBarLayout);
946             gravity = a.getInt(R.styleable.ActionBarLayout_android_layout_gravity, -1);
947             a.recycle();
948         }
949 
LayoutParams(int width, int height)950         public LayoutParams(int width, int height) {
951             super(width, height);
952             this.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
953         }
954 
LayoutParams(int width, int height, int gravity)955         public LayoutParams(int width, int height, int gravity) {
956             super(width, height);
957             this.gravity = gravity;
958         }
959 
LayoutParams(int gravity)960         public LayoutParams(int gravity) {
961             this(WRAP_CONTENT, FILL_PARENT, gravity);
962         }
963 
LayoutParams(LayoutParams source)964         public LayoutParams(LayoutParams source) {
965             super(source);
966 
967             this.gravity = source.gravity;
968         }
969 
LayoutParams(ViewGroup.LayoutParams source)970         public LayoutParams(ViewGroup.LayoutParams source) {
971             super(source);
972         }
973     }
974 
975     /**
976      * Interface implemented by entities such as Activities that host action bars.
977      */
978     interface Callback {
979 
getSupportFragmentManager()980         FragmentManager getSupportFragmentManager();
981     }
982 }
983