Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

CoordinatorLayout

open class CoordinatorLayout : ViewGroup, NestedScrollingParent2, NestedScrollingParent3
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.coordinatorlayout.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 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 LayoutParams#insetEdge to describe how the view insets the CoordinatorLayout. Any child views which are set to dodge the same inset edges by LayoutParams#dodgeInsetEdges will be moved appropriately so that the views do not overlap.

Summary

Nested classes

Defines the default Behavior of a View class.

abstract

Defines the default attached Behavior of a View class

abstract

Interaction behavior plugin for child views of CoordinatorLayout.

open

Parameters describing the desired layout for a child of a CoordinatorLayout.

open

Public constructors
<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, @AttrRes defStyleAttr: Int)

Public methods
open Unit

open Unit
onLayoutChild(@NonNull child: View, layoutDirection: Int)

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

open Boolean

open Int

open Boolean
onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean)

open Unit

open MutableList<View!>
getDependencies(@NonNull child: View)

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

open Drawable?

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

open Unit

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

open Unit

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

open Unit
setFitsSystemWindows(fitSystemWindows: Boolean)

open Unit
onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray)

open Unit
onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int)

open Boolean
doViewsOverlap(@NonNull first: View, @NonNull second: View)

Check whether two views overlap each other.

open Unit
setStatusBarBackgroundResource(@DrawableRes resId: Int)

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

open Unit

open Unit
onStopNestedScroll(target: View, type: Int)

open Boolean
requestChildRectangleOnScreen(child: View!, rectangle: Rect!, immediate: Boolean)

open Unit

open Boolean
onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int)

open Boolean
onStartNestedScroll(child: View, target: View, axes: Int, type: Int)

open Unit

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

open Unit
setVisibility(visibility: Int)

open Unit
onNestedScrollAccepted(child: View, target: View, nestedScrollAxes: Int)

open Unit
onNestedScrollAccepted(child: View, target: View, nestedScrollAxes: Int, type: Int)

open CoordinatorLayout.LayoutParams!

open MutableList<View!>
getDependents(@NonNull child: View)

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

open Unit

open Boolean
isPointInChildBounds(@NonNull child: View, x: Int, y: Int)

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

open Unit
onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int)

open Unit
onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int)

open Unit
onNestedScroll(@NonNull target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, @NonNull consumed: IntArray)

open Boolean
onNestedPreFling(target: View, velocityX: Float, velocityY: Float)

open Boolean

open Unit

open Unit
onMeasureChild(child: View!, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int)

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

Protected methods
open Unit

open Boolean

open Boolean

open Parcelable!

open Boolean
drawChild(canvas: Canvas!, child: View!, drawingTime: Long)

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open CoordinatorLayout.LayoutParams!

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Int

open CoordinatorLayout.LayoutParams!

open Unit

open Int

Public constructors

<init>

CoordinatorLayout(@NonNull context: Context)

<init>

CoordinatorLayout(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

CoordinatorLayout(@NonNull context: Context, @Nullable attrs: AttributeSet?, @AttrRes defStyleAttr: Int)

Public methods

onAttachedToWindow

open fun onAttachedToWindow(): Unit

onLayoutChild

open fun onLayoutChild(@NonNull child: View, layoutDirection: Int): Unit

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 View: the resolved layout direction for the CoordinatorLayout, such as ViewCompat#LAYOUT_DIRECTION_LTR or ViewCompat#LAYOUT_DIRECTION_RTL.

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

getNestedScrollAxes

open fun getNestedScrollAxes(): Int

onNestedFling

open fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean

setOnHierarchyChangeListener

open fun setOnHierarchyChangeListener(onHierarchyChangeListener: OnHierarchyChangeListener!): Unit

getDependencies

@NonNull open fun getDependencies(@NonNull child: View): MutableList<View!>

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.
Return
MutableList<View!>: the list of views which child depends on.

getStatusBarBackground

@Nullable open fun getStatusBarBackground(): Drawable?

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

Return
Drawable?: The status bar background drawable, or null if none set

dispatchDependentViewsChanged

open fun dispatchDependentViewsChanged(@NonNull view: View): Unit

Allows the caller to manually dispatch Behavior#onDependentViewChanged(CoordinatorLayout, View, View) to the associated 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.

setStatusBarBackground

open fun setStatusBarBackground(@Nullable bg: Drawable?): Unit

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

setFitsSystemWindows

open fun setFitsSystemWindows(fitSystemWindows: Boolean): Unit

onNestedPreScroll

open fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray): Unit

onNestedPreScroll

open fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int): Unit

doViewsOverlap

open fun doViewsOverlap(@NonNull first: View, @NonNull second: View): Boolean

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
Return
Boolean: true if both views are visible and overlap each other

setStatusBarBackgroundResource

open fun setStatusBarBackgroundResource(@DrawableRes resId: Int): Unit

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

onStopNestedScroll

open fun onStopNestedScroll(target: View): Unit

onStopNestedScroll

open fun onStopNestedScroll(target: View, type: Int): Unit

requestChildRectangleOnScreen

open fun requestChildRectangleOnScreen(child: View!, rectangle: Rect!, immediate: Boolean): Boolean

onDetachedFromWindow

open fun onDetachedFromWindow(): Unit

onStartNestedScroll

open fun onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int): Boolean

onStartNestedScroll

open fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean

setStatusBarBackgroundColor

open fun setStatusBarBackgroundColor(color: Int): Unit

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.

setVisibility

open fun setVisibility(visibility: Int): Unit

onNestedScrollAccepted

open fun onNestedScrollAccepted(child: View, target: View, nestedScrollAxes: Int): Unit

onNestedScrollAccepted

open fun onNestedScrollAccepted(child: View, target: View, nestedScrollAxes: Int, type: Int): Unit

generateLayoutParams

open fun generateLayoutParams(attrs: AttributeSet!): CoordinatorLayout.LayoutParams!

getDependents

@NonNull open fun getDependents(@NonNull child: View): MutableList<View!>

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.
Return
MutableList<View!>: the list of views which depend on child.

requestDisallowInterceptTouchEvent

open fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean): Unit

isPointInChildBounds

open fun isPointInChildBounds(@NonNull child: View, x: Int, y: Int): Boolean

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 View: X coordinate to test, in the CoordinatorLayout's coordinate system
y View: Y coordinate to test, in the CoordinatorLayout's coordinate system
Return
Boolean: true if the point is within the child view's bounds, false otherwise

onNestedScroll

open fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int): Unit

onNestedScroll

open fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int): Unit

onNestedScroll

open fun onNestedScroll(@NonNull target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, @NonNull consumed: IntArray): Unit

onNestedPreFling

open fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean

onInterceptTouchEvent

open fun onInterceptTouchEvent(ev: MotionEvent!): Boolean

onDraw

open fun onDraw(c: Canvas!): Unit

onMeasureChild

open fun onMeasureChild(child: View!, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int): Unit

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 View!: the width requirements for this view
widthUsed View!: extra space that has been used up by the parent horizontally (possibly by other children of the parent)
parentHeightMeasureSpec View!: the height requirements for this view
heightUsed View!: extra space that has been used up by the parent vertically (possibly by other children of the parent)

Protected methods

drawableStateChanged

protected open fun drawableStateChanged(): Unit

verifyDrawable

protected open fun verifyDrawable(who: Drawable!): Boolean

checkLayoutParams

protected open fun checkLayoutParams(p: LayoutParams!): Boolean

onSaveInstanceState

protected open fun onSaveInstanceState(): Parcelable!

drawChild

protected open fun drawChild(canvas: Canvas!, child: View!, drawingTime: Long): Boolean

onLayout

protected open fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int): Unit

generateLayoutParams

protected open fun generateLayoutParams(p: LayoutParams!): CoordinatorLayout.LayoutParams!

onMeasure

protected open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

getSuggestedMinimumHeight

protected open fun getSuggestedMinimumHeight(): Int

generateDefaultLayoutParams

protected open fun generateDefaultLayoutParams(): CoordinatorLayout.LayoutParams!

onRestoreInstanceState

protected open fun onRestoreInstanceState(state: Parcelable!): Unit

getSuggestedMinimumWidth

protected open fun getSuggestedMinimumWidth(): Int