Collapsing Toolbar Layout Example

CollapsingToolbarLayout is provided to help in creating toolbars that follow material design. It is a wrapper for tool bar adding additional features to toolbar in the coordinator layout and app bar layout context.

Collapsing toolbar layout should be used as a child of app bar layout. To make functionality provided by app bar layout work, app bar layout needs to be part of coordinator layout. Collapsing toolbar layout, app bar layout and coordinator layout framework is provided in design support library.

To use these layouts in your project, you need to include design support library by adding below dependency in your gradle build file.

compile 'com.android.support:design:25.0.1'

For more information about coordinator layout and app bar layout, please read coordinator layout and app bar layout related articles.

How to use collapsing toolbar layout

Collapsing toolbar layout allows you to customize toolbar in app bar layout meaning depending on positional changes applied to tool bar by app bar layout and coordinator layout, collapsing toolbar layout lets you configure toolbar, changing tool bar UI experience.

Collapsing toolbar layout output

android collapsing toolbar layout example

Collapsing toolbar layout code

Layout xml








 package com.zoftino.materialdesign;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class CollapsingToolbarActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        Toolbar myToolbar = (Toolbar) findViewById(R.id.z_toolbar);


        CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        ctl.setTitle("Best Coupons Deals");

        TextView tv =(TextView)findViewById(R.id.coupons_lst);

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.z_menu, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.latest_coupons:
                Toast.makeText(this,"latest coupons action clicked", Toast.LENGTH_LONG).show();
                return true;

            case R.id.top_stores:
                Toast.makeText(this,"top stores action clicked", Toast.LENGTH_LONG).show();
                return true;

            case R.id.action_settings:
                Toast.makeText(this," action clicked", Toast.LENGTH_LONG).show();
                return true;

                return super.onOptionsItemSelected(item);


Collapsing toolbar layout title configuration

android collapsing toolbar layout title

As seen above, collapsing toolbar layout title can be set using setTitle method. You can apply different text styles for collapsing and expanding toolbar.

CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
ctl.setTitle("Best Coupons Deals");



Parallax scrolling and pinned position childern

Attribute layout_collapseMode controls how child views of collapsing toolbar layout move when layout is moving. If layout_collapseMode of a view is set to parallax, it moves in parallax fashion. When layout_collapseMode attribute is set to pin, view is placed in a fixed position. Below screens show toolbar when layout_collapseMode is set to pin and without the layout_collapseMode setting.

android collapsing toolbar layout pin position