0

I have a ScrollView with a series of linearlayout textviews. When the user "touches" one of those textviews, I am temporarily changing the background color to "show" the touch so they can visually confirm the item they touched. When they lift their finger, I am changing the background color back to the default color.

I was having a problem that if they user scrolled, the ACTION_UP didn't seem to fire so that textview's background never changed back to default.

I thought I fixed it by adding an ACTION_MOVE to my TouchListener and that seemed to correct the problem, better, but not always.

My code looks like this...

mytextview.setOnTouchListener(mytouch);

along with...

private View.OnTouchListener mytouch = new View.OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.setBackgroundColor(getResources().getColor(R.color.mytouchcolor));
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            v.setBackgroundColor(getResources().getColor(R.color.mydefaultcolor));
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            v.setBackgroundColor(getResources().getColor(R.color.mydefaultcolor));
        }
        return false;
    }
}

This code WORKS if I...

  • touch and release
  • touch, slowly scroll, then release.

If I touch and quick scroll (or flick), the ACTION_UP or ACTION_MOVE never seems to fire (according to some LOG.I() tests.

How can I fix this so that the background color ALWAYS turns back to default once the touch is lifted... scrolling or not?

Peter
  • 427
  • 4
  • 14

1 Answers1

1

There is a simple way:

in layout:

<TextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/view_selector"
        />

view_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@mipmap/view_press"/>
<item android:drawable="@mipmap/view_default"/>

</selector>
S-MILE-S
  • 114
  • 6