Reputation: 7853
according to the google docs, I should be able to set the color of Toolbar background using colorPrimary in the theme, but it's not working. Here's 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>
activity layout:
<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 have set my app theme to AppTheme in the manifest: android:theme="@style/AppTheme" >
I have setup android support appcompat in build.gradle
compile 'com.android.support:appcompat-v7:22.1.0'
But my toolbar, is still not colored. I know I can manually set the toolbar background color manually in the layout file, but shouldn't it get its color from the theme? as you can see the accent colors are working.
Upvotes: 9
Views: 14057
Reputation: 365048
Style and theme are different.
The style is local to the Toolbar view, for example the background color.
The theme is instead global to all ui elements inflated in the Toolbar, for example the color of the title and icons.
More info here.
With Material Components Library:
the android:background
attribute in the layout:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
apply the default style: style="@style/Widget.MaterialComponents.Toolbar.Primary"
or customize the style inheriting from it:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
override the default color using the android:theme
attribute:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/MyThemeOverlay_Toolbar"
with:
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="android:textColorPrimary">....</item>
<item name="colorPrimary">@color/.....
<item name="colorOnPrimary">@color/....</item>
</style>
With design support library:
<android.support.v7.widget.Toolbar
style="@style/HeaderBar"/>
where:
<style name="HeaderBar">
<item name="android:background">?colorPrimary</item>
</style>
Otherwise you can define the background for your Toolbar
.
<android.support.v7.widget.Toolbar
android:background="?attr/colorPrimary">
Upvotes: 7
Reputation: 9047
You have to use android:
prefixed attributes in the theme for android 5+, as the non-prefixed variant is only for the app compat parts 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 the v21 styles you define your theme as following:
<!-- 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 others here said before, you should define your own style for the toolbar, but for both variations:
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"
Like this the background color and other styles can be set on all versions and is still changeable by the colorPrimary
that is set in the theme(s).
Upvotes: 4
Reputation: 3070
Toolbar will not get primary color from your theme. You have to set the following xml property of the toolbar
android:background="@color/primary"
This is my working implementation of toolbar.
<?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 it works for you too.
Upvotes: 7
Reputation: 27545
Try to set background color to toolbar
This 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>
Upvotes: 0