Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar subtitle not appearing

I set up my Toolbar within a collapsingtoolbarlayout like so:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"/>


    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

However, when I attempt to set the title and subtitle, only the title appears in the toolbar!

private void setupToolbar(){
    toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
    if(toolbar != null){
        setSupportActionBar(toolbar);
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(mTitle);
    getSupportActionBar().setSubtitle("Subtitle);
}

How do I access the toolbar's subtitle?

like image 429
waylonion Avatar asked Sep 25 '15 15:09

waylonion


1 Answers

This is a CollapsingToolbarLayout bug.

Read http://saulmm.github.io/mastering-coordinator and visit some libraries:

https://github.com/anton46/WhatsApp-ProfileCollapsingToolbar,

https://github.com/hendraanggrian/collapsingtoolbarlayout-subtitle,

https://github.com/ahmadmuzakki/subtitle-collapsingtoolbar

https://github.com/k0shk0sh/CoordinatorLayoutExample or any others you like.

Also you can read my answer about shadows in Android 4.4: https://stackoverflow.com/a/48526318/2914140.

UPDATE

Currently I can see deleted answers. Maybe it will help somebody. Copy-paste from source (@q126y).

This happens because the toolbar has not been rendered when the call to setSubtitle is made.

Call the code like this.

mToolbar.post(new Runnable() {
        @Override
        public void run() {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(mTitle);
            getSupportActionBar().setSubtitle("Subtitle");
        }
    });
like image 184
CoolMind Avatar answered Sep 28 '22 07:09

CoolMind