Most visited

Recently visited

BottomSheetBehavior

public class BottomSheetBehavior
extends Behavior<V extends View>

java.lang.Object
   ↳ android.support.design.widget.CoordinatorLayout.Behavior<V extends android.view.View>
     ↳ android.support.design.widget.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

 

XML attributes

android.support.design:behavior_hideable Whether this bottom sheet can be hidden by dragging it further downwards

May be a boolean value, such as "true" or "false". 

android.support.design:behavior_peekHeight The height of the bottom sheet when it is collapsed. 
android.support.design:behavior_skipCollapsed Skip the collapsed state once expanded; no effect unless it is hideable

May be a boolean value, such as "true" or "false". 

Constants

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_HIDDEN

The bottom sheet is hidden.

int STATE_SETTLING

The bottom sheet is settling.

Public constructors

BottomSheetBehavior()

Default constructor for instantiating BottomSheetBehaviors.

BottomSheetBehavior(Context context, AttributeSet attrs)

Default constructor for inflating BottomSheetBehaviors from layout.

Public methods

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

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

final int getPeekHeight()

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

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 isHideable()

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

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

Respond to CoordinatorLayout touch events before they are dispatched to child views.

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

Called when the parent CoordinatorLayout is about the lay out the given child view.

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

Called when a nested scrolling child is about to start a fling.

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

Called when a nested scroll in progress is about to update, before the target has consumed any of the scrolled distance.

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

Hook allowing a behavior to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(CoordinatorLayout, V).

Parcelable onSaveInstanceState(CoordinatorLayout parent, V child)

Hook allowing a behavior to generate a representation of its internal state that can later be used to create a new instance with that same state.

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

Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll.

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

Called when a nested scroll has ended.

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

Respond to CoordinatorLayout touch events after this Behavior has started intercepting them.

void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)

Sets a callback to be notified of bottom sheet events.

void setHideable(boolean hideable)

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

final void setPeekHeight(int peekHeight)

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

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 android.support.design.widget.CoordinatorLayout.Behavior
From class java.lang.Object

XML attributes

android.support.design:behavior_hideable

Whether this bottom sheet can be hidden by dragging it further downwards

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:behavior_peekHeight

The height of the bottom sheet when it is collapsed.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android.support.design:behavior_skipCollapsed

Skip the collapsed state once expanded; no effect unless it is hideable

May be a boolean value, such as "true" or "false".

Related methods:

Constants

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_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 ()

Default constructor for instantiating BottomSheetBehaviors.

BottomSheetBehavior

BottomSheetBehavior (Context context, 
                AttributeSet attrs)

Default constructor for inflating BottomSheetBehaviors from layout.

Parameters
context Context: The Context.
attrs AttributeSet: The AttributeSet.

Public methods

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.

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.

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_COLLAPSED, STATE_DRAGGING, and STATE_SETTLING.

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.

onInterceptTouchEvent

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

Respond to CoordinatorLayout touch events before they are dispatched to child views.

Behaviors can use this to monitor inbound touch events until one decides to intercept the rest of the event stream to take an action on its associated child view. This method will return false until it detects the proper intercept conditions, then return true once those conditions have occurred.

Once a Behavior intercepts touch events, the rest of the event stream will be sent to the onTouchEvent(CoordinatorLayout, V, MotionEvent) method.

The default implementation of this method always returns false.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event
child V: the child view associated with this Behavior
event MotionEvent: the MotionEvent describing the touch event being processed
Returns
boolean true if this Behavior would like to intercept and take over the event stream. The default always returns false.

onLayoutChild

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

Called when the parent CoordinatorLayout is about the lay out the given child view.

This method can be used to perform custom or modified layout of a child view in place of the default child layout behavior. The Behavior's implementation can delegate to the standard CoordinatorLayout measurement behavior by calling parent.onLayoutChild.

If a Behavior implements onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View) to change the position of a view in response to a dependent view changing, it should also implement onLayoutChild in such a way that respects those dependent views. onLayoutChild will always be called for a dependent view after its dependency has been laid out.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
child V: child view to lay out
layoutDirection int: the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.
Returns
boolean true if the Behavior performed layout of the child view, false to request default layout behavior

onNestedPreFling

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

Called when a nested scrolling child is about to start a fling.

Any Behavior associated with the direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onNestedPreFling is called when the current nested scrolling child view detects the proper conditions for a fling, but it has not acted on it yet. A Behavior can return true to indicate that it consumed the fling. If at least one Behavior returns true, the fling should not be acted upon by the child.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child V: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout performing the nested scroll
velocityX float: horizontal velocity of the attempted fling
velocityY float: vertical velocity of the attempted fling
Returns
boolean true if the Behavior consumed the fling

onNestedPreScroll

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

Called when a nested scroll in progress is about to update, before the target has consumed any of the scrolled distance.

Any Behavior associated with the direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onNestedPreScroll is called each time the nested scroll is updated by the nested scrolling child, before the nested scrolling child has consumed the scroll distance itself. Each Behavior responding to the nested scroll will receive the same values. The CoordinatorLayout will report as consumed the maximum number of pixels in either direction that any Behavior responding to the nested scroll reported as consumed.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child V: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout performing the nested scroll
dx int: the raw horizontal number of pixels that the user attempted to scroll
dy int: the raw vertical number of pixels that the user attempted to scroll
consumed int: out parameter. consumed[0] should be set to the distance of dx that was consumed, consumed[1] should be set to the distance of dy that was consumed

onRestoreInstanceState

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

Hook allowing a behavior to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(CoordinatorLayout, V). This function will never be called with a null state.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
child V: child view to restore from
state Parcelable: The frozen state that had previously been returned by onSaveInstanceState(CoordinatorLayout, V).

onSaveInstanceState

Parcelable onSaveInstanceState (CoordinatorLayout parent, 
                V child)

Hook allowing a behavior to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later.

Behavior state is only saved when both the parent CoordinatorLayout and a view using this behavior have valid IDs set.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
child V: child view to restore from
Returns
Parcelable Returns a Parcelable object containing the behavior's current dynamic state.

onStartNestedScroll

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

Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll.

Any Behavior associated with any direct child of the CoordinatorLayout may respond to this event and return true to indicate that the CoordinatorLayout should act as a nested scrolling parent for this scroll. Only Behaviors that return true from this method will receive subsequent nested scroll events.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child V: the child view of the CoordinatorLayout this Behavior is associated with
directTargetChild View: the child view of the CoordinatorLayout that either is or contains the target of the nested scroll operation
target View: the descendant view of the CoordinatorLayout initiating the nested scroll
nestedScrollAxes int: the axes that this nested scroll applies to. See SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL
Returns
boolean true if the Behavior wishes to accept this nested scroll

onStopNestedScroll

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

Called when a nested scroll has ended.

Any Behavior associated with any direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onStopNestedScroll marks the end of a single nested scroll event sequence. This is a good place to clean up any state related to the nested scroll.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child V: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout that initiated the nested scroll

onTouchEvent

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

Respond to CoordinatorLayout touch events after this Behavior has started intercepting them.

Behaviors may intercept touch events in order to help the CoordinatorLayout manipulate its child views. For example, a Behavior may allow a user to drag a UI pane open or closed. This method should perform actual mutations of view layout state.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event
child V: the child view associated with this Behavior
event MotionEvent: the MotionEvent describing the touch event being processed
Returns
boolean true if this Behavior handled this touch event and would like to continue receiving events in this stream. The default always returns false.

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.

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)

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.

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, or STATE_HIDDEN.

Hooray!