DrawerLayout


public class DrawerLayout extends ViewGroup implements Openable


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 types

Listener for monitoring events about drawers.

protected class DrawerLayout.SavedState extends AbsSavedState

State persisted across instances

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

Constants

static final int

The drawer is locked closed.

static final int

The drawer is locked open.

static final int

The drawer's lock state is reset to default.

static final int

The drawer is unlocked.

static final int

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

static final int

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

static final int

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

Public constructors

DrawerLayout(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

Public methods

void

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

Close the START drawer by animating it out of view.

void
closeDrawer(@NonNull 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(@NonNull View drawerView, boolean animate)

Close the specified drawer view.

void
closeDrawer(int gravity, boolean animate)

Close the specified drawer.

void

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

void
boolean
ViewGroup.LayoutParams
float

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

int

Check the lock mode of the given drawer view.

int
getDrawerLockMode(int edgeGravity)

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

@Nullable CharSequence
getDrawerTitle(int edgeGravity)

Returns the title of the drawer with the given gravity.

@Nullable Drawable

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

boolean
isDrawerOpen(int drawerGravity)

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

boolean

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

boolean
isDrawerVisible(int drawerGravity)

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

boolean

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

boolean

Check if the START drawer is currently in an open state.

void
boolean
boolean
onKeyDown(int keyCode, KeyEvent event)
boolean
onKeyUp(int keyCode, KeyEvent event)
void
onRtlPropertiesChanged(int layoutDirection)
boolean
void

Open the START drawer by animating it into view.

void
openDrawer(@NonNull 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(@NonNull View drawerView, boolean animate)

Open the specified drawer view.

void
openDrawer(int gravity, boolean animate)

Open the specified drawer.

void

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

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

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

void

This method is deprecated.

Use addDrawerListener

void
setDrawerLockMode(int lockMode)

Enable or disable interaction with all drawers.

void
setDrawerLockMode(int lockMode, @NonNull View drawerView)

Enable or disable interaction with the given drawer.

void
setDrawerLockMode(int lockMode, int edgeGravity)

Enable or disable interaction with the given drawer.

void
setDrawerShadow(@DrawableRes int resId, int gravity)

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

void
setDrawerShadow(@Nullable Drawable shadowDrawable, int gravity)

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

void
setDrawerTitle(int edgeGravity, @Nullable CharSequence title)

Sets the title of the drawer with the given gravity.

void
setScrimColor(@ColorInt int color)

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

void

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

void

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

void

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

Protected methods

boolean
boolean
drawChild(@NonNull Canvas canvas, View child, long drawingTime)
ViewGroup.LayoutParams
ViewGroup.LayoutParams
void
void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void
@NonNull Parcelable

Inherited Constants

From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

Inherited methods

From android.view.View
void
void
void
ViewPropertyAnimator
void
void
boolean
void
void

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
canScrollHorizontally(int direction)
boolean
canScrollVertically(int direction)
final void
void
final void
boolean
void
void
static int
combineMeasuredStates(int curState, int newState)
int
int
int
WindowInsets
int
int
int
AccessibilityNodeInfo
void
void

This method is deprecated.

boolean
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean
dispatchNestedPreFling(float velocityX, float velocityY)
boolean
dispatchNestedPrePerformAccessibilityAction(
    int action,
    Bundle arguments
)
boolean
dispatchNestedPreScroll(
    int dx,
    int dy,
    int[] consumed,
    int[] offsetInWindow
)
boolean
dispatchNestedScroll(
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int[] offsetInWindow
)
boolean
void
draw(Canvas canvas)
void
drawableHotspotChanged(float x, float y)
final OnBackInvokedDispatcher
final T
<T extends View> findViewById(int id)
final T
<T extends View> findViewWithTag(Object tag)
boolean

This method is deprecated.

void
forceHasOverlappingRendering(boolean hasOverlappingRendering)
void
void
generateDisplayHash(
    String hashAlgorithm,
    Rect bounds,
    Executor executor,
    DisplayHashResultCallback callback
)
static int
View.AccessibilityDelegate
int
AccessibilityNodeProvider
CharSequence
int
int
String
String
float
Animation
Matrix
IBinder
int[]
Map<IntegerInteger>
String[]
final AutofillId
int
AutofillValue
Drawable
BlendMode
ColorStateList
PorterDuff.Mode
int
final int
float
int
float
Rect
boolean
final boolean
final ContentCaptureSession
CharSequence
final Context
ContextMenu.ContextMenuInfo
final boolean
static int
getDefaultSize(int size, int measureSpec)
Display
final int[]
Bitmap

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
long
float
int
boolean
boolean
int
ArrayList<View>
getFocusables(int direction)
void
Drawable
int
BlendMode
ColorStateList
PorterDuff.Mode
boolean
getGlobalVisibleRect(Rect r, Point globalOffset)
Handler
float
float
float