added in version 22.1.0
belongs to Maven artifact com.android.support:drawerlayout:28.0.0-alpha1

DrawerLayout

public class DrawerLayout
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.v4.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.

DrawerLayout.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. DrawerLayout.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

interface DrawerLayout.DrawerListener

Listener for monitoring events about drawers. 

class DrawerLayout.LayoutParams

 

class DrawerLayout.SavedState

State persisted across instances  

class DrawerLayout.SimpleDrawerListener

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

Constants

int LOCK_MODE_LOCKED_CLOSED

The drawer is locked closed.

int LOCK_MODE_LOCKED_OPEN

The drawer is locked open.

int LOCK_MODE_UNDEFINED

The drawer's lock state is reset to default.

int LOCK_MODE_UNLOCKED

The drawer is unlocked.

int STATE_DRAGGING

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

int STATE_IDLE

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

int STATE_SETTLING

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

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

DrawerLayout(Context context)
DrawerLayout(Context context, AttributeSet attrs)
DrawerLayout(Context context, AttributeSet attrs, int defStyle)

Public methods

void addDrawerListener(DrawerLayout.DrawerListener listener)

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

void addFocusables(ArrayList<View> views, int direction, int focusableMode)
void addView(View child, int index, ViewGroup.LayoutParams params)
void closeDrawer(View drawerView)

Close the specified drawer view by animating it into view.

void closeDrawer(int gravity)

Close the specified drawer by animating it out of view.

void closeDrawer(View drawerView, boolean animate)

Close the specified drawer view.

void closeDrawer(int gravity, boolean animate)

Close the specified drawer.

void closeDrawers()

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

void computeScroll()
ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
float getDrawerElevation()

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

int getDrawerLockMode(int edgeGravity)

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

int getDrawerLockMode(View drawerView)

Check the lock mode of the given drawer view.

CharSequence getDrawerTitle(int edgeGravity)

Returns the title of the drawer with the given gravity.

Drawable getStatusBarBackgroundDrawable()

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

boolean isDrawerOpen(View drawer)

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

boolean isDrawerOpen(int drawerGravity)

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

boolean isDrawerVisible(View drawer)

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

boolean isDrawerVisible(int drawerGravity)

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

void onDraw(Canvas c)
boolean onInterceptTouchEvent(MotionEvent ev)
boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)
void onRtlPropertiesChanged(int layoutDirection)
boolean onTouchEvent(MotionEvent ev)
void openDrawer(View drawerView, boolean animate)

Open the specified drawer view.

void openDrawer(View drawerView)

Open the specified drawer view by animating it into view.

void openDrawer(int gravity)

Open the specified drawer by animating it out of view.

void openDrawer(int gravity, boolean animate)

Open the specified drawer.

void removeDrawerListener(DrawerLayout.DrawerListener listener)

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

void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
void requestLayout()
void setDrawerElevation(float elevation)

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

void setDrawerListener(DrawerLayout.DrawerListener listener)

This method was deprecated in API level 24.1.0. Use addDrawerListener(DrawerListener)

void setDrawerLockMode(int lockMode, View drawerView)

Enable or disable interaction with the given drawer.

void setDrawerLockMode(int lockMode)

Enable or disable interaction with all drawers.

void setDrawerLockMode(int lockMode, int edgeGravity)

Enable or disable interaction with the given drawer.

void setDrawerShadow(int resId, int gravity)

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

void setDrawerShadow(Drawable shadowDrawable, int gravity)

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

void setDrawerTitle(int edgeGravity, CharSequence title)

Sets the title of the drawer with the given gravity.

void setScrimColor(int color)

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

void setStatusBarBackground(Drawable bg)

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

void setStatusBarBackground(int resId)

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

void setStatusBarBackgroundColor(int color)

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

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
boolean drawChild(Canvas canvas, View child, long drawingTime)
ViewGroup.LayoutParams generateDefaultLayoutParams()
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
void onAttachedToWindow()
void onDetachedFromWindow()
void onLayout(boolean changed, int l, int t, int r, int b)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Constants

LOCK_MODE_LOCKED_CLOSED

added in version 22.1.0
int LOCK_MODE_LOCKED_CLOSED

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

Constant Value: 1 (0x00000001)

LOCK_MODE_LOCKED_OPEN

added in version 22.1.0
int LOCK_MODE_LOCKED_OPEN

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

Constant Value: 2 (0x00000002)

LOCK_MODE_UNDEFINED

added in version 24.1.0
int LOCK_MODE_UNDEFINED

The drawer's lock state is reset to default.

Constant Value: 3 (0x00000003)

LOCK_MODE_UNLOCKED

added in version 22.1.0
int LOCK_MODE_UNLOCKED

The drawer is unlocked.

Constant Value: 0 (0x00000000)

STATE_DRAGGING

added in version 22.1.0
int STATE_DRAGGING

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

Constant Value: 1 (0x00000001)

STATE_IDLE

added in version 22.1.0
int STATE_IDLE

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

Constant Value: 0 (0x00000000)

STATE_SETTLING

added in version 22.1.0
int STATE_SETTLING

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

Constant Value: 2 (0x00000002)

Public constructors

DrawerLayout

added in version 22.1.0
DrawerLayout (Context context)

Parameters
context Context

DrawerLayout

added in version 22.1.0
DrawerLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

DrawerLayout

added in version 22.1.0
DrawerLayout (Context context, 
                AttributeSet attrs, 
                int defStyle)

Parameters
context Context

attrs AttributeSet

defStyle int

Public methods

addDrawerListener

added in version 24.1.0
void addDrawerListener (DrawerLayout.DrawerListener listener)

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

void addFocusables (ArrayList<View> views, 
                int direction, 
                int focusableMode)

Parameters
views ArrayList

direction int

focusableMode int

addView

void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

Parameters
child View

index int

params ViewGroup.LayoutParams

closeDrawer

added in version 22.1.0
void closeDrawer (View drawerView)

Close the specified drawer view by animating it into view.

Parameters
drawerView View: Drawer view to close

closeDrawer

added in version 22.1.0
void closeDrawer (int gravity)

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

added in version 24.1.0
void closeDrawer (View drawerView, 
                boolean animate)

Close the specified drawer view.

Parameters
drawerView View: Drawer view to close

animate boolean: Whether closing of the drawer should be animated.

closeDrawer

added in version 24.1.0
void closeDrawer (int gravity, 
                boolean animate)

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 boolean: Whether closing of the drawer should be animated.

closeDrawers

added in version 22.1.0
void closeDrawers ()

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

computeScroll

void computeScroll ()

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
ViewGroup.LayoutParams

getDrawerElevation

added in version 24.1.0
float getDrawerElevation ()

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.

Returns
float The base depth position of the view, in pixels.

getDrawerLockMode

added in version 22.1.0
int getDrawerLockMode (int edgeGravity)

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

Parameters
edgeGravity int: Gravity of the drawer to check

Returns
int one of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.

getDrawerLockMode

added in version 22.1.0
int getDrawerLockMode (View drawerView)

Check the lock mode of the given drawer view.

Parameters
drawerView View: Drawer view to check lock mode

Returns
int one of LOCK_MODE_UNLOCKED, LOCK_MODE_LOCKED_CLOSED or LOCK_MODE_LOCKED_OPEN.

getDrawerTitle

added in version 22.1.0
CharSequence getDrawerTitle (int edgeGravity)

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.

Returns
CharSequence The title of the drawer, or null if none set.

getStatusBarBackgroundDrawable

added in version 22.1.0
Drawable getStatusBarBackgroundDrawable ()

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

Returns
Drawable The status bar background drawable, or null if none set

isDrawerOpen

added in version 22.1.0
boolean isDrawerOpen (View drawer)

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

Returns
boolean true if the given drawer view is in an open state

isDrawerOpen

added in version 22.1.0
boolean isDrawerOpen (int drawerGravity)

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

Returns
boolean true if the given drawer view is in an open state

isDrawerVisible

added in version 22.1.0
boolean isDrawerVisible (View drawer)

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

Returns
boolean true if the given drawer is visible on-screen

isDrawerVisible

added in version 22.1.0
boolean isDrawerVisible (int drawerGravity)

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

Returns
boolean true if the given drawer is visible on-screen

onDraw

added in version 22.1.0
void onDraw (Canvas c)

Parameters
c Canvas

onInterceptTouchEvent

boolean onInterceptTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onKeyDown

boolean onKeyDown (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onKeyUp

boolean onKeyUp (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onRtlPropertiesChanged

void onRtlPropertiesChanged (int layoutDirection)

Parameters
layoutDirection int

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

openDrawer

added in version 24.1.0
void openDrawer (View drawerView, 
                boolean animate)

Open the specified drawer view.

Parameters
drawerView View: Drawer view to open

animate boolean: Whether opening of the drawer should be animated.

openDrawer

added in version 22.1.0
void openDrawer (View drawerView)

Open the specified drawer view by animating it into view.

Parameters
drawerView View: Drawer view to open

openDrawer

added in version 22.1.0
void openDrawer (int gravity)

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

added in version 24.1.0
void openDrawer (int gravity, 
                boolean animate)

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 boolean: Whether opening of the drawer should be animated.

removeDrawerListener

added in version 24.1.0
void removeDrawerListener (DrawerLayout.DrawerListener listener)

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

void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Parameters
disallowIntercept boolean

requestLayout

void requestLayout ()

setDrawerElevation

added in version 24.1.0
void setDrawerElevation (float elevation)

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

added in version 22.1.0
void setDrawerListener (DrawerLayout.DrawerListener listener)

This method was deprecated in API level 24.1.0.
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

added in version 22.1.0
void setDrawerLockMode (int lockMode, 
                View drawerView)

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 View: The drawer view to change the lock mode for

setDrawerLockMode

added in version 22.1.0
void setDrawerLockMode (int lockMode)

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

added in version 22.1.0
void setDrawerLockMode (int lockMode, 
                int edgeGravity)

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.

setDrawerShadow

added in version 22.1.0
void setDrawerShadow (int resId, 
                int gravity)

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 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

setDrawerShadow

added in version 22.1.0
void setDrawerShadow (Drawable shadowDrawable, 
                int gravity)

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 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 int: Which drawer the shadow should apply to

setDrawerTitle

added in version 22.1.0
void setDrawerTitle (int edgeGravity, 
                CharSequence title)

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 CharSequence: The title for the drawer.

setScrimColor

added in version 22.1.0
void setScrimColor (int color)

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

added in version 22.1.0
void setStatusBarBackground (Drawable bg)

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

added in version 22.1.0
void setStatusBarBackground (int resId)

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

added in version 22.1.0
void setStatusBarBackgroundColor (int color)

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

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

drawChild

boolean drawChild (Canvas canvas, 
                View child, 
                long drawingTime)

Parameters
canvas Canvas

child View

drawingTime long

Returns
boolean

generateDefaultLayoutParams

ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onLayout

added in version 22.1.0
void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Parameters
changed boolean

l int

t int

r int

b int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable