# CoordinatorLayout and Behaviors

The CoordinatorLayout is a super-powered FrameLayout and goal of this ViewGroup is to coordinate the views that are inside it.

The main appeal of the CoordinatorLayout is its ability to coordinate the animations and transitions of the views within the XML file itself.

CoordinatorLayout is intended for two primary use cases:

:As a top-level application decor or chrome layout

:As a container for a specific interaction with one or more child views

# Creating a simple Behavior

To create a Behavior just extend the CoordinatorLayout.Behavior (opens new window) class.

# Extend the CoordinatorLayout.Behavior


public class MyBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

       * Default constructor.
      public MyBehavior() {
       * Default constructor for inflating a MyBehavior from layout.
       * @param context The {@link Context}.
       * @param attrs The {@link AttributeSet}.
      public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);

This behavior need to be attached to a child View of a CoordinatorLayout to be called.

# Attach a Behavior programmatically

MyBehavior myBehavior = new MyBehavior();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();

# Attach a Behavior in XML

You can use the layout_behavior attribute to attach the behavior in XML:

  app:layout_behavior=".MyBehavior" />

# Attach a Behavior automatically

If you are working with a custom view you can attach the behavior using the @CoordinatorLayout.DefaultBehavior annotation:

public class MyView extends ..... {


# Using the SwipeDismissBehavior

The SwipeDismissBehavior (opens new window) works on any View and implements the functionality of swipe to dismiss in our layouts with a CoordinatorLayout.

Just use:

       final SwipeDismissBehavior<MyView> swipe = new SwipeDismissBehavior();

        //Sets the swipe direction for this behavior.

        //Set the listener to be used when a dismiss event occurs
            new SwipeDismissBehavior.OnDismissListener() {
            @Override public void onDismiss(View view) {

            public void onDragStateChanged(int state) {

        //Attach the SwipeDismissBehavior to a view
        LayoutParams coordinatorParams = 
            (LayoutParams) mView.getLayoutParams();    

# Create dependencies between Views

You can use the CoordinatorLayout.Behavior (opens new window) to create dependencies between views. You can anchor a View to another View by:

For example, in order to create a Behavior for moving an ImageView when another one is moved (example Toolbar), perform the following steps:

  • [Create the custom Behavior](http://stackoverflow.com/documentation/android/5714/coordinatorlayout-and-behaviors/20239/creating-a-simple-behavior#t=201608310823294520346):
    public class MyBehavior extends CoordinatorLayout.Behavior<ImageView> {...}
  • Override the [`layoutDependsOn`](https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.Behavior.html#layoutDependsOn(android.support.design.widget.CoordinatorLayout,%20V,%20android.view.View)) method returning `true`. This method is called every time a change occurs to the layout:
    public boolean layoutDependsOn(CoordinatorLayout parent, 
            ImageView child, View dependency) {
        // Returns true to add a dependency.
        return dependency instanceof Toolbar;
  • Whenever the method [`layoutDependsOn`](https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.Behavior.html#layoutDependsOn(android.support.design.widget.CoordinatorLayout,%20V,%20android.view.View)) returns `true` the method [`onDependentViewChanged`](https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.Behavior.html#onDependentViewChanged(android.support.design.widget.CoordinatorLayout,%20V,%20android.view.View)) is called:
    public boolean onDependentViewChanged(CoordinatorLayout parent, ImageView child, View dependency) {
        // Implement here animations, translations, or movements; always related to the provided dependency.
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()); 
  • # Remarks

    The CoordinatorLayout (opens new window) is a container that extends the FrameLayout.
    By attaching a CoordinatorLayout.Behavior (opens new window) to a direct child of CoordinatorLayout, you’ll be able to intercept touch events, window insets, measurement, layout, and nested scrolling.

    By specifying Behaviors (opens new window) 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.