Question: I have set up a GridView that has TextViews, and I would like to change the background of the text view when it is touch, then upon release reset the background. The grid view already has OnItemClick and OnItemLongClick listeners. I have implemented the background swap for the touched text view by setting a OnTouch listener for the GridView, then I get the position of the touch and use `getChildAt` to get the text view that was touched. This works really well, until I scroll the grid view. Because the text views are reused, `getChildAt` is returning the wrong text view, so I'm changing the background of the wrong text view in that situation.

How can I always get the correct text view, or is there a way to set an OnTouch listener for each text view instead of trying to get a text view after touching the grid view?

```
    gridView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float currentXPosition = event.getX();
            float currentYPosition = event.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);

            if (gridView.getChildAt(position) != null) {
                TextView txtView = (TextView)gridView.getChildAt(position);
                //might not be the right text view!
                ...

```

Answer: 
You can use `getFirstVisiblePosition` to retrieve the offset for the first child

```
int firstPosition = gridView.getFirstVisiblePosition();
int childPosition = position - firstPosition;
TextView txtView = (TextView)gridView.getChildAt(childPosition);

```

This should give you the correct position. Of course, you should check whether `childPosition` is actually a valid position (i.e. not `<0` or `>=gridView.getChildCount()`)
I suggest you should take a look at Android's list selectors. You don't even need listeners for this task. List selectors make customizing touch feedback a piece of cake ;)

There are lots of good answers and questions about list selectors on Stack Overflow, for example: [Custom selector for list background](https://stackoverflow.com/questions/18386035/custom-selector-for-list-background)

But if you really are going to ignore my advice...

Then I suggest you to check the MotionEvent for being of the type ACTION\_DOWN or ACTION\_UP instead of processing every little bit of touch input.

Example code:

```
int action = event.getActionMasked();

if(action == MotionEvent.ACTION_DOWN){
    //get child and change background to pressed state
} else if(action == MotionEvent.ACTION_UP){
    //get child and change background to normal state
}

```