Material design proposed interesting animation features, that can be seen throughout it’s design guidelines. One of these features is called Flexible Space Header, and today we’re going to take a closer look on how to implement it.

To get the visual feeling of what we’re about to implement, check out this video.

The code

We’ve prepared the demo code at : https://github.com/SnowdogApps/Material-Flexible-Space-Header You can import it to Android Studio and give it a try straight away. The code works with Appcompat library, so it’s fine to run it on any 4.x+ device.

Note that we use excellent ObservableScrollView library by Ksoichiro. It provides handy ways for observing the scroll events in scrollable views.

First, we need to create XML layout file

It’s structure allows us to divide the UI in 2 sections – Flexible Header Space and place for displaying actual Data. Here’s an excerpt from the XML, to give you an idea of what split sections:

  

To see the full source code, follow this link.

It’s pretty straight-forward implementation.

Second, we create ObservableScrollViewWithFling class

It allows us to track the fling events while view is being scrolled. Processing the fling started event gives us the right place to show back the toolbar. Without intercepting fling event, we didn’t have a chance to show / hide toolbar while scrollview was in the fling state. Lucky us the solution is quite simple, and works like a charm. The class can be found here.

Third, it’s time to create Fragment that will make use of our layout and ObservableScrollViewWithFling.

The fragment itself can be found here.

What’s worth additional comment is the way we move our Flexible space. Couple of notes:

  1. We determine if toolbar needs to be shown based on the scrollY position.
  2. If it’s visible and folded (Flexible Space is not visible), we use mToolbarViews background.
  3. If it’s unfolded, so the Flexible Space is visible, mToolbarViews background is transparent, and we change the llTintLayers background color instead (up to the point where it matches toolbar heights).
  4. Lastly we translate and scale the Flexible Space Text.

Ending note

If you go trough the code available at github, you should be easily able to implement this solution in your own app. In case you need additional informations or help, feel free to ask in the comments below.