CollapsingToolbarLayout setTitle() does not update unless collapsed
I

3

26

With the new Design Library, we're supposed to set the toolbar title on the CollapsingToolbarLayout, not the Toolbar itself(at least when using the collapsing toolbar). But setTitle() only updates the title in the following specific circumstances:

  1. When the CollapsingToolbarLayout does not have a title yet

  2. At the moment the CollapsingToolbarLayout becomes fully collapsed

  3. At the moment the CollapsingToolbarLayout starts to expand

What I'm actually trying to do is make the title become an EditText when fully expanded, allowing the user to give his/her character a name, which then displays as the title. I've tried to force the issue by calling invalidate() or requestLayout(), as well as both of those methods on CollapsingToolbarLayout's children. No effect.

Intyre answered 6/6, 2015 at 11:54 Comment(2)
I reported this as a bug, have no idea how to force it in the meanwhile code.google.com/p/android/issues/…Koressa
Chris Banes marked this bug for FutureRelease: code.google.com/p/android/issues/detail?id=175808Ringleader
G
27

EDIT: This solution is no longer needed. bug fixed in v22.2.1

I didnt want to just leave links so here is the full solution.

The bug occurs because the code to handle the collapsable title only updates the actual title if the current title is null or the text size has changed. The workaround is to change the title text size and then change it back. I used 0.5 sp so there was not too much of a jump. Changing the text size forces the text to be updated and there is no flicker. just a slight text size change.

This is what I have

private void setCollapsingToolbarLayoutTitle(String title) {
    mCollapsingToolbarLayout.setTitle(title);
    mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
    mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
    mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBarPlus1);
    mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBarPlus1);
}

in styles.xml I have

<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">28sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="ExpandedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">28.5sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="CollapsedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">24.5sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>
Greasy answered 9/7, 2015 at 6:5 Comment(10)
No need for this workaround anymore. It´s fixed in v22.2.1 Please @Greasy update the answerWellesz
I am using v23.0.1 and title is not showing up when I put the toolbar inside the CollapsingToolbarLayoutHaines
@NeonWarge Your issue sounds like a different problem. You should start a new question and try to get help from there. My solution works for those using pre v22.2.1 If you are using v23.0.1 then this particular issue does not affect you. Good luck and Happy Coding.Greasy
Yeah it works now, I stayed at build version 23.0.1 and use support library 23.1.1.Haines
Just using mCollapsingToolbarLayout.setTitle() fixed it for me, when using all toolbar.setTitle() methods did not update a set title.Katti
@Katti if you are using v22.2.1 or above then this should not be an issue as it has been fixed. Glad it works. Happy Coding. :)Greasy
I am experiencing this problem in v24.0.2Haines
Yes it looks like the problem has regressed. According to @marioosh it is working in 24.1.1Greasy
the problem stills in 24.2.1Rishi
Yes many people have reported that this problem has been popping up again. @Rishi i am interested to know if the fix still works? if not then it is not really the same problem even if it has the same symptoms.Greasy
S
5

Okay I have a workaround while we wait for Google:

  1. Grab the gist from https://gist.githubusercontent.com/blipinsk/3f8fb37209de6d3eea99/raw/b13bd20ebb319d94399f0e2a0bedbff4c044356a/ControllableAppBarLayout.java (I'm not the original creator but kudos to original author). This adds a few methods to the AppBarLayout, namely expand and collapse

  2. In your method that calls setTitle():

collapsingToolbar.setTitle("All Recent");
getSupportActionBar().setTitle("All Recent");
collapseThenExpand();
  1. Now create a collapseThenExpand() method:

private void collapseThenExpand() {
  appbar.collapseToolbar();

  Handler h = new Handler();
  h.postDelayed(new Runnable() {
    @Override
    public void run() {
      appbar.expandToolbar(true);
    }
  }, 800);
}

Note you can turn off the expand animation by setting it to false.

Slyke answered 25/6, 2015 at 0:41 Comment(2)
For anyone using this, it does work in forcing a refresh of the title, but this: collapsingToolbarLayout.setTitle("new title"); appbar.collapseToolbar(false); appbar.expandToolbar(false); does NOT work. There needs to be some time between collapsing an expanding- even a delay of 1ms works. Unfortunately this causes a flicker.Intyre
The @Greasy solution is dirty but more effective, no flicker and easy to do. I'm using it.Wellesz
A
0

In my solution I had to set the title for both the toolbar and the collapsing toolbar for it to work.

So in OnCreate:

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

    collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);

And then further down when I switch fragments I set the title for both when a tab is selected:

            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                //TAB1 - THE DEFAULT TAB
                switch (item.getItemId()) {

                        case R.id.tab_rooms:
                            toolbar.setTitle("My Title");
                            collapsingToolbar.setTitle("My Title");
                            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                            fragment = new RoomsFragment();
                            transaction.replace(R.id.fragment_container, fragment);
                            transaction.addToBackStack(null);
                            transaction.commit();

                            return true;

                        case R.id.tab_shisha:
                            toolbar.setTitle("My Title2");
                            collapsingToolbar.setTitle("My Title2");
                            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                            fragment = new ShishaFragment();
                            transaction.replace(R.id.fragment_container, fragment);
                            transaction.addToBackStack(null);
                            transaction.commit();
                            return true;
                          }

                    return false;
                }
Alphosis answered 26/11, 2016 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.