Android toolbar color is not set by color.

according to google docs, I should be able to set the toolbar background color using colorPrimary in the theme, but it does not work. Here is what I have:

styles.xml:

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/light_purple</item> <!-- colorPrimaryDark is used for the status bar --> <item name="colorPrimaryDark">@color/dark_purple</item> <!-- colorAccent is used as the default value for colorControlActivated, which is used to tint widgets --> <item name="colorAccent">@color/dark_purple</item> <item name="colorSwitchThumbNormal">@color/light_purple</item> </style> </resources> 

action location:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:theme="@style/AppTheme" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/pivot_title_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="toolbar text view" /> </android.support.v7.widget.Toolbar> ... </LinearLayout> 

Activity:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } 

I installed the application theme in AppTheme in the manifest: android:theme="@style/AppTheme" > I have appcommpat support for Android support in the build.gradle file

 compile 'com.android.support:appcompat-v7:22.1.0' 

But my toolbar is still not painted. I know that I can manually set the background color of the toolbar manually in the layout file , but should it not get its color from the theme? as you can see accent colors work.

enter image description here

+5
source share
4 answers

The toolbar will not get the primary color from your theme. You must set the following toolbar xml property

 android:background="@color/primary" 

This is my working toolbar implementation.

  <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary" android:minHeight="?actionBarSize"> </android.support.v7.widget.Toolbar> 

Hope this works for you too.

+4
source

You can apply style to Toolbar

The style and theme are different.

The style is local on the toolbar, for example, the background color.

A theme instead of a global one for all ui elements inflated in the Toolbar, for example, the color of the name and icons.

More details here .

 <android.support.v7.widget.Toolbar style="@style/HeaderBar"/> 

Where:

 <style name="HeaderBar"> <item name="android:background">?colorPrimary</item> </style> 

Otherwise, you can define a background for your toolbar.

 <android.support.v7.widget.Toolbar android:background="?attr/colorPrimary"> 
+5
source

You should use the android: prefixed attributes in the theme for android 5+, since the option without a prefix is ​​intended only for parts that support the application, for versions <android 5.

So, you should have one values/styles.xml for pre-android 5 and one values-v21/styles.xml for android 5 +.

In v21 styles, you define your theme as follows:

 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:colorPrimary">@color/light_purple</item> <item name="android:colorPrimaryDark">@color/dark_purple</item> <item name="android:colorAccent">@color/dark_purple</item> </style> 

As you now define the colorPrimary attribute once simply as colorPrimary for pre-Lollipop and once as android:colorPrimary for Lollipop devices, you can no longer directly use ?attr/colorPrimary . Instead, like the others mentioned earlier, you should define your own style for the toolbar, but for both options:

values ​​/ styles.xml:

 <style name="Toolbar"> <item name="android:background">?attr/colorPrimary</item> </style> 

values-V21 / styles.xml:

 <style name="Toolbar"> <item name="android:background">?android:attr/colorPrimary</item> </style> 

And use the style for your toolbar:

 <android.support.v7.widget.Toolbar style="@style/Toolbar" 

Thus, the background color and other styles can be set on all versions and can still be changed using colorPrimary , which is set in the theme.

+4
source

Try setting the background color on the toolbar.

It worked for me

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_main" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:background="@color/primary_color" android:gravity="right" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <ProgressBar android:id="@+id/pb_loading_news" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:visibility="gone" /> </android.support.v7.widget.Toolbar> 
0
source

All Articles