Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 927
Rockwell Avatar asked Jul 08 '15 19:07

Rockwell


People also ask

How do you add a title to a collapsing toolbar?

By calling setTitleEnabled(false); , the title appeared in the toolbar. Save this answer. Show activity on this post. It is the same as setting title in a normal toolbar.

How do I change the collapsing toolbar title color?

xml file. In method 1 Just go to the activity_main. xml file and add a TextView in the toolbar widget with the text color attribute.

What is collapsing toolbar layout?

Android CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout.


1 Answers

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

like image 143
doubleA Avatar answered Sep 28 '22 07:09

doubleA