public class ActionBarDrawerToggle extends Object implements DrawerLayout.DrawerListener
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:
Call syncState()
from your Activity
's
onPostCreate
to synchronize the
indicator with the state of the linked DrawerLayout after onRestoreInstanceState
has occurred.
ActionBarDrawerToggle
can be used directly as a
DrawerLayout.DrawerListener
, or if you are already providing
your own listener, call through to each of the listener methods from your own.
You can customize the the animated toggle by defining the
drawerArrowStyle
in your
ActionBar theme.
Modifier and Type | Class and Description |
---|---|
static interface |
ActionBarDrawerToggle.Delegate |
static interface |
ActionBarDrawerToggle.DelegateProvider
Allows an implementing Activity to return an
ActionBarDrawerToggle.Delegate to use
with ActionBarDrawerToggle. |
Constructor and Description |
---|
ActionBarDrawerToggle(Activity activity,
DrawerLayout drawerLayout,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)
Construct a new ActionBarDrawerToggle.
|
ActionBarDrawerToggle(Activity activity,
DrawerLayout drawerLayout,
Toolbar toolbar,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)
Construct a new ActionBarDrawerToggle with a Toolbar.
|
Modifier and Type | Method and Description |
---|---|
DrawerArrowDrawable |
getDrawerArrowDrawable() |
View.OnClickListener |
getToolbarNavigationClickListener()
Returns the fallback listener for Navigation icon click events.
|
boolean |
isDrawerIndicatorEnabled() |
void |
onConfigurationChanged(Configuration newConfig)
This method should always be called by your
Activity 's
onConfigurationChanged
method. |
void |
onDrawerClosed(View drawerView)
DrawerLayout.DrawerListener callback method. |
void |
onDrawerOpened(View drawerView)
DrawerLayout.DrawerListener callback method. |
void |
onDrawerSlide(View drawerView,
float slideOffset)
DrawerLayout.DrawerListener callback method. |
void |
onDrawerStateChanged(int newState)
DrawerLayout.DrawerListener callback method. |
boolean |
onOptionsItemSelected(MenuItem item)
This method should be called by your
Activity 's
onOptionsItemSelected method. |
void |
setDrawerArrowDrawable(DrawerArrowDrawable drawable)
Sets the DrawerArrowDrawable that should be shown by this ActionBarDrawerToggle.
|
void |
setDrawerIndicatorEnabled(boolean enable)
Enable or disable the drawer indicator.
|
void |
setHomeAsUpIndicator(Drawable indicator)
Set the up indicator to display when the drawer indicator is not
enabled.
|
void |
setHomeAsUpIndicator(int resId)
Set the up indicator to display when the drawer indicator is not
enabled.
|
void |
setToolbarNavigationClickListener(View.OnClickListener onToolbarNavigationClickListener)
When DrawerToggle is constructed with a Toolbar, it sets the click listener on
the Navigation icon.
|
void |
syncState()
Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout.
|
public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes)
The given Activity
will be linked to the specified DrawerLayout
and
its Actionbar's Up button will be set to a custom drawable.
This drawable shows a Hamburger icon when drawer is closed and an arrow when drawer is open. It animates between these two states as the drawer opens.
String resources must be provided to describe the open/close drawer actions for accessibility services.
activity
- The Activity hosting the drawer. Should have an ActionBar.drawerLayout
- The DrawerLayout to link to the given Activity's ActionBaropenDrawerContentDescRes
- A String resource to describe the "open drawer" action
for accessibilitycloseDrawerContentDescRes
- A String resource to describe the "close drawer" action
for accessibilitypublic ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes)
The given Activity
will be linked to the specified DrawerLayout
and
the Toolbar's navigation icon will be set to a custom drawable. Using this constructor
will set Toolbar's navigation click listener to toggle the drawer when it is clicked.
This drawable shows a Hamburger icon when drawer is closed and an arrow when drawer is open. It animates between these two states as the drawer opens.
String resources must be provided to describe the open/close drawer actions for accessibility services.
Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int)
if you are
setting the Toolbar as the ActionBar of your activity.
activity
- The Activity hosting the drawer.toolbar
- The toolbar to use if you have an independent Toolbar.drawerLayout
- The DrawerLayout to link to the given Activity's ActionBaropenDrawerContentDescRes
- A String resource to describe the "open drawer" action
for accessibilitycloseDrawerContentDescRes
- A String resource to describe the "close drawer" action
for accessibilitypublic void syncState()
This should be called from your Activity
's
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.)
public void onConfigurationChanged(Configuration newConfig)
Activity
's
onConfigurationChanged
method.newConfig
- The new configurationpublic boolean onOptionsItemSelected(MenuItem item)
Activity
's
onOptionsItemSelected
method.
If it returns true, your onOptionsItemSelected
method should return true and
skip further processing.item
- the MenuItem instance representing the selected menu itempublic void setHomeAsUpIndicator(Drawable indicator)
If you pass null
to this method, the default drawable from
the theme will be used.
indicator
- A drawable to use for the up indicator, or null to use
the theme's defaultsetDrawerIndicatorEnabled(boolean)
public void setHomeAsUpIndicator(int resId)
If you pass 0 to this method, the default drawable from the theme will be used.
resId
- Resource ID of a drawable to use for the up indicator, or 0
to use the theme's defaultsetDrawerIndicatorEnabled(boolean)
public boolean isDrawerIndicatorEnabled()
setDrawerIndicatorEnabled(boolean)
public void setDrawerIndicatorEnabled(boolean enable)
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.
enable
- true to enable, false to disable@NonNull public DrawerArrowDrawable getDrawerArrowDrawable()
public void setDrawerArrowDrawable(@NonNull DrawerArrowDrawable drawable)
drawable
- DrawerArrowDrawable that should be shown by this ActionBarDrawerToggle.public void onDrawerSlide(View drawerView, float slideOffset)
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.onDrawerSlide
in interface DrawerLayout.DrawerListener
drawerView
- The child view that was movedslideOffset
- The new offset of this drawer within its range, from 0-1public void onDrawerOpened(View drawerView)
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.onDrawerOpened
in interface DrawerLayout.DrawerListener
drawerView
- Drawer view that is now openpublic void onDrawerClosed(View drawerView)
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.onDrawerClosed
in interface DrawerLayout.DrawerListener
drawerView
- Drawer view that is now closedpublic void onDrawerStateChanged(int newState)
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.onDrawerStateChanged
in interface DrawerLayout.DrawerListener
newState
- The new drawer motion statepublic View.OnClickListener getToolbarNavigationClickListener()
setToolbarNavigationClickListener(android.view.View.OnClickListener)
,
setDrawerIndicatorEnabled(boolean)
,
isDrawerIndicatorEnabled()
public void setToolbarNavigationClickListener(View.OnClickListener onToolbarNavigationClickListener)
setDrawerIndicatorEnabled(boolean)
, you should call this
method with your listener and DrawerToggle will forward click events to that listener
when drawer indicator is disabled.setDrawerIndicatorEnabled(boolean)