Michael
Michael

Reputation: 9824

Reference Resource @values/color/my_color.xml from android:color

I want to change the color of my custom pressed state for different version of android.

Example: v19 = white, v18 and lower = cyan blue

How do I reference @values/color/my_color as my button color.

Referencing default Android color values would be awesome, but I don't know how to do it :/

Here is my code

button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true" >
    <shape>
        <solid 
            android:color="@values/color/button_selection" /> //ERROR: SAYS RESOURCE DOESN'T EXIST, BUT IT DOES
        <corners
            android:bottomRightRadius="3dp"
            android:bottomLeftRadius="3dp" />
    </shape>
</item>
<item 
    android:state_pressed="false" >
    <shape>
        <corners
            android:bottomRightRadius="3dp"
            android:bottomLeftRadius="3dp" />
    </shape>
</item>
</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="button_selection">#ffffff</color>
</resources>

If this is a duplicate, please point me to it.

Thank you

Upvotes: 0

Views: 2209

Answers (2)

alvarovmz
alvarovmz

Reputation: 93

You should place your colors.xml in res/values/ folder. Then you can reference it from your layouts like @color/<attribute_name>.

Example (res/values/colors.xml):

<resources>
    <color name="custom_black">#000000</color>
</resources>

Reference:

<TextView
    android:textColor="@color/custom_black" 
/>

Upvotes: 2

Michael
Michael

Reputation: 9824

In short - Remove @values from the reference

  1. Place color.xml in the values folder of res

  2. Reference android:color="@color/my_custom_color".

Upvotes: 0

Related Questions