Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 298
learner Avatar asked Oct 24 '25 18:10

learner


2 Answers

Here's a solution I found.

You just need to create a "color" folder in your res and save the file in the newly created folder.

After completing this step you will be able to get the color selector file in your attribute of respective xml file.

I'm attaching screenshots for better understanding.

Step 1

Step 2

like image 99
Vishal Shah Avatar answered Oct 26 '25 08:10

Vishal Shah


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>
like image 32
murielK Avatar answered Oct 26 '25 08:10

murielK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!