views:

239

answers:

1

How do I set up a TextView to flash when it is clicked? With flashing I mean that I want to change the background color of the TextView. I essentially want one of the objects that is displayed in a ListActivity, but inside a normal View.

I have tried to do this by adding an OnClickListener, but what I really need is something like adding an On(Un)SelectListener. Using the onClickListener, I can change the TextView background, but obviously the background stays that color. I thought of using a new Handler().postDelayed(new Runnable(){ ... }) kind of thing to reset the backround after some small time, but I did not know if this would be overkill for what I'm trying to do.

What would you recommend?

+1  A: 

Normally this kind of thing is achieved by having a <selector> drawable. For example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />
</selector>

The selector arbitrates between other drawables based on the state of the view it is in. You would put the above xml in an file in res/drawable/ and then use it as background for your view. You also need to have the normal and pressed drawables.

You can also create the selector-drawable in code where it is called StateListDrawable.

But perhaps your solution is simpler...

hermo