afellowcoder afellowcoder - 16 days ago 6
Java Question

Removing toolbar subtitle

How can we properly remove the toolbar's subtitle and put the title back in it's place?

Doing this

getSupportActionBar().setSubtitle(null);
only removes the text but the title remains in the top, this image represents the behaviour.

I've also tried this as an attempt to refresh it, but didn't work aswell:

getSupportActionBar().setSubtitle(null);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);


XML code:

<android.support.v7.widget.Toolbar
android:id="@+id/settingsToolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:paddingTop="@dimen/toolbar_padding"
android:minHeight="@dimen/toolbar_min_height"
android:background="@color/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:animateLayoutChanges="true"/>


Setting up the toolbar:

Toolbar toolbar = (Toolbar)findViewById(R.id.settingsToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Settings");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


This is not a duplicate of this question since the accepted answer does not solve it.

Answer

The problem is in android:animateLayoutChanges="true"

If you remove that it will work but it won't have the pretty animation, right now I've settled for this:

public void changeSubtitle(@Nullable String subtitle) {

    if (subtitle == null) {
        toolbar.setLayoutTransition(null);
        getSupportActionBar().setSubtitle(null);
    } else {
        toolbar.setLayoutTransition(new LayoutTransition());
        getSupportActionBar().setSubtitle(subtitle);
    }
}

It animates when adding the subtitle, but doesn't when removing.