0

I'm trying to learn Android, now I'm trying to change color in my android project. Here is what I do,

I change my color.xml to:

<resources>
    <color name="primaryColor">#d81b60</color>
    <color name="primaryLightColor">#ff5c8d</color>
    <color name="primaryDarkColor">#a00037</color>
    <color name="primaryTextColor">#ffffff</color>
</resources>

and my style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="primaryColor">@color/primaryColor</item>
    <item name="primaryLightColor">@color/primaryLightColor</item>
    <item name="primaryDarkColor">@color/primaryDarkColor</item>
    <item name="primaryTextColor">@color/primaryTextColor</item>
</style>

But when I try to run I get this error(s):

enter image description here

So, how can I fix it?

I already try this:

  1. Android Studio does not find color style resource

  2. Android Studio can't find color reference within the same resource file

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
YVS1102
  • 2,658
  • 5
  • 34
  • 63

3 Answers3

0
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

in style these tags are used to specify the colorPrimary accent and primaryDark

to be more specific change the style xml as

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorAccent">@color/primaryAcentColor</item>
    </style>
Manoj kumar
  • 450
  • 4
  • 13
0

Here you go..

You need to create attrs.xml under values folder.

Mention all your reference colors inside this file.

e.g.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="primaryColor"></attr>
    <attr name="primaryLightColor"></attr>
    <attr name="primaryDarkColor"></attr>
    <attr name="primaryTextColor"></attr>
</resources>

Now you can directly use your colors in style.xml

e.g.

<resources xmlns:android="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="primaryColor">@color/primaryColor</item>
        <item name="primaryLightColor">@color/primaryLightColor</item>
        <item name="primaryDarkColor">@color/primaryDarkColor</item>
        <item name="primaryTextColor">@color/primaryTextColor</item>
    </style>

</resources>
Flutterian
  • 1,761
  • 1
  • 21
  • 47
0

You are trying to define non-exist attribute in your style:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="primaryColor">@color/primaryColor</item>
    <item name="primaryLightColor">@color/primaryLightColor</item>
    <item name="primaryDarkColor">@color/primaryDarkColor</item>
    <item name="primaryTextColor">@color/primaryTextColor</item>
</style>

There are no primaryColor, primaryLightColor, primaryDarkColor, and primaryTextColor. The right attributes are colorPrimary, colorPrimaryDark, colorPrimaryLight, and textColorPrimary. This image will show you the attributes (from Using the material theme):

enter image description here

So, you need to change it to the following:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryLight">@color/primaryLightColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="textColorPrimary">@color/primaryTextColor</item>
</style>
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96