Kalpesh Rajai
Kalpesh Rajai

Reputation: 2056

Android MotionEvent to detect only ACTION_MOVE

As per android MotionEvent documents: A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). The motion contains the most recent point, as well as any intermediate points since the last down or move event.

ACTION_MOVE Android doc

so when i apply a setOnTouchListene on my view is perfectly works, It give me ACTION_DOWN, ACTION_UP, and ACTION_MOVE

but my problem is i just want to ignore a ACTION_DOWN events exactly before ACTION_MOVE. Because ACTION_MOVE event occur only after the ACTION_DOWN event as per its documents.

my coding:

      button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        Log.e("Mouse: ", "Click");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.e("Mouse: ", "Move");
                        break;
                }

                return false;
            }
        });

So, there is any why to ignore ACTION_DOWN event. Because user only want to move not want to click, and ACTION_MOVE is also occur ACTION_DOWN before it execute it self.

Thanks...

Upvotes: 3

Views: 3454

Answers (1)

Alexander Tumanin
Alexander Tumanin

Reputation: 1842

According to your comment - you can play with counter. For example:

private static int counter = 0;
...  
button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {


            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    Log.e("Mouse: ", "Click");
                    break;
                case MotionEvent.ACTION_MOVE:
                    counter++; //check how long the button is pressed
                    if(counter>10){
                       Log.e("Mouse: ", "Move");
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    if(counter<10){
                       //this is just onClick, handle it(10 is example, try different numbers)
                    }else{
                       //it's a move
                    }
                    counter = 0;
                    break;
            }

            return false;
        }
    });

Upvotes: 5

Related Questions