AppBar / CollapsingToolbarLayout / Toolbar

AppBar – Part 1

The AppBar is an evolution of the ActionBar which provides us with a rich toolbox for defining and customising the behaviour. All apps will have different requirements for their AppBars and each app may have different AppBar implementations in different Activities. In this series, rather than look at the AppBar implementations themselves, we’re going to explore how can integrate and animate different elements of the AppBar to provide smooth UI and UX which implement material design style animations and transitions.

Let’s first consider the basic AppBar model that we’re looking to achieve. The main body content will be a ViewPager enabling the user to swipe between sections. In the AppBar we want a TabBar which will also control the ViewPager. Also in the AppBar we want an image which is specific to the current section of the ViewPager which changes as the use changes sections. We want the image to disappear, leaving the TabBar, when the user scrolls up so that it is not obscuring the main content. This will provide a nice balance of interesting, content-driven UI which can be removed to provide more access to the actual content.

That may sound rather complex, but hopefully it will become clear as we progress through the series.

I’ve created the basic project from an Android Studio Navigation Drawer Activity template project. I’ve tidied up and refactored the code a little, but I won’t go in to it in detail here as it is not what we’re interested in – it just felt appropriate to have a nav drawer in place so that we have the appropriate hamburger menu on the Toolbar. We’re also using the design support library (for material goodness, and many of the components we’ll be using). This has dependencies on both appcompat-v7 and support-v4 libraries, and the latter will give us the ViewPager implementation.

Let’s begin by looking at our main Activity layout:

The highlighted section is the bit we’re interested in – it is the ViewPager, and the main AppBar layout wrapped inside a CoordinatorLayout. We’ve looked at CoordinatorLayout before on Styling Android and its purpose in this case will become apparent as we progress.

Now let’s take a look at the AppBar layout. We used an include in the main layout because, in a real project, we would, most likely, want to re-use the same AppBar across multiple Activities and an inclusion here makes that much easier.

So, this is pretty simple – we have our Toolbar, then our ImageView, and then the TabBar. One thing worth of note is the use of the fitSystemWindows attribute – this is to allow the image to display behind the status bar when the Toolbar is expanded.

We wire this all up in our Activity using a simple PagerAdapter (it’s in the source so I won’t bother listing it here):

If we run this we get our basic AppBar layout:


OK, that’s a nice start, but the AppBar takes up most of the screen and the actual content area of the ViewPager is pretty small – even though it’s got nothing in it, in a real app this would be the main content. So what we want to do is collapse the image when the user scrolls up. This is the point where the relevance of the CoordinatorLayout should become apparent – it will marshall the scrolling across multiple components and enable us to collapse the Toolbar when we scroll. So all we need to do is update our AppBar layout to implement this behaviour:

There are a couple of things worth pointing out, here. Firstly we’ve added the CollapsingToolbarLayout which is providing the specific behaviour required based upon scrolling events being passed on by the CoordinatorLayout. The exact behaviour is what we declare in the app:layout_scrollFlags attribute. The CollapsingToolbarLayout enables us to define a contentScrim colour which is what will be used to blend with the status bar when the Toolbar is collapsed, and also offsets for the title text when the Toolbar is expanded.

Secondly, we have swapped the order of the Toolbar and the ImageView. This is to enable us to move the title text with the expanding and collapsing toolbar.

Thirdly, we have defined different collapseMode values for the Toolbar and ImageView to control that the ImageView will do a parallax collapse and the Toolbar (i.e. the title text) will remain pinned even when collapsed.

Let’s see this in action:

That demonstrates the power of CollapsingToolbarLayout – just adding some simple wrapping and a couple of attributes to define the behaviour that we want and we get something which already looks pretty good. However, there’s much more that we can do and in the next article we’ll look at changing the feature image when the user moves between ViewPager sections.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved.

Copyright © 2015 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at


  1. This article is great. However, does the appbar collapse when you scroll up from viewpager? I follow your instruction but my appbar doesn’t collapse when I scroll up the listView inside the viewPager

    1. If you use a standard ListView I don’t think it will work correctly because your scrolling container needs to implement NestedScrollingChild to work correctly with CoordinatorLayout. You’ll need to subclass ListView to implement NestedScrollingChild to get it to work correctly.RecyclerView should work out of the box, though.

      1. I used recycleView to accomplished that but I need to make a cursorAdapter like for my recycleView. I never knew that I can implement NestedScrollingChild to do so. Thanks for your reply!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.