13
votes

I've been searching how to customize the toolbar, for example how to add background color, but I don't understand how it works.

I've been trying to add a custom style for my toolbar but any result ...

The Manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Design">

The style.xml file

<resources>

    <style name="Theme.Design" parent="Base.Theme.Design">
    </style>

    <style name="Base.Theme.Design" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryDark">@color/red</item>
        <item name="colorAccent">@color/red</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
    </style>    
    ...

And the toolbar in layout

<android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
6

6 Answers

23
votes

Thanks, but any solution works.

 <android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

or

toolbar.setBackgroundColor(Color.parseColor("#80000000"));

May be because my toolbar is in android.support.design.widget.CoordinatorLayout (to put a android.support.design.widget.FloatingActionButton) ?

7
votes

In fact, there was a Android Developers pro-tip which go into details on how to color the Toolbar using colorPrimary.

You were definitely on the right track, adding colorPrimary to your theme. What you need is to set the background on the Toolbar:

<android.support.v7.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

Note, if you have a dark colorPrimary and a light theme, you'll need to also add android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" to ensure the text and icons are white over the dark background.

3
votes

set Color from resource file

toolbar.setBackgroundColor(getResources().getColor(R.color.red));
1
votes

You can set the background in the xml.

<android.support.v7.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
/>
1
votes

Use this

toolbar.setBackgroundColor((Color.parseColor("#80000000")));
1
votes

Try to use

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

in your styles. It's the same tag name with window background, but changes the toolbar background color only when you use it with your toolbar styles.