16

My problem is a weird one (I think).

Using AppCompat my references to ?attr/colorPrimary are not working.

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">@color/primary_material_dark</color>
    <color name="colorPrimaryDark">@color/primary_dark_material_dark</color>
    <color name="test">#ff2800</color>
</resources>

styles.xml:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/primary_material_dark</item>
        <item name="android:colorPrimaryDark">@color/primary_dark_material_dark</item>
    </style>
</resources>

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context=".MainActivity"
          android:background="?attr/colorPrimary">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

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

        />
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary">

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@string/hello_world"/>
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="COLORCLOR"
        android:background="?attr/colorPrimary"/>
    </LinearLayout>


</LinearLayout>

Heres what it looks like:

?attr/colorPrimary

But everything seems to work fine when I replace the ?attr/colorPrimary references with actual color refrences. Really confused about this, tried removing the theme and popupTheme attributes from toolbar, still didn't work.

PS. ?attr/colorPrimaryDark works just fine

user3690467
  • 3,049
  • 6
  • 27
  • 54

6 Answers6

13

Remove android tag from your style to make you able to use the material design theme:

<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_material_dark</item>
    <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
</style>
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • I am facing the same issue but removing android: worked for me.The amusing thing is that in my one project its working with android: and in another its allowing to put android: :( – Gufran Khurshid Jan 06 '16 at 10:09
  • I also had to [set the app theme in the manifest file](http://stackoverflow.com/a/39341616/3681880). – Suragch Sep 06 '16 at 06:08
5

Add implementation 'com.android.support:design:26.1.0' in build.gradle(Module:app) in section dependencies {}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // If I  commented I have your error
    implementation 'com.android.support:design:26.1.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Fortran
  • 2,218
  • 2
  • 27
  • 33
4

There is an issue on Pre-Lollipop.You can change

android:background="?attr/colorPrimary"

instead of

app:background="?attr/colorPrimary"

It will work fine.

piyushj
  • 1,546
  • 5
  • 21
  • 29
Nitin Karande
  • 1,280
  • 14
  • 33
1

Try in toolbar

android:background="?android:attr/colorPrimary"

and styles:

<item name="colorPrimary">@color/primary</item>
Juan Labrador
  • 1,204
  • 10
  • 14
  • 1
    Wouldn't this just reference the attr from the device's/OS's theme, not the app's theme, rendering your style item useless? – Maxr1998 Nov 06 '15 at 22:31
  • There's no such resource as `?android:attr/*` - this doesn't work. – k2col Nov 26 '17 at 18:58
1

i face the same problem then i find the solutuion you need to remove this line

<item name="android:statusBarColor">@android:color/transparent</item>

It's overriding the colorPrimaryDark color and basically removing it from the status bar from the style.xml file

0

Update to the latest libraries, in gradle. Click on the red light bulb to update.

implementation 'com.android.support:design:27.1.1'

If you are using 27.1.1, make sure all your other libraries are also using 27.1.1.

live-love
  • 48,840
  • 22
  • 240
  • 204