FileDocCategorySizeDatePackage
ActionBarDrawerToggle.javaAPI DocAndroid 5.1 API21790Thu Mar 12 22:22:56 GMT 2015android.support.v4.app

ActionBarDrawerToggle

public class ActionBarDrawerToggle extends Object implements DrawerLayout.DrawerListener
deprecated
Please use ActionBarDrawerToggle in support-v7-appcompat.

This class provides a handy way to tie together the functionality of {@link DrawerLayout} and the framework ActionBar to implement the recommended design for navigation drawers.

To use ActionBarDrawerToggle, create one in your Activity and call through to the following methods corresponding to your Activity callbacks:

  • {@link Activity#onConfigurationChanged(android.content.res.Configuration) onConfigurationChanged}
  • {@link Activity#onOptionsItemSelected(android.view.MenuItem) onOptionsItemSelected}

Call {@link #syncState()} from your Activity's {@link Activity#onPostCreate(android.os.Bundle) onPostCreate} to synchronize the indicator with the state of the linked DrawerLayout after onRestoreInstanceState has occurred.

ActionBarDrawerToggle can be used directly as a {@link DrawerLayout.DrawerListener}, or if you are already providing your own listener, call through to each of the listener methods from your own.

Fields Summary
private static final ActionBarDrawerToggleImpl
IMPL
private static final float
TOGGLE_DRAWABLE_OFFSET
Fraction of its total width by which to offset the toggle drawable.
private static final int
ID_HOME
private final android.app.Activity
mActivity
private final Delegate
mActivityImpl
private final android.support.v4.widget.DrawerLayout
mDrawerLayout
private boolean
mDrawerIndicatorEnabled
private boolean
mHasCustomUpIndicator
private android.graphics.drawable.Drawable
mHomeAsUpIndicator
private android.graphics.drawable.Drawable
mDrawerImage
private SlideDrawable
mSlider
private final int
mDrawerImageResource
private final int
mOpenDrawerContentDescRes
private final int
mCloseDrawerContentDescRes
private Object
mSetIndicatorInfo
Constructors Summary
public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)
Construct a new ActionBarDrawerToggle.

The given {@link Activity} will be linked to the specified {@link DrawerLayout}. The provided drawer indicator drawable will animate slightly off-screen as the drawer is opened, indicating that in the open state the drawer will move off-screen when pressed and in the closed state the drawer will move on-screen when pressed.

String resources must be provided to describe the open/close drawer actions for accessibility services.

param
activity The Activity hosting the drawer
param
drawerLayout The DrawerLayout to link to the given Activity's ActionBar
param
drawerImageRes A Drawable resource to use as the drawer indicator
param
openDrawerContentDescRes A String resource to describe the "open drawer" action for accessibility
param
closeDrawerContentDescRes A String resource to describe the "close drawer" action for accessibility


                                                                                                                                                                                                      
        
                 
               
        this(activity, drawerLayout, !assumeMaterial(activity), drawerImageRes,
                openDrawerContentDescRes, closeDrawerContentDescRes);
    
public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, boolean animate, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)
Construct a new ActionBarDrawerToggle.

The given {@link Activity} will be linked to the specified {@link DrawerLayout}. The provided drawer indicator drawable will animate slightly off-screen as the drawer is opened, indicating that in the open state the drawer will move off-screen when pressed and in the closed state the drawer will move on-screen when pressed.

String resources must be provided to describe the open/close drawer actions for accessibility services.

param
activity The Activity hosting the drawer
param
drawerLayout The DrawerLayout to link to the given Activity's ActionBar
param
animate True to animate the drawer indicator along with the drawer's position. Material apps should set this to false.
param
drawerImageRes A Drawable resource to use as the drawer indicator
param
openDrawerContentDescRes A String resource to describe the "open drawer" action for accessibility
param
closeDrawerContentDescRes A String resource to describe the "close drawer" action for accessibility

        mActivity = activity;

        // Allow the Activity to provide an impl
        if (activity instanceof DelegateProvider) {
            mActivityImpl = ((DelegateProvider) activity).getDrawerToggleDelegate();
        } else {
            mActivityImpl = null;
        }

        mDrawerLayout = drawerLayout;
        mDrawerImageResource = drawerImageRes;
        mOpenDrawerContentDescRes = openDrawerContentDescRes;
        mCloseDrawerContentDescRes = closeDrawerContentDescRes;

        mHomeAsUpIndicator = getThemeUpIndicator();
        mDrawerImage = ContextCompat.getDrawable(activity, drawerImageRes);
        mSlider = new SlideDrawable(mDrawerImage);
        mSlider.setOffset(animate ? TOGGLE_DRAWABLE_OFFSET : 0);
    
Methods Summary
private static booleanassumeMaterial(android.content.Context context)

        return context.getApplicationInfo().targetSdkVersion >= 21 &&
                (Build.VERSION.SDK_INT >= 21);
    
android.graphics.drawable.DrawablegetThemeUpIndicator()

        if (mActivityImpl != null) {
            return mActivityImpl.getThemeUpIndicator();
        }
        return IMPL.getThemeUpIndicator(mActivity);
    
public booleanisDrawerIndicatorEnabled()

return
true if the enhanced drawer indicator is enabled, false otherwise
see
#setDrawerIndicatorEnabled(boolean)

        return mDrawerIndicatorEnabled;
    
public voidonConfigurationChanged(android.content.res.Configuration newConfig)
This method should always be called by your Activity's {@link Activity#onConfigurationChanged(android.content.res.Configuration) onConfigurationChanged} method.

param
newConfig The new configuration

        // Reload drawables that can change with configuration
        if (!mHasCustomUpIndicator) {
            mHomeAsUpIndicator = getThemeUpIndicator();
        }
        mDrawerImage = ContextCompat.getDrawable(mActivity, mDrawerImageResource);
        syncState();
    
public voidonDrawerClosed(android.view.View drawerView)
{@link DrawerLayout.DrawerListener} callback method. If you do not use your ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call through to this method from your own listener object.

param
drawerView Drawer view that is now closed

        mSlider.setPosition(0);
        if (mDrawerIndicatorEnabled) {
            setActionBarDescription(mOpenDrawerContentDescRes);
        }
    
public voidonDrawerOpened(android.view.View drawerView)
{@link DrawerLayout.DrawerListener} callback method. If you do not use your ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call through to this method from your own listener object.

param
drawerView Drawer view that is now open

        mSlider.setPosition(1);
        if (mDrawerIndicatorEnabled) {
            setActionBarDescription(mCloseDrawerContentDescRes);
        }
    
public voidonDrawerSlide(android.view.View drawerView, float slideOffset)
{@link DrawerLayout.DrawerListener} callback method. If you do not use your ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call through to this method from your own listener object.

param
drawerView The child view that was moved
param
slideOffset The new offset of this drawer within its range, from 0-1

        float glyphOffset = mSlider.getPosition();
        if (slideOffset > 0.5f) {
            glyphOffset = Math.max(glyphOffset, Math.max(0.f, slideOffset - 0.5f) * 2);
        } else {
            glyphOffset = Math.min(glyphOffset, slideOffset * 2);
        }
        mSlider.setPosition(glyphOffset);
    
public voidonDrawerStateChanged(int newState)
{@link DrawerLayout.DrawerListener} callback method. If you do not use your ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call through to this method from your own listener object.

param
newState The new drawer motion state

    
public booleanonOptionsItemSelected(android.view.MenuItem item)
This method should be called by your Activity's {@link Activity#onOptionsItemSelected(android.view.MenuItem) onOptionsItemSelected} method. If it returns true, your onOptionsItemSelected method should return true and skip further processing.

param
item the MenuItem instance representing the selected menu item
return
true if the event was handled and further processing should not occur

        if (item != null && item.getItemId() == ID_HOME && mDrawerIndicatorEnabled) {
            if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
            } else {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
            return true;
        }
        return false;
    
voidsetActionBarDescription(int contentDescRes)

        if (mActivityImpl != null) {
            mActivityImpl.setActionBarDescription(contentDescRes);
            return;
        }
        mSetIndicatorInfo = IMPL
                .setActionBarDescription(mSetIndicatorInfo, mActivity, contentDescRes);
    
voidsetActionBarUpIndicator(android.graphics.drawable.Drawable upDrawable, int contentDescRes)

        if (mActivityImpl != null) {
            mActivityImpl.setActionBarUpIndicator(upDrawable, contentDescRes);
            return;
        }
        mSetIndicatorInfo = IMPL
                .setActionBarUpIndicator(mSetIndicatorInfo, mActivity, upDrawable, contentDescRes);
    
public voidsetDrawerIndicatorEnabled(boolean enable)
Enable or disable the drawer indicator. The indicator defaults to enabled.

When the indicator is disabled, the ActionBar will revert to displaying the home-as-up indicator provided by the Activity's theme in the android.R.attr.homeAsUpIndicator attribute instead of the animated drawer glyph.

param
enable true to enable, false to disable

        if (enable != mDrawerIndicatorEnabled) {
            if (enable) {
                setActionBarUpIndicator(mSlider, mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
                        mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
            } else {
                setActionBarUpIndicator(mHomeAsUpIndicator, 0);
            }
            mDrawerIndicatorEnabled = enable;
        }
    
public voidsetHomeAsUpIndicator(android.graphics.drawable.Drawable indicator)
Set the up indicator to display when the drawer indicator is not enabled.

If you pass null to this method, the default drawable from the theme will be used.

param
indicator A drawable to use for the up indicator, or null to use the theme's default
see
#setDrawerIndicatorEnabled(boolean)

        if (indicator == null) {
            mHomeAsUpIndicator = getThemeUpIndicator();
            mHasCustomUpIndicator = false;
        } else {
            mHomeAsUpIndicator = indicator;
            mHasCustomUpIndicator = true;
        }

        if (!mDrawerIndicatorEnabled) {
            setActionBarUpIndicator(mHomeAsUpIndicator, 0);
        }
    
public voidsetHomeAsUpIndicator(int resId)
Set the up indicator to display when the drawer indicator is not enabled.

If you pass 0 to this method, the default drawable from the theme will be used.

param
resId Resource ID of a drawable to use for the up indicator, or 0 to use the theme's default
see
#setDrawerIndicatorEnabled(boolean)

        Drawable indicator = null;
        if (resId != 0) {
            indicator = ContextCompat.getDrawable(mActivity, resId);
        }

        setHomeAsUpIndicator(indicator);
    
public voidsyncState()
Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout.

This should be called from your Activity's {@link Activity#onPostCreate(android.os.Bundle) onPostCreate} method to synchronize after the DrawerLayout's instance state has been restored, and any other time when the state may have diverged in such a way that the ActionBarDrawerToggle was not notified. (For example, if you stop forwarding appropriate drawer events for a period of time.)

        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mSlider.setPosition(1);
        } else {
            mSlider.setPosition(0);
        }

        if (mDrawerIndicatorEnabled) {
            setActionBarUpIndicator(mSlider, mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
                    mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
        }