No resource found that matches the given name attr "colorPrimary"

49,840

Solution 1

This is what worked for my project.

In res/values-v21/styles.xml,

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/primaryColor</item>
    </style>
</resources>

In res/values/styles.xml,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
    </style>
</resources>

Solution 2

You should follow the convention directly from the Material Design documentation(https://developer.android.com/training/material/theme.html#ColorPalette):

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>

What you are missing here is the android: namespace prefix to the colorPrimary item. Because of this, it cannot find the respective attribute as it's not defined in the scope.

Otherwise you would need to remove the android: prefix from the theme

parent="@android:style/Theme.Material.Light.DarkActionBar"

Solution 3

In my case, the colors used in styles.xml where not defined. So,make sure u have defined all the colors as well as names properly.

Share:
49,840
Boris
Author by

Boris

Updated on July 09, 2022

Comments

  • Boris
    Boris almost 2 years

    I'm having a hard time compiling my Android App in Xamarin Studio. The error that comes up is as follows:

    No resource found that matches the given name attr "colorPrimary"

    Which refers to my styles.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
            <item name="colorPrimary">@color/colorPrimary</item>
            <!--item name="colorPrimaryDark">@color/colorPrimaryDark</item-->
            <!--item name="colorAccent">@color/colorAccent</item-->
        </style>
    </resources>
    

    The usual advice found online is to set the SDK version to 21 or higher. But I already tried that:

    • Set minSdkVersion to 21 in manifest
    • Set targetSdkVersion to 21 in manifest
    • Set taget framework to Android 5.0 in project settings
    • Cleaned and rebuilt project

    The error is still there :-( Are there other settings required to make this work?

  • Boris
    Boris over 7 years
    Nope? I'm referring to a color from my colors.xml
  • Mulflar
    Mulflar over 7 years
    Edited on how to declare and call the color
  • Boris
    Boris over 7 years
    That's exactly what I did. Even your first suggestion produces the same error... The problem is not that the color is not found, but the attribute colorPrimary.
  • Mulflar
    Mulflar over 7 years
    try parent="Theme.Material.Light.DarkActionBar" instead of parent="@android:style/Theme.Material.Light.DarkActionBar"
  • MMAdams
    MMAdams over 6 years
    OP didn't want to simply hard code values, they wanted to read colorPrimary from their colors.xml, and derive the other two colors from that.
  • Vikas Yadav
    Vikas Yadav over 6 years
    Suggest some generic way to solve the problem please.