How to change toolbar color

I have a search on how to customize a toolbar and, for example, add a background color, but I don’t understand how it works.

I am trying to add my own style to my toolbar, but any result ...

Manifesto

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

File style.xml

 <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 the layout

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

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")); 

Maybe because my toolbar is in android.support.design.widget.CoordinatorLayout (to host android.support.design.widget.FloatingActionButton)?

+7
source

You can set the background in 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" /> 
+2
source

In fact, there is a developer of Android developers that details how to colorize a toolbar with colorPrimary .

You were definitely on the right track by adding colorPrimary to your topic. You need 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 that if you have a dark colorPrimary and light theme, you also need to add android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" so that the text and icons are white on a dark background.

+1
source

Use this

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

Try using

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

in your styles. This is the same name of the tag with the background of the window, but changes the background color of the toolbar only when you use it with your toolbar styles.

0
source

All Articles