Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Toolbar background color programmatically does not change Toolbar Title Background color

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>
like image 425
geekkoz Avatar asked Apr 06 '15 01:04

geekkoz


People also ask

How do I change the toolbar background color in android programmatically?

getSherlockActivity(). getSupportActionBar(). setBackgroundDrawable(new ColorDrawable(0xff00ACED)); To change the color of my action bar in a fragment and it works.

How can change action bar background color and title color in Android?

Just go to res/values/styles.edit the xml file to change the color of action bar.

How do you change the color of the title bar on Android?

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


3 Answers

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

like image 90
reVerse Avatar answered Oct 16 '22 06:10

reVerse


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.

like image 39
Igor Tyulkanov Avatar answered Oct 16 '22 04:10

Igor Tyulkanov


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 
            }
        }
    }
}
like image 25
daduck Avatar answered Oct 16 '22 05:10

daduck