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?
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");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With