Register now for Android Dev Summit 2019!

BottomSheetBehavior

public class BottomSheetBehavior
extends Behavior<V extends View>

java.lang.Object
   ↳ androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior<V extends android.view.View>
     ↳ com.google.android.material.bottomsheet.BottomSheetBehavior<V extends android.view.View>


An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.

Summary

Nested classes

class BottomSheetBehavior.BottomSheetCallback

Callback for monitoring events about bottom sheets. 

class BottomSheetBehavior.SavedState

State persisted across instances  

XML attributes

BottomSheetBehavior_Layout_behavior_hideable  
BottomSheetBehavior_Layout_behavior_peekHeight  
BottomSheetBehavior_Layout_behavior_saveFlags  
BottomSheetBehavior_Layout_behavior_skipCollapsed  

Constants

int PEEK_HEIGHT_AUTO

Peek at the 16:9 ratio keyline of its parent.

int SAVE_ALL

This flag will preserve all aforementioned values on configuration change.

int SAVE_FIT_TO_CONTENTS

This flag will preserve the fitToContents boolean value on configuration change.

int SAVE_HIDEABLE

This flag will preserve the hideable boolean value on configuration change.

int SAVE_NONE

This flag will not preserve the aforementioned values set at runtime if the view is destroyed and recreated.

int SAVE_PEEK_HEIGHT

This flag will preserve the peekHeight int value on configuration change.

int SAVE_SKIP_COLLAPSED

This flag will preserve the skipCollapsed boolean value on configuration change.

int STATE_COLLAPSED

The bottom sheet is collapsed.

int STATE_DRAGGING

The bottom sheet is dragging.

int STATE_EXPANDED

The bottom sheet is expanded.

int STATE_HALF_EXPANDED

The bottom sheet is half-expanded (used when mFitToContents is false).

int STATE_HIDDEN

The bottom sheet is hidden.

int STATE_SETTLING

The bottom sheet is settling.

Public constructors

BottomSheetBehavior()
BottomSheetBehavior(Context context, AttributeSet attrs)

Public methods

void disableShapeAnimations()
static <V extends View> BottomSheetBehavior<V> from(V view)

A utility function to get the BottomSheetBehavior associated with the view.

float getHalfExpandedRatio()

Gets the ratio for the height of the BottomSheet in the STATE_HALF_EXPANDED state.

final int getPeekHeight()

Gets the height of the bottom sheet when it is collapsed.

int getSaveFlags()

Returns the save flags.

boolean getSkipCollapsed()

Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once.

final int getState()

Gets the current state of the bottom sheet.

boolean isFitToContents()
boolean isHideable()

Gets whether this bottom sheet can hide when it is swiped down.

void onAttachedToLayoutParams(CoordinatorLayout.LayoutParams layoutParams)
void onDetachedFromLayoutParams()
boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection)
boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY)
void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed, int type)
void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, int[] consumed)
void onRestoreInstanceState(CoordinatorLayout parent, V child, Parcelable state)
Parcelable onSaveInstanceState(CoordinatorLayout parent, V child)
boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int axes, int type)
void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target, int type)
boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)

Sets a callback to be notified of bottom sheet events.

void setFitToContents(boolean fitToContents)

Sets whether the height of the expanded sheet is determined by the height of its contents, or if it is expanded in two stages (half the height of the parent container, full height of parent container).

void setHalfExpandedRatio(float ratio)

Determines the height of the BottomSheet in the STATE_HALF_EXPANDED state.

void setHideable(boolean hideable)

Sets whether this bottom sheet can hide when it is swiped down.

final void setPeekHeight(int peekHeight, boolean animate)

Sets the height of the bottom sheet when it is collapsed while optionally animating between the old height and the new height.

final void setPeekHeight(int peekHeight)

Sets the height of the bottom sheet when it is collapsed.

void setSaveFlags(int flags)

Sets save flags to be preserved in bottomsheet on configuration change.

void setSkipCollapsed(boolean skipCollapsed)

Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once.

final void setState(int state)

Sets the state of the bottom sheet.

Inherited methods

From class androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior
From class java.lang.Object

XML attributes

BottomSheetBehavior_Layout_behavior_hideable

Related methods:

BottomSheetBehavior_Layout_behavior_peekHeight

Related methods:

BottomSheetBehavior_Layout_behavior_saveFlags

Related methods:

BottomSheetBehavior_Layout_behavior_skipCollapsed

Related methods:

Constants

PEEK_HEIGHT_AUTO

int PEEK_HEIGHT_AUTO

Peek at the 16:9 ratio keyline of its parent.

This can be used as a parameter for setPeekHeight(int). getPeekHeight() will return this when the value is set.

Constant Value: -1 (0xffffffff)

SAVE_ALL

int SAVE_ALL

This flag will preserve all aforementioned values on configuration change.

Constant Value: -1 (0xffffffff)

SAVE_FIT_TO_CONTENTS

int SAVE_FIT_TO_CONTENTS

This flag will preserve the fitToContents boolean value on configuration change.

Constant Value: 2 (0x00000002)

SAVE_HIDEABLE

int SAVE_HIDEABLE

This flag will preserve the hideable boolean value on configuration change.

Constant Value: 4 (0x00000004)

SAVE_NONE

int SAVE_NONE

This flag will not preserve the aforementioned values set at runtime if the view is destroyed and recreated. The only value preserved will be the positional state, e.g. collapsed, hidden, expanded, etc. This is the default behavior.

Constant Value: 0 (0x00000000)

SAVE_PEEK_HEIGHT

int SAVE_PEEK_HEIGHT

This flag will preserve the peekHeight int value on configuration change.

Constant Value: 1 (0x00000001)

SAVE_SKIP_COLLAPSED

int SAVE_SKIP_COLLAPSED

This flag will preserve the skipCollapsed boolean value on configuration change.

Constant Value: 8 (0x00000008)

STATE_COLLAPSED

int STATE_COLLAPSED

The bottom sheet is collapsed.

Constant Value: 4 (0x00000004)

STATE_DRAGGING

int STATE_DRAGGING

The bottom sheet is dragging.

Constant Value: 1 (0x00000001)

STATE_EXPANDED

int STATE_EXPANDED

The bottom sheet is expanded.

Constant Value: 3 (0x00000003)

STATE_HALF_EXPANDED

int STATE_HALF_EXPANDED

The bottom sheet is half-expanded (used when mFitToContents is false).

Constant Value: 6 (0x00000006)

STATE_HIDDEN

int STATE_HIDDEN

The bottom sheet is hidden.

Constant Value: 5 (0x00000005)

STATE_SETTLING

int STATE_SETTLING

The bottom sheet is settling.

Constant Value: 2 (0x00000002)

Public constructors

BottomSheetBehavior

BottomSheetBehavior ()

BottomSheetBehavior

BottomSheetBehavior (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Public methods

disableShapeAnimations

void disableShapeAnimations ()

from

BottomSheetBehavior<V> from (V view)

A utility function to get the BottomSheetBehavior associated with the view.

Parameters
view V: The View with BottomSheetBehavior.

Returns
BottomSheetBehavior<V> The BottomSheetBehavior associated with the view.

getHalfExpandedRatio

float getHalfExpandedRatio ()

Gets the ratio for the height of the BottomSheet in the STATE_HALF_EXPANDED state.

Returns
float

getPeekHeight

int getPeekHeight ()

Gets the height of the bottom sheet when it is collapsed.

Related XML Attributes:

Returns
int The height of the collapsed bottom sheet in pixels, or PEEK_HEIGHT_AUTO if the sheet is configured to peek automatically at 16:9 ratio keyline

getSaveFlags

int getSaveFlags ()

Returns the save flags.

Related XML Attributes:

Returns
int

See also:

getSkipCollapsed

boolean getSkipCollapsed ()

Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once.

Related XML Attributes:

Returns
boolean Whether the bottom sheet should skip the collapsed state.

getState

int getState ()

Gets the current state of the bottom sheet.

Returns
int One of STATE_EXPANDED, STATE_HALF_EXPANDED, STATE_COLLAPSED, STATE_DRAGGING, STATE_SETTLING, or STATE_HALF_EXPANDED.

isFitToContents

boolean isFitToContents ()

Returns
boolean whether the height of the expanded sheet is determined by the height of its contents, or if it is expanded in two stages (half the height of the parent container, full height of parent container).

isHideable

boolean isHideable ()

Gets whether this bottom sheet can hide when it is swiped down.

Related XML Attributes:

Returns
boolean true if this bottom sheet can hide.

onAttachedToLayoutParams

void onAttachedToLayoutParams (CoordinatorLayout.LayoutParams layoutParams)

Parameters
layoutParams CoordinatorLayout.LayoutParams

onDetachedFromLayoutParams

void onDetachedFromLayoutParams ()

onInterceptTouchEvent

boolean onInterceptTouchEvent (CoordinatorLayout parent, 
                V child, 
                MotionEvent event)

Parameters
parent CoordinatorLayout

child V

event MotionEvent

Returns
boolean

onLayoutChild

boolean onLayoutChild (CoordinatorLayout parent, 
                V child, 
                int layoutDirection)

Parameters
parent CoordinatorLayout

child V

layoutDirection int

Returns
boolean

onNestedPreFling

boolean onNestedPreFling (CoordinatorLayout coordinatorLayout, 
                V child, 
                View target, 
                float velocityX, 
                float velocityY)

Parameters
coordinatorLayout CoordinatorLayout

child V

target View

velocityX float

velocityY float

Returns
boolean

onNestedPreScroll

void onNestedPreScroll (CoordinatorLayout coordinatorLayout, 
                V child, 
                View target, 
                int dx, 
                int dy, 
                int[] consumed, 
                int type)

Parameters
coordinatorLayout CoordinatorLayout

child V

target View

dx int

dy int

consumed int

type int

onNestedScroll

void onNestedScroll (CoordinatorLayout coordinatorLayout, 
                V child, 
                View target, 
                int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed, 
                int type, 
                int[] consumed)

Parameters
coordinatorLayout CoordinatorLayout

child V

target View

dxConsumed int

dyConsumed int

dxUnconsumed int

dyUnconsumed int

type int

consumed int

onRestoreInstanceState

void onRestoreInstanceState (CoordinatorLayout parent, 
                V child, 
                Parcelable state)

Parameters
parent CoordinatorLayout

child V

state Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState (CoordinatorLayout parent, 
                V child)

Parameters
parent CoordinatorLayout

child V

Returns
Parcelable

onStartNestedScroll

boolean onStartNestedScroll (CoordinatorLayout coordinatorLayout, 
                V child, 
                View directTargetChild, 
                View target, 
                int axes, 
                int type)

Parameters
coordinatorLayout CoordinatorLayout

child V

directTargetChild View

target View

axes int

type int

Returns
boolean

onStopNestedScroll

void onStopNestedScroll (CoordinatorLayout coordinatorLayout, 
                V child, 
                View target, 
                int type)

Parameters
coordinatorLayout CoordinatorLayout

child V

target View

type int

onTouchEvent

boolean onTouchEvent (CoordinatorLayout parent, 
                V child, 
                MotionEvent event)

Parameters
parent CoordinatorLayout

child V

event MotionEvent

Returns
boolean

setBottomSheetCallback

void setBottomSheetCallback (BottomSheetBehavior.BottomSheetCallback callback)

Sets a callback to be notified of bottom sheet events.

Parameters
callback BottomSheetBehavior.BottomSheetCallback: The callback to notify when bottom sheet events occur.

setFitToContents

void setFitToContents (boolean fitToContents)

Sets whether the height of the expanded sheet is determined by the height of its contents, or if it is expanded in two stages (half the height of the parent container, full height of parent container). Default value is true.

Parameters
fitToContents boolean: whether or not to fit the expanded sheet to its contents.

setHalfExpandedRatio

void setHalfExpandedRatio (float ratio)

Determines the height of the BottomSheet in the STATE_HALF_EXPANDED state. The material guidelines recommended a value of 0.5, which results in the sheet filling half of the parent. The height of the BottomSheet will be smaller as this ratio is decreased and taller as it is increased. The default value is 0.5.

Parameters
ratio float: a float between 0 and 1, representing the STATE_HALF_EXPANDED ratio.

setHideable

void setHideable (boolean hideable)

Sets whether this bottom sheet can hide when it is swiped down.

Related XML Attributes:

Parameters
hideable boolean: true to make this bottom sheet hideable.

setPeekHeight

void setPeekHeight (int peekHeight, 
                boolean animate)

Sets the height of the bottom sheet when it is collapsed while optionally animating between the old height and the new height.

Related XML Attributes:

Parameters
peekHeight int: The height of the collapsed bottom sheet in pixels, or PEEK_HEIGHT_AUTO to configure the sheet to peek automatically at 16:9 ratio keyline.

animate boolean: Whether to animate between the old height and the new height.

setPeekHeight

void setPeekHeight (int peekHeight)

Sets the height of the bottom sheet when it is collapsed.

Related XML Attributes:

Parameters
peekHeight int: The height of the collapsed bottom sheet in pixels, or PEEK_HEIGHT_AUTO to configure the sheet to peek automatically at 16:9 ratio keyline.

setSaveFlags

void setSaveFlags (int flags)

Sets save flags to be preserved in bottomsheet on configuration change.

Related XML Attributes:

Parameters
flags int: bitwise int of SAVE_PEEK_HEIGHT, SAVE_FIT_TO_CONTENTS, SAVE_HIDEABLE, SAVE_SKIP_COLLAPSED, SAVE_ALL and SAVE_NONE.

See also:

setSkipCollapsed

void setSkipCollapsed (boolean skipCollapsed)

Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once. Setting this to true has no effect unless the sheet is hideable.

Related XML Attributes:

Parameters
skipCollapsed boolean: True if the bottom sheet should skip the collapsed state.

setState

void setState (int state)

Sets the state of the bottom sheet. The bottom sheet will transition to that state with animation.

Parameters
state int: One of STATE_COLLAPSED, STATE_EXPANDED, STATE_HIDDEN, or STATE_HALF_EXPANDED.