Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle touch or press down events in Android?

Tags:

java

android

Alright so i have 4 image views..

How can i control what happens when they are pressed down and up

So say like:

one of the images is press down a textview gets changed to 1 but when they let go of that one the text will change back to 0?

like image 447
Hailei Edwards Avatar asked Oct 04 '11 04:10

Hailei Edwards


1 Answers

You need to use an OnTouchListener and have it listen for TouchEvents on your Views.

For Example:

MyTouchListener l = new MyTouchListener();
view.setOnTouchListener(l);

Here is an example of what MyTouchListener could look like:

class MyOnTouchListener implements OnTouchListener {
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // touch down code
                break;

            case MotionEvent.ACTION_MOVE:
                // touch move code
                break;

            case MotionEvent.ACTION_UP:
                // touch up code
                break;
        }
        return true;
    }
}
like image 148
slayton Avatar answered Nov 05 '22 15:11

slayton