Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

DrawerLayout

open class DrawerLayout : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.drawerlayout.widget.DrawerLayout

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right (or start/end on platform versions that support layout direction.) Note that you can only have one drawer view for each vertical edge of the window. If your layout configures more than one drawer view per vertical edge of the window, an exception will be thrown at runtime.

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity>. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

DrawerListener can be used to monitor the state and motion of drawer views. Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the STATE_IDLE state. SimpleDrawerListener offers default/no-op implementations of each callback method.

As per the Android Design guide, any drawers positioned to the left/start should always contain content for navigating around the application, whereas any drawers positioned to the right/end should always contain actions to take on the current content. This preserves the same navigation left, actions right structure present in the Action Bar and elsewhere.

For more information about how to use DrawerLayout, read Creating a Navigation Drawer.

Summary

Nested classes

abstract

Listener for monitoring events about drawers.

open

open

State persisted across instances

abstract

Stub/no-op implementations of all methods of DrawerListener.

Constants

static Int

The drawer is locked closed.

static Int

The drawer is locked open.

static Int

The drawer's lock state is reset to default.

static Int

The drawer is unlocked.

static Int

Indicates that a drawer is currently being dragged by the user.

static Int

Indicates that any drawers are in an idle, settled state.

static Int

Indicates that a drawer is in the process of settling to a final position.

Public constructors

<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

open Unit

Adds the specified listener to the list of listeners that will be notified of drawer events.

open Unit
addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int)

open Unit
addView(child: View!, index: Int, params: LayoutParams!)

open Unit
closeDrawer(@NonNull drawerView: View)

Close the specified drawer view by animating it into view.

open Unit
closeDrawer(@NonNull drawerView: View, animate: Boolean)

Close the specified drawer view.

open Unit
closeDrawer(gravity: Int)

Close the specified drawer by animating it out of view.

open Unit
closeDrawer(gravity: Int, animate: Boolean)

Close the specified drawer.

open Unit

Close all currently open drawer views by animating them out of view.

open Unit

open Boolean

open LayoutParams!

open Float

The base elevation of the drawer(s) relative to the parent, in pixels.

open Int
getDrawerLockMode(edgeGravity: Int)

Check the lock mode of the drawer with the given gravity.

open Int
getDrawerLockMode(@NonNull drawerView: View)

Check the lock mode of the given drawer view.

open CharSequence?
getDrawerTitle(edgeGravity: Int)

Returns the title of the drawer with the given gravity.

open Drawable?

Gets the drawable used to draw in the insets area for the status bar.

open Boolean
isDrawerOpen(@NonNull drawer: View)

Check if the given drawer view is currently in an open state.

open Boolean
isDrawerOpen(drawerGravity: Int)

Check if the given drawer view is currently in an open state.

open Boolean
isDrawerVisible(@NonNull drawer: View)

Check if a given drawer view is currently visible on-screen.

open Boolean
isDrawerVisible(drawerGravity: Int)

Check if a given drawer view is currently visible on-screen.

open Unit

open Boolean

open Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)

open Boolean
onKeyUp(keyCode: Int, event: KeyEvent!)

open Unit
onRtlPropertiesChanged(layoutDirection: Int)

open Boolean

open Unit
openDrawer(@NonNull drawerView: View)

Open the specified drawer view by animating it into view.

open Unit
openDrawer(@NonNull drawerView: View, animate: Boolean)

Open the specified drawer view.

open Unit
openDrawer(gravity: Int)

Open the specified drawer by animating it out of view.

open Unit
openDrawer(gravity: Int, animate: Boolean)

Open the specified drawer.

open Unit

Removes the specified listener from the list of listeners that will be notified of drawer events.

open Unit

open Unit

open Unit

Sets the base elevation of the drawer(s) relative to the parent, in pixels.

open Unit

Set a listener to be notified of drawer events.

open Unit

Enable or disable interaction with all drawers.

open Unit
setDrawerLockMode(lockMode: Int, edgeGravity: Int)

Enable or disable interaction with the given drawer.

open Unit
setDrawerLockMode(lockMode: Int, @NonNull drawerView: View)

Enable or disable interaction with the given drawer.

open Unit
setDrawerShadow(shadowDrawable: Drawable!, gravity: Int)

Set a simple drawable used for the left or right shadow.

open Unit
setDrawerShadow(@DrawableRes resId: Int, gravity: Int)

Set a simple drawable used for the left or right shadow.

open Unit
setDrawerTitle(edgeGravity: Int, @Nullable title: CharSequence?)

Sets the title of the drawer with the given gravity.

open Unit

Set a color to use for the scrim that obscures primary content while a drawer is open.

open Unit

Set a drawable to draw in the insets area for the status bar.

open Unit

Set a drawable to draw in the insets area for the status bar.

open Unit

Set a drawable to draw in the insets area for the status bar.

Protected methods

open Boolean

open Boolean
drawChild(canvas: Canvas!, child: View!, drawingTime: Long)

open LayoutParams!

open LayoutParams!

open Unit

open Unit

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Unit

open Parcelable!

Constants

LOCK_MODE_LOCKED_CLOSED

static val LOCK_MODE_LOCKED_CLOSED: Int

The drawer is locked closed. The user may not open it, though the app may open it programmatically.

Value: 1

LOCK_MODE_LOCKED_OPEN

static val LOCK_MODE_LOCKED_OPEN: Int

The drawer is locked open. The user may not close it, though the app may close it programmatically.

Value: 2

LOCK_MODE_UNDEFINED

static val LOCK_MODE_UNDEFINED: Int

The drawer's lock state is reset to default.

Value: 3

LOCK_MODE_UNLOCKED

static val LOCK_MODE_UNLOCKED: Int

The drawer is unlocked.

Value: 0

STATE_DRAGGING

static val STATE_DRAGGING: Int

Indicates that a drawer is currently being dragged by the user.

Value: ViewDragHelper.STATE_DRAGGING

STATE_IDLE

static val STATE_IDLE: Int

Indicates that any drawers are in an idle, settled state. No animation is in progress.

Value: ViewDragHelper.STATE_IDLE

STATE_SETTLING

static val STATE_SETTLING: Int

Indicates that a drawer is in the process of settling to a final position.

Value: ViewDragHelper.STATE_SETTLING

Public constructors

<init>

DrawerLayout(@NonNull context: Context)

<init>

DrawerLayout(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

DrawerLayout(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

addDrawerListener

open fun addDrawerListener(@NonNull listener: DrawerLayout.DrawerListener): Unit

Adds the specified listener to the list of listeners that will be notified of drawer events.

Parameters
listener DrawerLayout.DrawerListener: Listener to notify when drawer events occur.

addFocusables

open fun addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int): Unit

addView

open fun addView(child: View!, index: Int, params: LayoutParams!): Unit

closeDrawer

open fun closeDrawer(@NonNull drawerView: View): Unit

Close the specified drawer view by animating it into view.

Parameters
drawerView View: Drawer view to close

closeDrawer

open fun closeDrawer(@NonNull drawerView: View, animate: Boolean): Unit

Close the specified drawer view.

Parameters
drawerView View: Drawer view to close
animate View: Whether closing of the drawer should be animated.

closeDrawer

open fun closeDrawer(gravity: Int): Unit

Close the specified drawer by animating it out of view.

Parameters
gravity Int: Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.

closeDrawer

open fun closeDrawer(gravity: Int, animate: Boolean): Unit

Close the specified drawer.

Parameters
gravity Int: Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.
animate Int: Whether closing of the drawer should be animated.

closeDrawers

open fun closeDrawers(): Unit

Close all currently open drawer views by animating them out of view.

computeScroll

open fun computeScroll(): Unit

dispatchGenericMotionEvent

open fun dispatchGenericMotionEvent(event: MotionEvent!): Boolean

generateLayoutParams

open fun generateLayoutParams(attrs: AttributeSet!): LayoutParams!

getDrawerElevation

open fun getDrawerElevation(): Float

The base elevation of the drawer(s) relative to the parent, in pixels. Note that the elevation change is only supported in API 21 and above. For unsupported API levels, 0 will be returned as the elevation.

Return
Float: The base depth position of the view, in pixels.

getDrawerLockMode

open fun getDrawerLockMode(edgeGravity: Int): Int

Check the lock mode of the drawer with the given gravity.

Parameters
edgeGravity Int: Gravity of the drawer to check
Return
Int: one of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.

getDrawerLockMode

open fun getDrawerLockMode(@NonNull drawerView: View): Int

Check the lock mode of the given drawer view.

Parameters
drawerView View: Drawer view to check lock mode
Return
Int: one of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.

getDrawerTitle

@Nullable open fun getDrawerTitle(edgeGravity: Int): CharSequence?

Returns the title of the drawer with the given gravity.

Parameters
edgeGravity Int: Gravity.LEFT, RIGHT, START or END. Expresses which drawer to return the title for.
Return
CharSequence?: The title of the drawer, or null if none set.

getStatusBarBackgroundDrawable

@Nullable open fun getStatusBarBackgroundDrawable(): Drawable?

Gets the drawable used to draw in the insets area for the status bar.

Return
Drawable?: The status bar background drawable, or null if none set

isDrawerOpen

open fun isDrawerOpen(@NonNull drawer: View): Boolean

Check if the given drawer view is currently in an open state. To be considered "open" the drawer must have settled into its fully visible state. To check for partial visibility use isDrawerVisible(android.view.View).

Parameters
drawer View: Drawer view to check
Return
Boolean: true if the given drawer view is in an open state

isDrawerOpen

open fun isDrawerOpen(drawerGravity: Int): Boolean

Check if the given drawer view is currently in an open state. To be considered "open" the drawer must have settled into its fully visible state. If there is no drawer with the given gravity this method will return false.

Parameters
drawerGravity Int: Gravity of the drawer to check
Return
Boolean: true if the given drawer view is in an open state

isDrawerVisible

open fun isDrawerVisible(@NonNull drawer: View): Boolean

Check if a given drawer view is currently visible on-screen. The drawer may be only peeking onto the screen, fully extended, or anywhere inbetween.

Parameters
drawer View: Drawer view to check
Return
Boolean: true if the given drawer is visible on-screen

isDrawerVisible

open fun isDrawerVisible(drawerGravity: Int): Boolean

Check if a given drawer view is currently visible on-screen. The drawer may be only peeking onto the screen, fully extended, or anywhere in between. If there is no drawer with the given gravity this method will return false.

Parameters
drawerGravity Int: Gravity of the drawer to check
Return
Boolean: true if the given drawer is visible on-screen

onDraw

open fun onDraw(c: Canvas!): Unit

onInterceptTouchEvent

open fun onInterceptTouchEvent(ev: MotionEvent!): Boolean

onKeyDown

open fun onKeyDown(keyCode: Int, event: KeyEvent!): Boolean

onKeyUp

open fun onKeyUp(keyCode: Int, event: KeyEvent!): Boolean

onRtlPropertiesChanged

open fun onRtlPropertiesChanged(layoutDirection: Int): Unit

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

openDrawer

open fun openDrawer(@NonNull drawerView: View): Unit

Open the specified drawer view by animating it into view.

Parameters
drawerView View: Drawer view to open

openDrawer

open fun openDrawer(@NonNull drawerView: View, animate: Boolean): Unit

Open the specified drawer view.

Parameters
drawerView View: Drawer view to open
animate View: Whether opening of the drawer should be animated.

openDrawer

open fun openDrawer(gravity: Int): Unit

Open the specified drawer by animating it out of view.

Parameters
gravity Int: Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.

openDrawer

open fun openDrawer(gravity: Int, animate: Boolean): Unit

Open the specified drawer.

Parameters
gravity Int: Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.
animate Int: Whether opening of the drawer should be animated.

removeDrawerListener

open fun removeDrawerListener(@NonNull listener: DrawerLayout.DrawerListener): Unit

Removes the specified listener from the list of listeners that will be notified of drawer events.

Parameters
listener DrawerLayout.DrawerListener: Listener to remove from being notified of drawer events

requestDisallowInterceptTouchEvent

open fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean): Unit

requestLayout

open fun requestLayout(): Unit

setDrawerElevation

open fun setDrawerElevation(elevation: Float): Unit

Sets the base elevation of the drawer(s) relative to the parent, in pixels. Note that the elevation change is only supported in API 21 and above.

Parameters
elevation Float: The base depth position of the view, in pixels.

setDrawerListener

open fun setDrawerListener(listener: DrawerLayout.DrawerListener!): Unit

Deprecated: Use addDrawerListener(DrawerListener)

Set a listener to be notified of drawer events. Note that this method is deprecated and you should use addDrawerListener(DrawerListener) to add a listener and removeDrawerListener(DrawerListener) to remove a registered listener.

Parameters
listener DrawerLayout.DrawerListener!: Listener to notify when drawer events occur

setDrawerLockMode

open fun setDrawerLockMode(lockMode: Int): Unit

Enable or disable interaction with all drawers.

This allows the application to restrict the user's ability to open or close any drawer within this layout. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.

Locking drawers open or closed will implicitly open or close any drawers as appropriate.

Parameters
lockMode Int: The new lock mode for the given drawer. One of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.

setDrawerLockMode

open fun setDrawerLockMode(lockMode: Int, edgeGravity: Int): Unit

Enable or disable interaction with the given drawer.

This allows the application to restrict the user's ability to open or close the given drawer. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.

Locking a drawer open or closed will implicitly open or close that drawer as appropriate.

Parameters
lockMode Int: The new lock mode for the given drawer. One of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.
edgeGravity Int: Gravity.LEFT, RIGHT, START or END. Expresses which drawer to change the mode for.

setDrawerLockMode

open fun setDrawerLockMode(lockMode: Int, @NonNull drawerView: View): Unit

Enable or disable interaction with the given drawer.

This allows the application to restrict the user's ability to open or close the given drawer. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.

Locking a drawer open or closed will implicitly open or close that drawer as appropriate.

Parameters
lockMode Int: The new lock mode for the given drawer. One of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.
drawerView Int: The drawer view to change the lock mode for

setDrawerShadow

open fun setDrawerShadow(shadowDrawable: Drawable!, gravity: Int): Unit

Set a simple drawable used for the left or right shadow. The drawable provided must have a nonzero intrinsic width. For API 21 and above, an elevation will be set on the drawer instead of using the provided shadow drawable.

Note that for better support for both left-to-right and right-to-left layout directions, a drawable for RTL layout (in additional to the one in LTR layout) can be defined with a resource qualifier "ldrtl" for API 17 and above with the gravity GravityCompat#START. Alternatively, for API 23 and above, the drawable can auto-mirrored such that the drawable will be mirrored in RTL layout.

Parameters
shadowDrawable Drawable!: Shadow drawable to use at the edge of a drawer
gravity Drawable!: Which drawer the shadow should apply to

setDrawerShadow

open fun setDrawerShadow(@DrawableRes resId: Int, gravity: Int): Unit

Set a simple drawable used for the left or right shadow. The drawable provided must have a nonzero intrinsic width. For API 21 and above, an elevation will be set on the drawer instead of using the provided shadow drawable.

Note that for better support for both left-to-right and right-to-left layout directions, a drawable for RTL layout (in additional to the one in LTR layout) can be defined with a resource qualifier "ldrtl" for API 17 and above with the gravity GravityCompat#START. Alternatively, for API 23 and above, the drawable can auto-mirrored such that the drawable will be mirrored in RTL layout.

Parameters
resId Int: Resource id of a shadow drawable to use at the edge of a drawer
gravity Int: Which drawer the shadow should apply to

setDrawerTitle

open fun setDrawerTitle(edgeGravity: Int, @Nullable title: CharSequence?): Unit

Sets the title of the drawer with the given gravity.

When accessibility is turned on, this is the title that will be used to identify the drawer to the active accessibility service.

Parameters
edgeGravity Int: Gravity.LEFT, RIGHT, START or END. Expresses which drawer to set the title for.
title Int: The title for the drawer.

setScrimColor

open fun setScrimColor(color: Int): Unit

Set a color to use for the scrim that obscures primary content while a drawer is open.

Parameters
color Int: Color to use in 0xAARRGGBB format.

setStatusBarBackground

open fun setStatusBarBackground(@Nullable bg: Drawable?): Unit

Set a drawable to draw in the insets area for the status bar. Note that this will only be activated if this DrawerLayout fitsSystemWindows.

Parameters
bg Drawable?: Background drawable to draw behind the status bar

setStatusBarBackground

open fun setStatusBarBackground(resId: Int): Unit

Set a drawable to draw in the insets area for the status bar. Note that this will only be activated if this DrawerLayout fitsSystemWindows.

Parameters
resId Int: Resource id of a background drawable to draw behind the status bar

setStatusBarBackgroundColor

open fun setStatusBarBackgroundColor(color: Int): Unit

Set a drawable to draw in the insets area for the status bar. Note that this will only be activated if this DrawerLayout fitsSystemWindows.

Parameters
color Int: Color to use as a background drawable to draw behind the status bar in 0xAARRGGBB format.

Protected methods

checkLayoutParams

protected open fun checkLayoutParams(p: LayoutParams!): Boolean

drawChild

protected open fun drawChild(canvas: Canvas!, child: View!, drawingTime: Long): Boolean

generateDefaultLayoutParams

protected open fun generateDefaultLayoutParams(): LayoutParams!

generateLayoutParams

protected open fun generateLayoutParams(p: LayoutParams!): LayoutParams!

onAttachedToWindow

protected open fun onAttachedToWindow(): Unit

onDetachedFromWindow

protected open fun onDetachedFromWindow(): Unit

onLayout

protected open fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int): Unit

onMeasure

protected open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

onRestoreInstanceState

protected open fun onRestoreInstanceState(state: Parcelable!): Unit

onSaveInstanceState

protected open fun onSaveInstanceState(): Parcelable!