tags:

views:

60

answers:

4

Is it possible to change the text color of a textview if the textview is pressed? I want to achieve a flashing effect with the color change only lasting as long as the button is pressed.

I know how to change the background of the textview with a selector list and the correct state but how can I change the color of text if the user pushes a button or a simple textview?

A: 
myTextView.setTextColor( 0xFFFF0000 )
jeremynealbrown
this won't have the desired effect I want to get a flashing change while the user presses and change the color back at the moment he releases the textview
Janusz
i'd suggest adding that requirement to your original question
jeremynealbrown
you are right. Edited my question, sorry for the confusion
Janusz
+1  A: 

you can change it using the setTextColor(ColorStateList) method

myTextView.setTextColor(myColorStates);
Ryan Conrad
Looks good but I like to do all my layout with XML
Janusz
+1  A: 

search for color selector to use in the

android:setTexColor

attr

weakwire
A: 

You can define a selector for colors as well. A short example that only distinguishes between pressed and all other states is:

<?xml version="1.0" encoding="utf-8"?>
<selector
   xmlns:android="http://schemas.android.com/apk/res/android"&gt;
   <item
      android:state_pressed="true"
      android:color="#FFFFFF" />
   <item
      android:color="#4C566C" />
</selector>

For a full documentation on the selector see this unofficial documentation.

Put every color selector in a single file and put this files in a directory called color in the resources folder of your project.

Janusz
this only works for text color. If you want to use it for a background color you need to define a color drawable in each item.
Janusz