14
votes

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?

7
do you have created a colorPrimary value in your color xml file?Luca Ziegler
Did you try android:colorPrimary?K Neeraj Lal
Ha, that did the trick :-)Boris
Please share the solution ^_^Mulflar
Sure, see my answer :)K Neeraj Lal

7 Answers

14
votes

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>
7
votes

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"
2
votes

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.

0
votes

In my case it were wrong entries in the SDK from an external partner i use:

  • they had style information in the SDK value.xml file
  • they also hat an android:theme part in their AndroidManifest.xml

which both are obviously not nessesary when you build an SDK (and not an app, where you need config about the themes and colors...)

0
votes

The major issue people will face this because they forgot to add android before defining the colors And The defined colors not exists in your colors xml

<item name="android:colorPrimary">#fff</item> <item name="android:colorPrimaryDark">#fff</item> <item name="android:colorAccent">#fff</item>

Here I have defined colors directly if you use @color/color_primary like this the mentioned color_primary must exist in your color file.

-2
votes

What you are doing is declare that the colorPrimary of your style is the colorPrimary of your style... What does not have sense.

You should declare wich color is:

<style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="colorPrimary">#6c0e54</item>

If you want to define another colors and the call it on the style create a xml file like:

<?xml version="1.0" encoding="utf-8"?>
   <resources>
     <color name="apptheme_color1">#9D3F86</color>
     <color name="apptheme_color2">#84226C</color>
  </resources>

and then you can do:

 <style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="colorPrimary">@color/apptheme_color1</item>
-3
votes

Add this code in res\values\colors.xml

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

Now sync your project.