tylercomp
tylercomp

Reputation: 132

How do you check for someone touching the screen and dragging in a specific direction?

I want to check for someone placing their finger on the center of the screen and dragging to the right, how could this be accomplished?

SOLUTION: http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

Upvotes: 2

Views: 145

Answers (2)

xil3
xil3

Reputation: 16439

You could implement the onTouchListener and do something like this:

public boolean onTouch(View view, MotionEvent event) {
  int currentX = event.getX();    

  if(event.getAction() == MotionEvent.ACTION_MOVE) {
    // oldX would be defined as a private property of the class (most likely an Activity)
    if(currentX > oldX) {
      // moving right

      oldX = currentX;
    } else {
      // moving left or not moving at all

      oldX = currentX;
    }
  }

  return true;
}

You can probably play around with that and make it work the way you want.

Upvotes: 1

Nathan Schwermann
Nathan Schwermann

Reputation: 31503

Look into the GestureDetector class

Upvotes: 0

Related Questions