Change Toolbar background color programmatically does not change Toolbar Title Background color
Asked Answered
M

5

26

I´m trying to change the toolbar Background color programmatically by doing this:

getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));

And this is the result:

before:

enter image description here

After:

enter image description here

Some how the toolbar title still has the same background color as before.

here is my toolbar xml:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/Theme.Toolbar">

And here is the Theme:

<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
    <item name="android:background">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@android:color/white</item>
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
</style>
Malamut answered 6/4, 2015 at 1:7 Comment(3)
Have you applied any custom style for actionbar in styles.xml, may be some of them is conflicting.Fusil
post your layout xml pleaseFestivity
@DavidJhons i have posted new details on the questionsMalamut
D
14

Change your code as follows:

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/MyToolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

Theme / Style

<style name="MyToolbarStyle">
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
    <item name="android:background">@color/primary</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
    <!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
         this should go to the AppTheme -->
</style>

Result

Before setting the new background color:

picture before background color change

and after:

picture after background color change

Depersonalize answered 9/4, 2015 at 8:2 Comment(2)
Where did you put colorPrimary/PrimaryDark etc i.e. where do you place your 'AppTheme'? Also, is there any particular reason you're not setting a parent theme?Tate
It's basically just a custom style and I don't want to inherit any attributes from an other theme thus I'm not specifying a parent theme. Regarding colorPrimary: That's set in the AppTheme which is used by the application / activities. By default the Toolbar sets the background depending on the colorPrimary attribute unless you don't override it with a style.Depersonalize
L
7

Late, but I hope it would be helpful comment

I had this item in my Activity style

<item name="android:background">someColor</item>

so when I changed toolbar color, title and menu items didn't change background. I just removed this item and now it works perfect.

I did not have time to understand the details, but I think it might be useful to someone else.

Lyonnaise answered 20/2, 2016 at 4:26 Comment(1)
I have found that removing this does fix the toolbar. However, how can you set the background colour without breading the toolbar?Bugaboo
M
4

use this to access the textview

public void changeToggleTitle() {
    if (mToolbar != null) {
        for(int i= 0; i < mToolbar.getChildCount(); i++){
            View v = mToolbar.getChildAt(i);
            if(v != null && v instanceof TextView){
                TextView t = (TextView) v;
                // Do the magic 
            }
        }
    }
}
Mezcaline answered 6/4, 2015 at 15:9 Comment(0)
B
1

In my style I changed

        <item name="android:background">@color/background</item>

to

        <item name="android:windowBackground">@color/background</item>

and that seems to have fixed it for me.

Bugaboo answered 18/6, 2020 at 10:53 Comment(0)
H
-1
mToolbar.setBackgroundResource(mGameEnum.getPrimeColorRes());
Hesper answered 24/3, 2017 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.