От халепа... Ця сторінка ще не має українського перекладу, але ми вже над цим працюємо!


Hi! This website uses cookies. By continuing to browse or by clicking “I agree”, you accept this use. For more information, please see our Privacy Policy


How to build Collapsing Toolbar with MotionLayout


Dmytro Ruban


Android Developer

4 min read

4 min read

Creating a Collapsing Toolbar can be easily considered an essential part of an engaging design for Android application development. So how can we build it?

Well, there are two ways to do it.

  1. The most common: using a CollapsingToolbarLayout inside a CoordinatorLayout to achieve this exact behavior. And it’s totally okay if you already have it implemented and running on your app. Don’t mess up with what’s already working, as they say.
  2. An extra one: using MotionLayout, which provides excellent flexibility and allows achieving exciting results almost effortlessly when developing android applications.

So, in this article, I’ll show you how to create an Android Collapsing Toolbar with MotionLayout.

Andoid app development

Article content:

  1. A couple of words about MotionLayout
  2. Applying MotionLayout for a Collapsing Toolbar: the process
  3. Behind the scene
  4. To wrap up

Disclaimer: For continuing to read the article, just make sure that you’re familiar with MotionLayout and ConstraintLayout basics.

A couple of words about MotionLayout

Basically, MotionLayout is a new class of the ConstraintLayout 2.0 library, which was announced back at Google IO 2018. Since then, it’s already been released to a stable beta version, so it’s become widely available and popular for android applications development.

Fundamentally, the tool helps Android app developers to handle motion and widget animation in their applications. As Nicolas Roard, one of the Google developers working on MotionLayout puts it:

in terms of capabilities, it’s like a mix between the property animation framework, TransitionManager, and CoordinatorLayout.

MotionLayout is primarily used in mobile application development when animating the UI elements a user will interact with. The second but not less critical purpose of applying the tool is to help users understand what your app actually does.

Now that we have a general meaning of MotionLayout and its possibilities, we should deal with the flesh implementation of a Collapsing Toolbar Android apps usually have.

Applying MotionLayout for a Collapsing Toolbar: the process

The building process has two main parts. Let’s tap a toe into each of them step by step.

Layouts part

First, let’s see how our layout should look like:


    // This is our scrollable content: RecyclerView or NestedScrollView, etc.


            // Inside MotionLayout, we will place our collapsing toolbar content



Now we have to dive deeper into each component in our tree.

For proper content scrolling `app:layout_behavior=”@string/appbar_scrolling_view_behavior` must be set on the scrolling sibling (e.g., `NestedScrollView`, `RecyclerView`, etc.).


AppBarLayout must have a fixed height of an expanded state. Otherwise, MotionLayout behaves unpredictably. Please, keep this in mind as this is important.



The next thing to do is to set `layout_height` to `match_parent` to keep MotionLayout a fixed height.

Attribute `minHeight` represents our collapsed toolbar height. We have to set it to system action bar size just to follow the same size to all collapsed toolbars across the app. However, you can set up any size you want.

Now, let’s proceed further. `layoutDescription` is a simple MotionScene with a start and end `ConstraintSets.`

Please note that this part doesn’t require any special activities. We don’t need to add any transitions triggers because the animation will be triggered programmatically after setting `layout_scrollFlags` the same as with the implementation of `CollapsingToolbarLayout.`



The biggest trick here is a proper transition between the expanded and collapsed state of our toolbar content.

To make it smooth and non-lagging, we need to define two guidelines: one for a collapsed state and another one for expanded.

The first guideline will be a guideline for insets. It must be constrained to begin. Thus, all content of the expanded toolbar must be constrained to it like to the top of the parent.

Consequently, we align all content from the start `ConstraintSet` to `insets_guideline` like to the top of the parent.

As we need setup insets in runtime, to have a look at a proper render in preview, let’s set `tools:layout_constraintGuide_begin=”24dp”.

     tools:layout_constraintGuide_begin="24dp" />

Just as `insets_guideline` works as the top for the expanded state, our second guideline will serve as the bottom for the collapsed state and should be constrained to the end of the parent.

     tools:layout_constraintGuide_end="?attr/actionBarSize" />

Behind the scenes

Now we have to connect `motionLayout` with `appBarLayout` to trigger animation through `OnOffsetChangedListener`.

Every time the Collapsing Toolbar should change its collapsed progress, it will inform us by just converting `verticalOffset` to a state of animation between 0f and 1f. It will allow us to inform MotionLayout about the new progress.

This action will be called every time the user scrolls an app.

    AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
        val seekPosition = -verticalOffset / appBarLayout.totalScrollRange.toFloat()
        binding.motionLayout.progress = seekPosition

And finally, we set up guidelines with insets.

// Desired collapsed height of toolbar
val toolBarHeight = binding.motionLayout.minimumHeight
ViewCompat.setOnApplyWindowInsetsListener(requireView()) { v, insets ->
    // Resizing motion layout in a collapsed state with needed insets to not overlap    system bars
    val insetHeight = insets.systemWindowInsetTop
    binding.motionLayout.minimumHeight = toolBarHeight + insetHeight

    // Update guidelines with givens insets
    val startConstraintSet = binding.motionLayout.getConstraintSet(
    startConstraintSet.setGuidelineBegin(, insetHeight)
    val endConstraintSet = binding.motionLayout.getConstraintSet(
    endConstraintSet.setGuidelineBegin(, insetHeight)
    endConstraintSet.setGuidelineEnd(, toolBarHeight )

motionlayout collapsing toolbar

To wrap up

Well, as we see, MotionLayout is pretty good at handling a Collapsing Toolbar.

There are a few more reasons to include MotionLayout into your animations.

  1. It allows animating any layout property, making any interaction between a user and your app even more engaged.
  2. MotionLayout empowers you to customize transitions to cover a wide range of UI needs.
  3. Its declarative nature, which means that any of the transitions can be described in XML, regardless of their complexity, simplifies building code-less animations.

All those above significantly enhance graphical tooling in Android Studio, as the core MotionLayout developers state.

Well, I hope that my thoughts and experiments were somehow helpful for you, and now you have a clear perception of how to build a Collapsing Toolbar with MotionLayout for your app.

Anyway, if you have any questions regarding this matter or any other topic in mobile app development for Android, please feel free to contact NERDZ LAB as your trustworthy Android app development company.