CoordinatorLayout

CoordinatorLayout allows to specify the coordinate dependencies between child views. But most signifant feature is abilty to provide interaction behavior plugin for child views. Without this CoordinatorLayout can be considered as powerful FrameLayout.

There are widgets of material design like AppBarLayout are designed special to be as direct child of CoordinatorLayout.

Such widgets have a built-in behavior plugin implementation. For example, FAB changes its visibility depending on the visibility of the AppBarLayout, when anchored to it.

CoordinatorLayout defines additional xml properties for its children:

  • app:layout_anchor - specifies id of the view that the current view depends on
  • app:layout_anchorGravity - specifies how the current view depends
  • app:layout_behavior - specifies behavior for child. It can be class name, or referrence to built-in behaviour like string/appbar_scrolling_view_behavior.

scrolling activity

Scrolling activity layout

A vertical scrolling activity is one of typical usage of CoordinatorLayout. You can generate it in Android Studio

File->New->Activity->Scrolling Activity

Main highlights.

NestedScrollView has app:layout_behavior = "@string/appbar_scrolling_view_behavior", which checks is the content scrolling allowed, or whether appbar should collapse or expand.

You can replace NestedScrollView by other modern view like RecyclerView. Some views like ScrollView is not working as expected.

Nested scrolling can originate not only on direct children of a CoordinatorLayout, but on any child View. For example, ViewPager with RecyclerView for pages also will work as expected. Just remember to assign the behavior in the app: layout_behavior property for ViewPager.

AppBarLayout is extension of LinearLayout with vertical orientation. You can assign different values of app:layout_scrollFlags for children. Possible values are:

  • scroll - view will be scroll in direct relation to scroll events
  • snap - upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it’s closest edge
  • enterAlways - when entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling.
  • enterAlwaysCollapsed - an additional flag for enterAlways which modifies the returning view to only initially scroll back to it’s collapsed height.
  • exitUntilCollapsed - when exiting (scrolling off screen) the view will be scrolled until it is "collapsed"

CollapsingToolbarLayout is a wrapper Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout. One interesting feature is that the views can be scrolled within this layout in a parallax fashion (usually ImageView).

Paralax effect example

behaviour

A Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

There are some built-in behaviours. For example, SwipeDismissBehavior provides support for the 'swipe-to-dismiss' gesture.

SwipeDismissBehavior example

You can implement own behaviour for the specified view.

Custom behaviour example

You can assign behaviour programmatically

val customBehaviour = CustomBehaviour()
yourView.getLayoutParams().behavior = customBehaviour

You can assign behaviour in the app:layout_behavior xml property

<FrameLayout
  android:layout_height=”wrap_content”
  android:layout_width=”match_parent”
  app:layout_behavior=”.CustomBehaviour” />