added in version 24.1.0
belongs to Maven artifact com.android.support:coordinatorlayout:28.0.0-alpha1

CoordinatorLayout

public class CoordinatorLayout
extends ViewGroup implements NestedScrollingParent2

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.design.widget.CoordinatorLayout


CoordinatorLayout is a super-powered FrameLayout.

CoordinatorLayout is intended for two primary use cases:

  1. As a top-level application decor or chrome layout
  2. As a container for a specific interaction with one or more child views

By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the CoordinatorLayout.DefaultBehavior annotation.

Behaviors may be used to implement a variety of interactions and additional layout modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons that stick to other elements as they move and animate.

Children of a CoordinatorLayout may have an anchor. This view id must correspond to an arbitrary descendant of the CoordinatorLayout, but it may not be the anchored child itself or a descendant of the anchored child. This can be used to place floating views relative to other arbitrary content panes.

Children can specify insetEdge to describe how the view insets the CoordinatorLayout. Any child views which are set to dodge the same inset edges by dodgeInsetEdges will be moved appropriately so that the views do not overlap.

Summary

Nested classes

interface CoordinatorLayout.AttachedBehavior

Defines the default attached CoordinatorLayout.Behavior of a View class

When writing a custom view, implement this interface to return the default behavior when used as a direct child of an CoordinatorLayout

class CoordinatorLayout.Behavior<V extends View>

Interaction behavior plugin for child views of CoordinatorLayout

@interface CoordinatorLayout.DefaultBehavior

This @interface was deprecated in API level 27.1.0. Use CoordinatorLayout.AttachedBehavior instead  

class CoordinatorLayout.LayoutParams

Parameters describing the desired layout for a child of a CoordinatorLayout

class CoordinatorLayout.SavedState

 

Inherited constants

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

Inherited fields

From class android.view.View

Public constructors

CoordinatorLayout(Context context)
CoordinatorLayout(Context context, AttributeSet attrs)
CoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void dispatchDependentViewsChanged(View view)

Allows the caller to manually dispatch onDependentViewChanged(CoordinatorLayout, View, View) to the associated CoordinatorLayout.Behavior instances of views which depend on the provided View.

boolean doViewsOverlap(View first, View second)

Check whether two views overlap each other.

CoordinatorLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
List<View> getDependencies(View child)

Returns the list of views which the provided view depends on.

List<View> getDependents(View child)

Returns the list of views which depend on the provided view.

int getNestedScrollAxes()
Drawable getStatusBarBackground()

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

boolean isPointInChildBounds(View child, int x, int y)

Check if a given point in the CoordinatorLayout's coordinates are within the view bounds of the given direct child view.

void onAttachedToWindow()
void onDetachedFromWindow()
void onDraw(Canvas c)
boolean onInterceptTouchEvent(MotionEvent ev)
void onLayoutChild(View child, int layoutDirection)

Called to lay out each individual child view unless a Behavior is present.

void onMeasureChild(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

Called to measure each individual child view unless a Behavior is present.

boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed)
boolean onNestedPreFling(View target, float velocityX, float velocityY)
void onNestedPreScroll(View target, int dx, int dy, int[] consumed)
void onNestedPreScroll(View target, int dx, int dy, int[] consumed, int type)

React to a nested scroll in progress before the target view consumes a portion of the scroll.

void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type)

React to a nested scroll in progress.

void onNestedScrollAccepted(View child, View target, int nestedScrollAxes, int type)

React to the successful claiming of a nested scroll operation.

void onNestedScrollAccepted(View child, View target, int nestedScrollAxes)
boolean onStartNestedScroll(View child, View target, int axes, int type)

React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.

boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)
void onStopNestedScroll(View target)
void onStopNestedScroll(View target, int type)

React to a nested scroll operation ending.

boolean onTouchEvent(MotionEvent ev)
boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)
void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
void setFitsSystemWindows(boolean fitSystemWindows)
void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener onHierarchyChangeListener)
void setStatusBarBackground(Drawable bg)

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.

void setStatusBarBackgroundResource(int resId)

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

void setVisibility(int visibility)

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
boolean drawChild(Canvas canvas, View child, long drawingTime)
void drawableStateChanged()
CoordinatorLayout.LayoutParams generateDefaultLayoutParams()
CoordinatorLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
int getSuggestedMinimumHeight()
int getSuggestedMinimumWidth()
void onLayout(boolean changed, int l, int t, int r, int b)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()
boolean verifyDrawable(Drawable who)

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
From interface android.support.v4.view.NestedScrollingParent2
From interface android.support.v4.view.NestedScrollingParent

Public constructors

CoordinatorLayout

added in version 24.1.0
CoordinatorLayout (Context context)

Parameters
context Context

CoordinatorLayout

added in version 24.1.0
CoordinatorLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CoordinatorLayout

added in version 24.1.0
CoordinatorLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

dispatchDependentViewsChanged

added in version 24.1.0
void dispatchDependentViewsChanged (View view)

Allows the caller to manually dispatch onDependentViewChanged(CoordinatorLayout, View, View) to the associated CoordinatorLayout.Behavior instances of views which depend on the provided View.

You should not normally need to call this method as the it will be automatically done when the view has changed.

Parameters
view View: the View to find dependents of to dispatch the call.

doViewsOverlap

added in version 24.1.0
boolean doViewsOverlap (View first, 
                View second)

Check whether two views overlap each other. The views need to be descendants of this CoordinatorLayout in the view hierarchy.

Parameters
first View: first child view to test

second View: second child view to test

Returns
boolean true if both views are visible and overlap each other

generateLayoutParams

added in version 26.1.0
CoordinatorLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
CoordinatorLayout.LayoutParams

getDependencies

added in version 24.1.0
List<View> getDependencies (View child)

Returns the list of views which the provided view depends on. Do not store this list as its contents may not be valid beyond the caller.

Parameters
child View: the view to find dependencies for.

Returns
List<View> the list of views which child depends on.

getDependents

added in version 25.1.0
List<View> getDependents (View child)

Returns the list of views which depend on the provided view. Do not store this list as its contents may not be valid beyond the caller.

Parameters
child View: the view to find dependents of.

Returns
List<View> the list of views which depend on child.

getNestedScrollAxes

int getNestedScrollAxes ()

Returns
int

getStatusBarBackground

added in version 24.1.0
Drawable getStatusBarBackground ()

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

isPointInChildBounds

added in version 24.1.0
boolean isPointInChildBounds (View child, 
                int x, 
                int y)

Check if a given point in the CoordinatorLayout's coordinates are within the view bounds of the given direct child view.

Parameters
child View: child view to test

x int: X coordinate to test, in the CoordinatorLayout's coordinate system

y int: Y coordinate to test, in the CoordinatorLayout's coordinate system

Returns
boolean true if the point is within the child view's bounds, false otherwise

onAttachedToWindow

added in version 24.1.0
void onAttachedToWindow ()

onDetachedFromWindow

added in version 24.1.0
void onDetachedFromWindow ()

onDraw

added in version 24.1.0
void onDraw (Canvas c)

Parameters
c Canvas

onInterceptTouchEvent

boolean onInterceptTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onLayoutChild

added in version 24.1.0
void onLayoutChild (View child, 
                int layoutDirection)

Called to lay out each individual child view unless a Behavior is present. The Behavior may choose to delegate child measurement to this method.

Parameters
child View: child view to lay out

layoutDirection int: the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.

onMeasureChild

added in version 24.1.0
void onMeasureChild (View child, 
                int parentWidthMeasureSpec, 
                int widthUsed, 
                int parentHeightMeasureSpec, 
                int heightUsed)

Called to measure each individual child view unless a Behavior is present. The Behavior may choose to delegate child measurement to this method.

Parameters
child View: the child to measure

parentWidthMeasureSpec int: the width requirements for this view

widthUsed int: extra space that has been used up by the parent horizontally (possibly by other children of the parent)

parentHeightMeasureSpec int: the height requirements for this view

heightUsed int: extra space that has been used up by the parent vertically (possibly by other children of the parent)

onNestedFling

boolean onNestedFling (View target, 
                float velocityX, 
                float velocityY, 
                boolean consumed)

Parameters
target View

velocityX float

velocityY float

consumed boolean

Returns
boolean

onNestedPreFling

boolean onNestedPreFling (View target, 
                float velocityX, 
                float velocityY)

Parameters
target View

velocityX float

velocityY float

Returns
boolean

onNestedPreScroll

void onNestedPreScroll (View target, 
                int dx, 
                int dy, 
                int[] consumed)

Parameters
target View

dx int

dy int

consumed int

onNestedPreScroll

added in version 26.1.0
void onNestedPreScroll (View target, 
                int dx, 
                int dy, 
                int[] consumed, 
                int type)

React to a nested scroll in progress before the target view consumes a portion of the scroll.

When working with nested scrolling often the parent view may want an opportunity to consume the scroll before the nested scrolling child does. An example of this is a drawer that contains a scrollable list. The user will want to be able to scroll the list fully into view before the list itself begins scrolling.

onNestedPreScroll is called when a nested scrolling child invokes dispatchNestedPreScroll(int, int, int[], int[]). The implementation should report how any pixels of the scroll reported by dx, dy were consumed in the consumed array. Index 0 corresponds to dx and index 1 corresponds to dy. This parameter will never be null. Initial values for consumed[0] and consumed[1] will always be 0.

Parameters
target View: View that initiated the nested scroll

dx int: Horizontal scroll distance in pixels

dy int: Vertical scroll distance in pixels

consumed int: Output. The horizontal and vertical scroll distance consumed by this parent

type int: the type of input which cause this scroll event

onNestedScroll

void onNestedScroll (View target, 
                int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed)

Parameters
target View

dxConsumed int

dyConsumed int

dxUnconsumed int

dyUnconsumed int

onNestedScroll

added in version 26.1.0
void onNestedScroll (View target, 
                int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed, 
                int type)

React to a nested scroll in progress.

This method will be called when the ViewParent's current nested scrolling child view dispatches a nested scroll event. To receive calls to this method the ViewParent must have previously returned true for a call to onStartNestedScroll(View, View, int, int).

Both the consumed and unconsumed portions of the scroll distance are reported to the ViewParent. An implementation may choose to use the consumed portion to match or chase scroll position of multiple child elements, for example. The unconsumed portion may be used to allow continuous dragging of multiple scrolling or draggable elements, such as scrolling a list within a vertical drawer where the drawer begins dragging once the edge of inner scrolling content is reached.

Parameters
target View: The descendent view controlling the nested scroll

dxConsumed int: Horizontal scroll distance in pixels already consumed by target

dyConsumed int: Vertical scroll distance in pixels already consumed by target

dxUnconsumed int: Horizontal scroll distance in pixels not consumed by target

dyUnconsumed int: Vertical scroll distance in pixels not consumed by target

type int: the type of input which cause this scroll event

onNestedScrollAccepted

added in version 26.1.0
void onNestedScrollAccepted (View child, 
                View target, 
                int nestedScrollAxes, 
                int type)

React to the successful claiming of a nested scroll operation.

This method will be called after onStartNestedScroll returns true. It offers an opportunity for the view and its superclasses to perform initial configuration for the nested scroll. Implementations of this method should always call their superclass's implementation of this method if one is present.

Parameters
child View: Direct child of this ViewParent containing target

target View: View that initiated the nested scroll

nestedScrollAxes int: Flags consisting of SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL or both

type int: the type of input which cause this scroll event

onNestedScrollAccepted

void onNestedScrollAccepted (View child, 
                View target, 
                int nestedScrollAxes)

Parameters
child View

target View

nestedScrollAxes int

onStartNestedScroll

added in version 26.1.0
boolean onStartNestedScroll (View child, 
                View target, 
                int axes, 
                int type)

React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.

This method will be called in response to a descendant view invoking startNestedScroll(View, int). Each parent up the view hierarchy will be given an opportunity to respond and claim the nested scrolling operation by returning true.

This method may be overridden by ViewParent implementations to indicate when the view is willing to support a nested scrolling operation that is about to begin. If it returns true, this ViewParent will become the target view's nested scrolling parent for the duration of the scroll operation in progress. When the nested scroll is finished this ViewParent will receive a call to onStopNestedScroll(View, int).

Parameters
child View: Direct child of this ViewParent containing target

target View: View that initiated the nested scroll

axes int: Flags consisting of SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL or both

type int: the type of input which cause this scroll event

Returns
boolean true if this ViewParent accepts the nested scroll operation

onStartNestedScroll

boolean onStartNestedScroll (View child, 
                View target, 
                int nestedScrollAxes)

Parameters
child View

target View

nestedScrollAxes int

Returns
boolean

onStopNestedScroll

void onStopNestedScroll (View target)

Parameters
target View

onStopNestedScroll

added in version 26.1.0
void onStopNestedScroll (View target, 
                int type)

React to a nested scroll operation ending.

Perform cleanup after a nested scrolling operation. This method will be called when a nested scroll stops, for example when a nested touch scroll ends with a ACTION_UP or ACTION_CANCEL event. Implementations of this method should always call their superclass's implementation of this method if one is present.

Parameters
target View: View that initiated the nested scroll

type int: the type of input which cause this scroll event

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

requestChildRectangleOnScreen

boolean requestChildRectangleOnScreen (View child, 
                Rect rectangle, 
                boolean immediate)

Parameters
child View

rectangle Rect

immediate boolean

Returns
boolean

requestDisallowInterceptTouchEvent

void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Parameters
disallowIntercept boolean

setFitsSystemWindows

void setFitsSystemWindows (boolean fitSystemWindows)

Parameters
fitSystemWindows boolean

setOnHierarchyChangeListener

void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener onHierarchyChangeListener)

Parameters
onHierarchyChangeListener ViewGroup.OnHierarchyChangeListener

setStatusBarBackground

added in version 24.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

setStatusBarBackgroundColor

added in version 24.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.

setStatusBarBackgroundResource

added in version 24.1.0
void setStatusBarBackgroundResource (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

setVisibility

void setVisibility (int visibility)

Parameters
visibility int

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

drawableStateChanged

void drawableStateChanged ()

generateDefaultLayoutParams

added in version 26.1.0
CoordinatorLayout.LayoutParams generateDefaultLayoutParams ()

Returns
CoordinatorLayout.LayoutParams

generateLayoutParams

added in version 26.1.0
CoordinatorLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
CoordinatorLayout.LayoutParams

getSuggestedMinimumHeight

int getSuggestedMinimumHeight ()

Returns
int

getSuggestedMinimumWidth

int getSuggestedMinimumWidth ()

Returns
int

onLayout

added in version 24.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

verifyDrawable

boolean verifyDrawable (Drawable who)

Parameters
who Drawable

Returns
boolean