How to capture finger movement direction in android phone?-ThrowExceptions

Exception or error:

I want to capture the finger movement direction on Android touch phone. If a user slides his finger in up/down/left/right direction, I want to capture this direction. How can I find this? Thanks.

How to solve:

Implement onTouchEvent(), and calculate dx and dy by where the user presses down and lifts up. You can use these values to figure out the direction of the move.

float x1, x2, y1, y2, dx, dy;
String direction;
switch(event.getAction()) {
        x1 = event.getX();
        y1 = event.getY();

    case(MotionEvent.ACTION_UP): {
        x2 = event.getX();
        y2 = event.getY();
        dx = x2-x1;
        dy = y2-y1;

        // Use dx and dy to determine the direction of the move
        if(Math.abs(dx) > Math.abs(dy)) {
              direction = "right";
              direction = "left";
        } else {
              direction = "down";
              direction = "up";


Your best bet would be to deal with the MotionEvent’s you get from a View.OnTouchListener() callback. The motion events keep track of how you are currently interacting with the View by its action property.

I would imagine you can calculate which direction someone slides their finger by examining the action property of the MotionEvents and the x/y values of where the motion event took place.

switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            oldX= event.getX();
            oldY= event.getY();
        case MotionEvent.ACTION_MOVE:
            int newX = motionEvent.getX();
            int newY = motionEvent.getY();

            int deltaX = oldX - newX;
            int deltaY = oldY - newY;

              //Motion in Y direction.
              // Motion in X direction.

There are lots of other method available on the MotionEvent object for use as well:


I think instead of having bunch of variables better to use VelocityTracker

Here is modified example from Developer Android

private var mVelocityTracker: VelocityTracker? = null

override fun onTouchEvent(event: MotionEvent): Boolean {

    when (event.actionMasked) {
        MotionEvent.ACTION_DOWN -> {
            // Reset the velocity tracker back to its initial state.
            // If necessary retrieve a new VelocityTracker object to watch the
            // velocity of a motion.
            mVelocityTracker = mVelocityTracker ?: VelocityTracker.obtain()
            // Add a user's movement to the tracker.
        MotionEvent.ACTION_MOVE -> {
            mVelocityTracker?.run {
                val pointerId: Int = event.getPointerId(event.actionIndex)
                // When you want to determine the velocity, call
                // computeCurrentVelocity(). Then call getXVelocity()
                // and getYVelocity() to retrieve the velocity for each pointer ID.

                val xVelocity = getXVelocity(pointerId)
                val yVelocity = getYVelocity(pointerId)

                if (abs(xVelocity) > abs(yVelocity))
                    if (xVelocity > 0)
                        // right
                        // left 
                    if (yVelocity > 0)
                       // down
                       // up
        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
            // Return a VelocityTracker object back to be re-used by others.
            mVelocityTracker = null
    return true

Leave a Reply

Your email address will not be published. Required fields are marked *