Android CollapsingToolbarLayout subtitle

5.3k views Asked by At

is it possible to set a subtitle to the CollapsingToolbarLayout like it is possible with a normal toolbar? As far as I know there is no method to do that programmatically. Also, how do I set the white back arrow to the toolbar? Using

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

does not display anything, also adding

app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"

to the toolbar doesn't display it either:

2

There are 2 answers

0
bkurzius On

For the subtitle question, I had a similar issue and posted the code I used to fix it here:

https://stackoverflow.com/a/31529101/834692

Hopefully you might also find this useful.

For the back icon, you need to set the toolbar first and then call setDisplayHomeAsUpEnabled():

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_white);
0
Hendra Anggrian On

There are some ways to have subtitle in a CollapsingToolbarLayout.

  1. Have a TextView inside your CollapsingToolbarLayout, but then only title would be your Toolbar's title when CollapsingToolbarLayout is collapsed, no third-party library is needed.
  2. Use my library: collapsingtoolbarlayout-subtitle.

CollapsingToolbarLayout.

Use it like you would on any CollapsingToolbarLayout, and add subtitle attribute to it:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout
            android:id="@+id/subtitlecollapsingtoolbarlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:subtitle="CollapsingToolbarLayout"
            app:title="Subtitle">

            <!-- collapsing toolbar content goes here -->

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:layout_collapseMode="pin"/>
        </com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <!-- content goes here -->

</android.support.design.widget.CoordinatorLayout>

Oh and about that back arrow on Toolbar, I usually set it by using ?homeAsUpIndicator in xml:

<android.support.v7.widget.Toolbar
    ...
    app:navigationIcon="?homeAsUpIndicator"/>

Hope it helps.