Reputation: 13
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
Upvotes: 1
Views: 6436
Reputation: 4141
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.
Upvotes: 0
Reputation: 798
try @color/yourcolor
to get and <color name="white">#FFFFFF</color>
to set colors in colors.xml (in res/values)
Upvotes: 0
Reputation: 5122
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.
Upvotes: 3