Toolbar subtitle not appearing
Asked Answered
S

2

21

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?

Scrimmage answered 25/9, 2015 at 15:18 Comment(5)
Not sure https://mcmap.net/q/661474/-android-material-with-extended-toolbarTenedos
Probably this answer can help you.Elephant
Anybody found the solution yet?Sandi
@Dr.aNdRO see my answer.Strategy
not working :( I don't know what is happening maybe some BugSandi
P
2

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://mcmap.net/q/137959/-add-elevation-shadow-on-toolbar-for-pre-lollipop-devices.

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");
        }
    });
Pronounced answered 22/12, 2017 at 14:35 Comment(0)
B
0

You can do this in layout xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:subtitle="ANDROID">
    <!-- sub title for the Toolbar-->
</android.support.v7.widget.Toolbar>

Or you can do programmatically:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // get the reference of Toolbar
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar4
toolbar.setSubtitle("ANDROID"); // setting a sub title for this Toolbar
getSupportActionBar().setDisplayShowTitleEnabled(false); // hide the current title from the Toolbar
Bowfin answered 13/3, 2018 at 11:10 Comment(1)
This will only work if you are not using CollapsingToolbarLayoutNelson

© 2022 - 2024 — McMap. All rights reserved.