Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?attr/ not setting right color in Toolbar

I am using the same code from http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html for my project.

When I created a blank project to only test the Toolbar, the color is working as it should. However, after upgrading my project to material design using the same code, the Toolbar color becomes grey.

It seems android:background?attr/colorPrimary isn't loading the right color. When I use @color\theme_red, the color is properly set on the Toolbar.

What is going wrong here?

My colors.xml:

<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="theme_red">#d43d1e</color>
    <color name="theme_red_dark">#aa3118</color>
    <color name="theme_accent">#3333ff</color>
</resources>

& styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/theme_red</item>
    <item name="colorPrimaryDark">@color/theme_red_dark</item>
    <item name="colorAccent">@color/theme_accent</item>
</style>

& code for toolbar:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"    />

How the toolbar looks like with android:background?attr/colorPrimary: How the toolbar looks like How the toolbar looks like with @color/theme_red:

How it should be

UPDATE: I am updating the question as now I have another bug in the code which seems to be related. I tried the app on Android 5.0.2 phone & the statusbar did not tint with the darker shade even with correct theme & colors defined in the styles.xml. The statusbar has exactly same color as the Toolbar in the first image.

The styles.xml is above. The v-21/styles.xml is below:

 <resources>
  <style name="AppTheme" parent="AppTheme.Base">
  <item name="colorPrimary">@color/theme_red</item>
  <item name="colorPrimaryDark">@color/theme_red_dark</item>
  <item name="colorAccent">@color/theme_accent</item>
  <item name="android:windowContentTransitions">true</item>
  <item name="android:windowAllowEnterTransitionOverlap">true</item>
  <item name="android:windowAllowReturnTransitionOverlap">true</item>
  <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
  <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

Somehow, the colors are not being available to the system even after being defined properly.

The full project is available at:https://github.com/pauldmps/BPUTApp-AndroidStudio if anybody wants to take a look at the whole code.

like image 844
Shantanu Paul Avatar asked Mar 11 '15 16:03

Shantanu Paul


1 Answers

You've specified the wrong theme in your manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

You should be using android:theme="@style/AppTheme".

like image 86
Vikram Avatar answered Oct 14 '22 10:10

Vikram