Toolbar title text color
Asked Answered
P

2

10

I have the following layout in my application:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:expanded="false">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/header_image"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

Also application theme is as following

<style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

Where colors are:

<color name="primary">#5677fc</color>
<color name="primary_dark">#303f9f</color>
<color name="accent">#e040fb</color>

To set title I use

CollapsingToolbarLayout collapsingToolbar;
...
collapsingToolbar.setTitle(title);

Title shown black, I what it white. How can I do it?

Both setting app:titleTextColor="@android:color/white" to Toolbar and toolbar.setTitleTextColor(0xffffff); does not help.

Pallette answered 9/5, 2017 at 15:31 Comment(0)
G
7

Try this

Create a style

<style name="collapsingToolbarLayoutTitleColor" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
</style>

Then, add the style on your CollapsingToolbarLayout when it is collapsed and expanded.

mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.collapsingToolbarLayoutTitleColor);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsingToolbarLayoutTitleColor);
Gunpowder answered 9/5, 2017 at 15:58 Comment(1)
Is it possible setExpandedTitleTextAppearance and setCollapsedTitleTextAppearance via application theme, not programmatically?Pallette
N
1

I think you should be using setCollapsedTitleTextColor():

collapsingToolbar.setCollapsedTitleTextColor(0xffffff);

BTW, your header_image might be taking the whole width of the screen and hiding the title as it has (perhaps it should be wrap_content):

android:layout_width="match_parent"
Nihon answered 9/5, 2017 at 15:42 Comment(2)
What OS are you using and what is your compileSdkVersion?Nihon
I tested on Genymotion simulator with Android 4.4.4 and real device with Android 5.1 - both the same result. minSdkVersion = 16, targetSdkVersion = 24.Pallette

© 2022 - 2024 — McMap. All rights reserved.