ActionBarDrawerToggle
class ActionBarDrawerToggle : DrawerLayout.DrawerListener
This class provides a handy way to tie together the functionality of 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.
Summary
Nested types |
|---|
interface ActionBarDrawerToggle.Delegate |
interface ActionBarDrawerToggle.DelegateProviderAllows an implementing Activity to return an |
Public constructors |
|---|
ActionBarDrawerToggle(Construct a new ActionBarDrawerToggle. |
ActionBarDrawerToggle(Construct a new ActionBarDrawerToggle with a Toolbar. |
Public functions |
|
|---|---|
DrawerArrowDrawable |
|
View.OnClickListener! |
Returns the fallback listener for Navigation icon click events. |
Boolean |
|
Boolean |
|
Unit |
onConfigurationChanged(newConfig: Configuration!)This method should always be called by your |
Unit |
onDrawerClosed(drawerView: View!)
|
Unit |
onDrawerOpened(drawerView: View!)
|
Unit |
onDrawerSlide(drawerView: View!, slideOffset: Float)
|
Unit |
onDrawerStateChanged(newState: Int)
|
Boolean |
onOptionsItemSelected(item: MenuItem!)This method should be called by your |
Unit |
setDrawerArrowDrawable(drawable: DrawerArrowDrawable)Sets the DrawerArrowDrawable that should be shown by this ActionBarDrawerToggle. |
Unit |
setDrawerIndicatorEnabled(enable: Boolean)Enable or disable the drawer indicator. |
Unit |
setDrawerSlideAnimationEnabled(enabled: Boolean)Specifies whether the drawer arrow should animate when the drawer position changes. |
Unit |
setHomeAsUpIndicator(indicator: Drawable!)Set the up indicator to display when the drawer indicator is not enabled. |
Unit |
setHomeAsUpIndicator(resId: Int)Set the up indicator to display when the drawer indicator is not enabled. |
Unit |
setToolbarNavigationClickListener(When DrawerToggle is constructed with a Toolbar, it sets the click listener on the Navigation icon. |
Unit |
Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout. |
Public constructors
ActionBarDrawerToggle
ActionBarDrawerToggle(
activity: Activity!,
drawerLayout: DrawerLayout!,
openDrawerContentDescRes: @StringRes Int,
closeDrawerContentDescRes: @StringRes Int
)
Construct a new ActionBarDrawerToggle.
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.
| Parameters | |
|---|---|
activity: Activity! |
The Activity hosting the drawer. Should have an ActionBar. |
drawerLayout: DrawerLayout! |
The DrawerLayout to link to the given Activity's ActionBar |
openDrawerContentDescRes: @StringRes Int |
A String resource to describe the "open drawer" action for accessibility |
closeDrawerContentDescRes: @StringRes Int |
A String resource to describe the "close drawer" action for accessibility |
ActionBarDrawerToggle
ActionBarDrawerToggle(
activity: Activity!,
drawerLayout: DrawerLayout!,
toolbar: Toolbar!,
openDrawerContentDescRes: @StringRes Int,
closeDrawerContentDescRes: @StringRes Int
)
Construct a new ActionBarDrawerToggle with a Toolbar.
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 if you are setting the Toolbar as the ActionBar of your activity.
| Parameters | |
|---|---|
activity: Activity! |
The Activity hosting the drawer. |
drawerLayout: DrawerLayout! |
The DrawerLayout to link to the given Activity's ActionBar |
toolbar: Toolbar! |
The toolbar to use if you have an independent Toolbar. |
openDrawerContentDescRes: @StringRes Int |
A String resource to describe the "open drawer" action for accessibility |
closeDrawerContentDescRes: @StringRes Int |
A String resource to describe the "close drawer" action for accessibility |
Public functions
getDrawerArrowDrawable
fun getDrawerArrowDrawable(): DrawerArrowDrawable
| Returns | |
|---|---|
DrawerArrowDrawable |
DrawerArrowDrawable that is currently shown by the ActionBarDrawerToggle. |
getToolbarNavigationClickListener
fun getToolbarNavigationClickListener(): View.OnClickListener!
Returns the fallback listener for Navigation icon click events.
| Returns | |
|---|---|
View.OnClickListener! |
The click listener which receives Navigation click events from Toolbar when drawer indicator is disabled. |
isDrawerIndicatorEnabled
fun isDrawerIndicatorEnabled(): Boolean
| Returns | |
|---|---|
Boolean |
true if the enhanced drawer indicator is enabled, false otherwise |
| See also | |
|---|---|
setDrawerIndicatorEnabled |
isDrawerSlideAnimationEnabled
fun isDrawerSlideAnimationEnabled(): Boolean
| Returns | |
|---|---|
Boolean |
whether the drawer slide animation is enabled |
onConfigurationChanged
fun onConfigurationChanged(newConfig: Configuration!): Unit
This method should always be called by your Activity's onConfigurationChanged method.
| Parameters | |
|---|---|
newConfig: Configuration! |
The new configuration |
onDrawerClosed
fun onDrawerClosed(drawerView: View!): Unit
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.
| Parameters | |
|---|---|
drawerView: View! |
Drawer view that is now closed |
onDrawerOpened
fun onDrawerOpened(drawerView: View!): Unit
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.
| Parameters | |
|---|---|
drawerView: View! |
Drawer view that is now open |
onDrawerSlide
fun onDrawerSlide(drawerView: View!, slideOffset: Float): Unit
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
fun onDrawerStateChanged(newState: Int): Unit
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.
| Parameters | |
|---|---|
newState: Int |
The new drawer motion state |
onOptionsItemSelected
fun onOptionsItemSelected(item: MenuItem!): Boolean
This method should be called by your Activity's onOptionsItemSelected method. If it returns true, your onOptionsItemSelected method should return true and skip further processing.
| Parameters | |
|---|---|
item: MenuItem! |
the MenuItem instance representing the selected menu item |
| Returns | |
|---|---|
Boolean |
true if the event was handled and further processing should not occur |
setDrawerArrowDrawable
fun setDrawerArrowDrawable(drawable: DrawerArrowDrawable): Unit
Sets the DrawerArrowDrawable that should be shown by this ActionBarDrawerToggle.
| Parameters | |
|---|---|
drawable: DrawerArrowDrawable |
DrawerArrowDrawable that should be shown by this ActionBarDrawerToggle |
setDrawerIndicatorEnabled
fun setDrawerIndicatorEnabled(enable: Boolean): Unit
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.
| Parameters | |
|---|---|
enable: Boolean |
true to enable, false to disable |
setDrawerSlideAnimationEnabled
fun setDrawerSlideAnimationEnabled(enabled: Boolean): Unit
Specifies whether the drawer arrow should animate when the drawer position changes.
| Parameters | |
|---|---|
enabled: Boolean |
if this is |
setHomeAsUpIndicator
fun setHomeAsUpIndicator(indicator: Drawable!): Unit
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.
| Parameters | |
|---|---|
indicator: Drawable! |
A drawable to use for the up indicator, or null to use the theme's default |
| See also | |
|---|---|
setDrawerIndicatorEnabled |
setHomeAsUpIndicator
fun setHomeAsUpIndicator(resId: Int): Unit
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.
| Parameters | |
|---|---|
resId: Int |
Resource ID of a drawable to use for the up indicator, or 0 to use the theme's default |
| See also | |
|---|---|
setDrawerIndicatorEnabled |
setToolbarNavigationClickListener
fun setToolbarNavigationClickListener(
onToolbarNavigationClickListener: View.OnClickListener!
): Unit
When DrawerToggle is constructed with a Toolbar, it sets the click listener on the Navigation icon. If you want to listen for clicks on the Navigation icon when DrawerToggle is disabled (setDrawerIndicatorEnabled, you should call this method with your listener and DrawerToggle will forward click events to that listener when drawer indicator is disabled.
| See also | |
|---|---|
setDrawerIndicatorEnabled |
syncState
fun syncState(): Unit
Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout.
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.)