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?

alijandro
  • 11,627
  • 2
  • 58
  • 74

4 Answers4

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();
    }
}
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • 1
    Did you find a permanent solution for this? I am using this currently but I don't know if this reliable – Bhargav Dec 21 '15 at 13:40
  • Not sure why but this is working, good enough so far I can't really find another way of doing it. – Roberto Mar 01 '16 at 15:37
  • This is suppose to work! Very good. Actually there bugs filed already: https://code.google.com/p/android/issues/detail?can=2&q=183333&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&id=183333 I find this answer related to the answers on these posts: http://stackoverflow.com/questions/32073812/collapsingtoolbarlayout-settitle-not-working-anymore | http://stackoverflow.com/questions/30682548/collapsingtoolbarlayout-settitle-does-not-update-unless-collapsed – Neon Warge Mar 05 '16 at 05:56
3

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

    app:titleEnabled="false"
Junaid Ahmed
  • 144
  • 2
  • 4
0

try this :

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("ABC");
Kevin
  • 23
  • 3
-1

Move

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

from onCreate to onCreateView inside if