to top
Android APIs
public class

ActionBarImplJB

extends ActionBar
java.lang.Object
   ↳ android.support.v7.app.ActionBar
     ↳ android.support.v7.app.ActionBarImplJB

Summary

[Expand]
Inherited Constants
From class android.support.v7.app.ActionBar
Public Constructors
ActionBarImplJB(Activity activity, ActionBar.Callback callback)
Public Methods
void addOnMenuVisibilityListener(ActionBar.OnMenuVisibilityListener listener)
Add a listener that will respond to menu visibility change events.
void addTab(ActionBar.Tab tab, int position)
Add a tab for use in tabbed navigation mode.
void addTab(ActionBar.Tab tab)
Add a tab for use in tabbed navigation mode.
void addTab(ActionBar.Tab tab, boolean setSelected)
Add a tab for use in tabbed navigation mode.
void addTab(ActionBar.Tab tab, int position, boolean setSelected)
Add a tab for use in tabbed navigation mode.
View getCustomView()
int getDisplayOptions()
int getHeight()
Retrieve the current height of the ActionBar.
int getNavigationItemCount()
Get the number of navigation items present in the current navigation mode.
int getNavigationMode()
Returns the current navigation mode.
int getSelectedNavigationIndex()
Get the position of the selected navigation item in list or tabbed navigation modes.
ActionBar.Tab getSelectedTab()
Returns the currently selected tab if in tabbed navigation mode and there is at least one tab present.
CharSequence getSubtitle()
Returns the current ActionBar subtitle in standard mode.
ActionBar.Tab getTabAt(int index)
Returns the tab at the specified index.
int getTabCount()
Returns the number of tabs currently registered with the action bar.
Context getThemedContext()
Returns a Context with an appropriate theme for creating views that will appear in the action bar.
CharSequence getTitle()
Returns the current ActionBar title in standard mode.
void hide()
Hide the ActionBar if it is currently showing.
boolean isShowing()
ActionBar.Tab newTab()
Create and return a new ActionBar.Tab.
void removeAllTabs()
Remove all tabs from the action bar and deselect the current tab.
void removeOnMenuVisibilityListener(ActionBar.OnMenuVisibilityListener listener)
Remove a menu visibility listener.
void removeTab(ActionBar.Tab tab)
Remove a tab from the action bar.
void removeTabAt(int position)
Remove a tab from the action bar.
void selectTab(ActionBar.Tab tab)
Select the specified tab.
void setBackgroundDrawable(Drawable d)
Set the ActionBar's background.
void setCustomView(int resId)
Set the action bar into custom navigation mode, supplying a view for custom navigation.
void setCustomView(View view, ActionBar.LayoutParams layoutParams)
Set the action bar into custom navigation mode, supplying a view for custom navigation.
void setCustomView(View view)
Set the action bar into custom navigation mode, supplying a view for custom navigation.
void setDisplayHomeAsUpEnabled(boolean showHomeAsUp)
Set whether home should be displayed as an "up" affordance.
void setDisplayOptions(int options, int mask)
Set selected display options.
void setDisplayOptions(int options)
Set display options.
void setDisplayShowCustomEnabled(boolean showCustom)
Set whether a custom view should be displayed, if set.
void setDisplayShowHomeEnabled(boolean showHome)
Set whether to include the application home affordance in the action bar.
void setDisplayShowTitleEnabled(boolean showTitle)
Set whether an activity title/subtitle should be displayed.
void setDisplayUseLogoEnabled(boolean useLogo)
Set whether to display the activity logo rather than the activity icon.
void setHomeButtonEnabled(boolean enabled)
Enable or disable the "home" button in the corner of the action bar.
void setIcon(Drawable icon)
Set the icon to display in the 'home' section of the action bar.
void setIcon(int resId)
Set the icon to display in the 'home' section of the action bar.
void setListNavigationCallbacks(SpinnerAdapter adapter, ActionBar.OnNavigationListener callback)
Set the adapter and navigation callback for list navigation mode.
void setLogo(int resId)
Set the logo to display in the 'home' section of the action bar.
void setLogo(Drawable logo)
Set the logo to display in the 'home' section of the action bar.
void setNavigationMode(int mode)
Set the current navigation mode.
void setSelectedNavigationItem(int position)
Set the selected navigation item in list or tabbed navigation modes.
void setSubtitle(int resId)
Set the action bar's subtitle.
void setSubtitle(CharSequence subtitle)
Set the action bar's subtitle.
void setTitle(int resId)
Set the action bar's title.
void setTitle(CharSequence title)
Set the action bar's title.
void show()
Show the ActionBar if it is not currently showing.
[Expand]
Inherited Methods
From class android.support.v7.app.ActionBar
From class java.lang.Object

Public Constructors

public ActionBarImplJB (Activity activity, ActionBar.Callback callback)

Public Methods

public void addOnMenuVisibilityListener (ActionBar.OnMenuVisibilityListener listener)

Add a listener that will respond to menu visibility change events.

Parameters
listener The new listener to add

public void addTab (ActionBar.Tab tab, int position)

Add a tab for use in tabbed navigation mode. The tab will be inserted at position. If this is the first tab to be added it will become the selected tab.

Parameters
tab The tab to add
position The new position of the tab

public void addTab (ActionBar.Tab tab)

Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list. If this is the first tab to be added it will become the selected tab.

Parameters
tab Tab to add

public void addTab (ActionBar.Tab tab, boolean setSelected)

Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.

Parameters
tab Tab to add
setSelected True if the added tab should become the selected tab.

public void addTab (ActionBar.Tab tab, int position, boolean setSelected)

Add a tab for use in tabbed navigation mode. The tab will be insterted at position.

Parameters
tab The tab to add
position The new position of the tab
setSelected True if the added tab should become the selected tab.

public View getCustomView ()

Returns
  • The current custom view.

public int getDisplayOptions ()

Returns
  • The current set of display options.

public int getHeight ()

Retrieve the current height of the ActionBar.

Returns
  • The ActionBar's height

public int getNavigationItemCount ()

Get the number of navigation items present in the current navigation mode.

Returns
  • Number of navigation items.

public int getNavigationMode ()

Returns the current navigation mode. The result will be one of:

Returns
  • The current navigation mode.

public int getSelectedNavigationIndex ()

Get the position of the selected navigation item in list or tabbed navigation modes.

Returns
  • Position of the selected item.

public ActionBar.Tab getSelectedTab ()

Returns the currently selected tab if in tabbed navigation mode and there is at least one tab present.

Returns
  • The currently selected tab or null

public CharSequence getSubtitle ()

Returns the current ActionBar subtitle in standard mode. Returns null if getNavigationMode() would not return NAVIGATION_MODE_STANDARD.

Returns
  • The current ActionBar subtitle or null.

public ActionBar.Tab getTabAt (int index)

Returns the tab at the specified index.

Parameters
index Index value in the range 0-get

public int getTabCount ()

Returns the number of tabs currently registered with the action bar.

Returns
  • Tab count

public Context getThemedContext ()

Returns a Context with an appropriate theme for creating views that will appear in the action bar. If you are inflating or instantiating custom views that will appear in an action bar, you should use the Context returned by this method. (This includes adapters used for list navigation mode.) This will ensure that views contrast properly against the action bar.

Returns
  • A themed Context for creating views

public CharSequence getTitle ()

Returns the current ActionBar title in standard mode. Returns null if getNavigationMode() would not return NAVIGATION_MODE_STANDARD.

Returns
  • The current ActionBar title or null.

public void hide ()

Hide the ActionBar if it is currently showing. If the window hosting the ActionBar does not have the feature FEATURE_ACTION_BAR_OVERLAY it will resize application content to fit the new space available.

Instead of calling this function directly, you can also cause an ActionBar using the overlay feature to hide through View.SYSTEM_UI_FLAG_FULLSCREEN. Hiding the ActionBar through this system UI flag allows you to more seamlessly hide it in conjunction with other screen decorations.

public boolean isShowing ()

Returns
  • true if the ActionBar is showing, false otherwise.

public ActionBar.Tab newTab ()

Create and return a new ActionBar.Tab. This tab will not be included in the action bar until it is added.

Very often tabs will be used to switch between Fragment objects. Here is a typical implementation of such tabs:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;

/**
 * This demonstrates the use of action bar tabs and how they interact
 * with other action bar features.
 */
public class FragmentTabs extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        bar.addTab(bar.newTab()
                .setText("Simple")
                .setTabListener(new TabListener<FragmentStack.CountingFragment>(
                        this, "simple", FragmentStack.CountingFragment.class)));
        bar.addTab(bar.newTab()
                .setText("Contacts")
                .setTabListener(new TabListener<LoaderCursor.CursorLoaderListFragment>(
                        this, "contacts", LoaderCursor.CursorLoaderListFragment.class)));
        bar.addTab(bar.newTab()
                .setText("Apps")
                .setTabListener(new TabListener<LoaderCustom.AppListFragment>(
                        this, "apps", LoaderCustom.AppListFragment.class)));
        bar.addTab(bar.newTab()
                .setText("Throttle")
                .setTabListener(new TabListener<LoaderThrottle.ThrottledLoaderListFragment>(
                        this, "throttle", LoaderThrottle.ThrottledLoaderListFragment.class)));

        if (savedInstanceState != null) {
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }

    public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;

        public TabListener(Activity activity, String tag, Class<T> clz) {
            this(activity, tag, clz, null);
        }

        public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;

            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
        }
    }
}

Returns
  • A new Tab

public void removeAllTabs ()

Remove all tabs from the action bar and deselect the current tab.

public void removeOnMenuVisibilityListener (ActionBar.OnMenuVisibilityListener listener)

Remove a menu visibility listener. This listener will no longer receive menu visibility change events.

Parameters
listener A listener to remove that was previously added

public void removeTab (ActionBar.Tab tab)

Remove a tab from the action bar. If the removed tab was selected it will be deselected and another tab will be selected if present.

Parameters
tab The tab to remove

public void removeTabAt (int position)

Remove a tab from the action bar. If the removed tab was selected it will be deselected and another tab will be selected if present.

Parameters
position Position of the tab to remove

public void selectTab (ActionBar.Tab tab)

Select the specified tab. If it is not a child of this action bar it will be added.

Note: If you want to select by index, use setSelectedNavigationItem(int).

Parameters
tab Tab to select

public void setBackgroundDrawable (Drawable d)

Set the ActionBar's background. This will be used for the primary action bar.

Parameters
d Background drawable

public void setCustomView (int resId)

Set the action bar into custom navigation mode, supplying a view for custom navigation.

Custom navigation views appear between the application icon and any action buttons and may use any space available there. Common use cases for custom navigation views might include an auto-suggesting address bar for a browser or other navigation mechanisms that do not translate well to provided navigation modes.

The display option DISPLAY_SHOW_CUSTOM must be set for the custom view to be displayed.

Parameters
resId Resource ID of a layout to inflate into the ActionBar.

public void setCustomView (View view, ActionBar.LayoutParams layoutParams)

Set the action bar into custom navigation mode, supplying a view for custom navigation.

Custom navigation views appear between the application icon and any action buttons and may use any space available there. Common use cases for custom navigation views might include an auto-suggesting address bar for a browser or other navigation mechanisms that do not translate well to provided navigation modes.

The display option DISPLAY_SHOW_CUSTOM must be set for the custom view to be displayed.

Parameters
view Custom navigation view to place in the ActionBar.
layoutParams How this custom view should layout in the bar.

public void setCustomView (View view)

Set the action bar into custom navigation mode, supplying a view for custom navigation. Custom navigation views appear between the application icon and any action buttons and may use any space available there. Common use cases for custom navigation views might include an auto-suggesting address bar for a browser or other navigation mechanisms that do not translate well to provided navigation modes.

Parameters
view Custom navigation view to place in the ActionBar.

public void setDisplayHomeAsUpEnabled (boolean showHomeAsUp)

Set whether home should be displayed as an "up" affordance. Set this to true if selecting "home" returns up by a single level in your UI rather than back to the top level or front page.

To set several display options at once, see the setDisplayOptions methods.

Parameters
showHomeAsUp true to show the user that selecting home will return one level up rather than to the top level of the app.

public void setDisplayOptions (int options, int mask)

Set selected display options. Only the options specified by mask will be changed. To change all display option bits at once, see setDisplayOptions(int).

Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the DISPLAY_SHOW_HOME option. setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO) will enable DISPLAY_SHOW_HOME and disable DISPLAY_USE_LOGO.

Parameters
options A combination of the bits defined by the DISPLAY_ constants defined in ActionBar.
mask A bit mask declaring which display options should be changed.

public void setDisplayOptions (int options)

Set display options. This changes all display option bits at once. To change a limited subset of display options, see setDisplayOptions(int, int).

Parameters
options A combination of the bits defined by the DISPLAY_ constants defined in ActionBar.

public void setDisplayShowCustomEnabled (boolean showCustom)

Set whether a custom view should be displayed, if set.

To set several display options at once, see the setDisplayOptions methods.

Parameters
showCustom true if the currently set custom view should be displayed, false otherwise.

public void setDisplayShowHomeEnabled (boolean showHome)

Set whether to include the application home affordance in the action bar. Home is presented as either an activity icon or logo.

To set several display options at once, see the setDisplayOptions methods.

Parameters
showHome true to show home, false otherwise.

public void setDisplayShowTitleEnabled (boolean showTitle)

Set whether an activity title/subtitle should be displayed.

To set several display options at once, see the setDisplayOptions methods.

Parameters
showTitle true to display a title/subtitle if present.

public void setDisplayUseLogoEnabled (boolean useLogo)

Set whether to display the activity logo rather than the activity icon. A logo is often a wider, more detailed image.

To set several display options at once, see the setDisplayOptions methods.

Parameters
useLogo true to use the activity logo, false to use the activity icon.

public void setHomeButtonEnabled (boolean enabled)

Enable or disable the "home" button in the corner of the action bar. (Note that this is the application home/up affordance on the action bar, not the systemwide home button.)

This defaults to true for packages targeting < API 14. For packages targeting API 14 or greater, the application should call this method to enable interaction with the home/up affordance.

Setting the DISPLAY_HOME_AS_UP display option will automatically enable the home button.

Parameters
enabled true to enable the home button, false to disable the home button.

public void setIcon (Drawable icon)

Set the icon to display in the 'home' section of the action bar. The action bar will use an icon specified by its style or the activity icon by default. Whether the home section shows an icon or logo is controlled by the display option DISPLAY_USE_LOGO.

Parameters
icon Drawable to show as an icon.

public void setIcon (int resId)

Set the icon to display in the 'home' section of the action bar. The action bar will use an icon specified by its style or the activity icon by default. Whether the home section shows an icon or logo is controlled by the display option DISPLAY_USE_LOGO.

Parameters
resId Resource ID of a drawable to show as an icon.

public void setListNavigationCallbacks (SpinnerAdapter adapter, ActionBar.OnNavigationListener callback)

Set the adapter and navigation callback for list navigation mode. The supplied adapter will provide views for the expanded list as well as the currently selected item. (These may be displayed differently.) The supplied OnNavigationListener will alert the application when the user changes the current list selection.

Parameters
adapter An adapter that will provide views both to display the current navigation selection and populate views within the dropdown navigation menu.
callback An OnNavigationListener that will receive events when the user selects a navigation item.

public void setLogo (int resId)

Set the logo to display in the 'home' section of the action bar. The action bar will use a logo specified by its style or the activity logo by default. Whether the home section shows an icon or logo is controlled by the display option DISPLAY_USE_LOGO.

Parameters
resId Resource ID of a drawable to show as a logo.

public void setLogo (Drawable logo)

Set the logo to display in the 'home' section of the action bar. The action bar will use a logo specified by its style or the activity logo by default. Whether the home section shows an icon or logo is controlled by the display option DISPLAY_USE_LOGO.

Parameters
logo Drawable to show as a logo.

public void setNavigationMode (int mode)

Set the current navigation mode.

Parameters
mode The new mode to set.

public void setSelectedNavigationItem (int position)

Set the selected navigation item in list or tabbed navigation modes.

Parameters
position Position of the item to select.

public void setSubtitle (int resId)

Set the action bar's subtitle. This will only be displayed if DISPLAY_SHOW_TITLE is set.

Parameters
resId Resource ID of subtitle string to set

public void setSubtitle (CharSequence subtitle)

Set the action bar's subtitle. This will only be displayed if DISPLAY_SHOW_TITLE is set. Set to null to disable the subtitle entirely.

Parameters
subtitle Subtitle to set

public void setTitle (int resId)

Set the action bar's title. This will only be displayed if DISPLAY_SHOW_TITLE is set.

Parameters
resId Resource ID of title string to set

public void setTitle (CharSequence title)

Set the action bar's title. This will only be displayed if DISPLAY_SHOW_TITLE is set.

Parameters
title Title to set

public void show ()

Show the ActionBar if it is not currently showing. If the window hosting the ActionBar does not have the feature FEATURE_ACTION_BAR_OVERLAY it will resize application content to fit the new space available.

If you are hiding the ActionBar through View.SYSTEM_UI_FLAG_FULLSCREEN, you should not call this function directly.