Search code examples
javaandroidandroid-toolbar

Correct way to change Collapseable Toolbar title


When I change title of collapsed collapsingToolbar, the title doesn't change.

I've tried getSupportActionBar.setTitle and collapseToolbar.setTitle, but it didn't help. Tell me, what's the problem ?


Solution

  • I believe that this issue describes what you are experiencing. I also had this issue and solved it today. Essentially the code that handles the collapsing text only update the text when the current text is null or the size of the text changes. Currently this is a bug that is closed and the patch is scheduled for a future release of the design library. For now use my workaround of just changing the size of the text and then changing it back.

    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>
    

    EDIT: The below code is from the collapsingtext helper that is used inside the collapsing toolbar layout to control the text of that view.

            if(availableWidth > 0.0F) {
                updateDrawText = this.mCurrentTextSize != newTextSize;
                this.mCurrentTextSize = newTextSize;
            }
    
            if(this.mTextToDraw == null || updateDrawText) {
                this.mTextPaint.setTextSize(this.mCurrentTextSize);
                CharSequence title = TextUtils.ellipsize(this.mText, this.mTextPaint, availableWidth, TruncateAt.END);
                if(this.mTextToDraw == null || !this.mTextToDraw.equals(title)) {
                    this.mTextToDraw = title;
                }
    
                this.mTextWidth = this.mTextPaint.measureText(this.mTextToDraw, 0, this.mTextToDraw.length());
            }
    

    the offending lines are updateDrawText = this.mCurrentTextSize != newTextSize; which sets the boolean to determine if we change the text in this line if(this.mTextToDraw == null || updateDrawText) { So when the collapsing toolbar layout recalculates its view the determining factor to set the text is the textsize. If you did not change the text size then your collapsing toolbar layout title will not change untill it is collapsed or expanded from collapsed position