Search code examples
androidandroid-layoutselectorandroid-resources

Where do I save selector color on android


Here is my selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@color/black"/>
    <item android:color="@color/white"/>

</selector>

apparently I cannot save it in the /drawable folder as my android won't compile. Then if I create a /color folder in res I get a red mark from eclipse. So where do I save my selector so I can use it as the background color of a TextView (or really any other view).

I have a colors.xml file in values, but how would I add a selector to it?

THE ACTUAL SOLUTION

I am posting this edit in case someone else needs help. I hope you this saves you some time.

For the correct answer, I did following

In strings.xml

<drawable name="black_drawable">@color/black</drawable>
<drawable name="white_drawable">@color/white</drawable>

In colors.xml

<color name="black">#000000</color>
<color name="white">#ffffff</color>

Then in the selector, I did

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/black_drawable" android:state_pressed="true"/>
    <item android:drawable="@drawable/white_drawable"/>

</selector>

The selector is saved in /drawable as selector_black_white.xml


Solution

  • Last edit i messed up pretty in my previous edit sorry my bad this one is working now res/drawable

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true" android:drawable="@color/whiteColor"/>
        <item android:drawable="@color/backColor"/>
    
    </selector>
    

    then on your res/value/string or create new values color

    <color name="whiteColor">#000000</color>
    
    
    
     <color name="backColor">#ffffff</color>