1

I have a TextView whose background I would like to change when clicked on. I've tried following the many examples of this I've found on this site and others, but I can't seem to get it working properly.

I'm completely new to android development, so it's quite possible that I'm missing something obvious, so please bear with me. I'm also trying to do this through XML as opposed to Java.

Main.xml (contained within a table layout)

<TextView
            android:id="@+id/character_option1"
            android:text="Option1" 
            android:textColor="@color/text_off"
            android:background="@drawable/selectors"
            android:clickable="true"
            android:padding="5dp" />

selectors.xml (in 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:color="@drawable/option_on">
</item>
<item
    android:color="@drawable/option_off" >
</item>
</selector>

colors.xml (in res/values)

<drawable name="option_on">#EBEBEB</drawable>
<drawable name="option_off">#CC0000</drawable>

I've also tried android:color="@color/option_off" in selectors.xml with color name="option_off" in res/values. And I tried simply putting android:color="#CC0000" right into selectors.xml. None of it worked.

I'm not sure it's receiving any information from the selector in general. I set the default color to red #CC0000 just to check, but the background color is still the style's default white.

--

These are the most recent links I have tried as well:

Selector on background color of TextView

Changing Textcolor and Background of a TextView

Change Background Color of TextView on Click

Community
  • 1
  • 1
V_Iker
  • 13
  • 1
  • 4

3 Answers3

3

When specifying a drawable for a background

android:background="@drawable/selectors"

the item selectors in selectors.xml requires you to have the drawable attribute set.

You're setting the color attribute which won't work.

Instead change color to 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="@drawable/option_on">
    </item>
    <item
        android:drawable="@drawable/option_off" >
    </item>
</selector>

Here's a reference. Refer to the item tag section.

android:drawable
   Drawable resource. Required. Reference to a drawable resource.

singularhum
  • 5,072
  • 2
  • 24
  • 32
  • Thank you for your reply. I've made the edit, but nothing seems to have changed. Even the "off state" color is incorrect. Would there be any issues with it being contained in a ``/``? – V_Iker Apr 26 '14 at 16:12
  • @V_Iker It should work fine in there. Are you sure you rebuilt your project after you made the changes and are not testing on an older version of your app? Works okay for me. – singularhum Apr 26 '14 at 16:39
  • Ok. So I pushed it to Git and it does indeed work as you said. I use Aide when I'm on the go and even running it through there wasn't working correctly. So it seems I've wasted some hours more than needed. Live and learn, as they say. Thanks again. – V_Iker Apr 26 '14 at 18:05
  • @V_Iker Ah I see, glad you got it working. No problem! – singularhum Apr 26 '14 at 18:18
0

try @color/yourcolor to get and <color name="white">#FFFFFF</color> to set colors in colors.xml (in res/values)

reference

Community
  • 1
  • 1
Bills
  • 768
  • 7
  • 19
  • Thanks for taking the time to reply. Seems it was my error by not checking it through Eclipse. – V_Iker Apr 26 '14 at 18:08
0

To change View background color on click

in res/drawable/my_onclick_background.xml:

<?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/colorPrimary">
</item>
<item
    android:drawable="@color/colorAccent" >
</item>

then in res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?><resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>

Put in your View's xml

android:background="@drawable/my_onclick_background"

And you are done.

CodeToLife
  • 3,672
  • 2
  • 41
  • 29