Toolbar inside CollapsingToolbarLayout, Toolbar title not showing
Asked Answered
V

4

15

I used CollapsingToolbarLayout as the parent of Toolbar, below it the layout

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/test_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"
            app:theme="@style/ThemeOverlay.AppCompat.Light" />

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

Then I want to set the title of the Toolbar with the following code, but it didn't work. The title just didn't show.

    Toolbar toolbar = (Toolbar) findViewById(R.id.test_toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("ABC");

I also tried set the title in CollapsingToolbarLayout with the following code, it didn't work either.

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
    collapsingToolbarLayout.setTitleEnabled(true);
    collapsingToolbarLayout.setTitle("ABC");

But if I removed CollapsingToolbarLayout from my layout and make AppBarLayout as the direct parent of Toolbar, the code above to set the title of Toolbar worked.

Did I missed something? This issue is so weird. Is it a bug in design support library? How can I solve it without changing my layout?

Vitrine answered 25/8, 2015 at 15:14 Comment(3)
Try android:fitsSystemWindows="true" on your CoordinatorLayout, AppBarLayout and CollapsingToolbarLayout.Mllly
@FarbodSalamat-Zadeh Thanks for your reply. It didn't work after I tried as you said.Vitrine
any updates on this? Facing the same problem here!!Hadley
V
5

This is a temporary solution. I didn't investigate the code deeply, but by disabling the refresh of Toolbar in CollapsingToolbarLayout, it worked.
Here is what I did:

public static void setRefreshToolbarEnable(CollapsingToolbarLayout collapsingToolbarLayout,
                                           boolean refreshToolbarEnable) {
    try {
        Field field = CollapsingToolbarLayout.class.getDeclaredField("mRefreshToolbar");
        field.setAccessible(true);
        field.setBoolean(collapsingToolbarLayout, refreshToolbarEnable);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
Vitrine answered 6/9, 2015 at 5:7 Comment(3)
Did you find a permanent solution for this? I am using this currently but I don't know if this reliableParabasis
Not sure why but this is working, good enough so far I can't really find another way of doing it.Feliciafeliciano
This is suppose to work! Very good. Actually there bugs filed already: code.google.com/p/android/issues/… I find this answer related to the answers on these posts: #32074312 | #30683048Prophylactic
C
3

Simply set Collapsing toolbar 'titleEnabled=false' in xml Then Toolbar title will show up.

    app:titleEnabled="false"
Chatelain answered 7/10, 2017 at 17:38 Comment(0)
T
0

try this :

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("ABC");
Telescopic answered 7/11, 2017 at 8:46 Comment(0)
L
-1

Move

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
collapsingToolbarLayout.setTitleEnabled(true);
collapsingToolbarLayout.setTitle("ABC");

from onCreate to onCreateView inside if

Logsdon answered 22/3, 2016 at 2:42 Comment(1)
why both of you has got a same name?Ballista

© 2022 - 2024 — McMap. All rights reserved.