How to capture finger movement direction in android phone?

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.

  • Android Spannable Line Height
  • Android slide menu that slide from both sides left and right
  • Genymotion problems in windows 10
  • DrawerLayout and Multi Pane Layout
  • Debugging options unavailable in Eclipse after ADT reinstall
  • Android - Anything similar to the iPhone SDK Delegate Callbacks?
  • Related posts:

    setImageResource Vs setDrawable
    Is there a function in Android analogous to “int main” in C/C++ which contains t...
    Best practices for ID naming conventions in Android?
    RecyclerView onBindViewHolder only called when getItemViewType changes
    Define “Indirect subclass” in Android
    Status says Draft in Prod
  • Disable SSL as a protocol in HttpsURLConnection
  • Add an android library to the default list of libraries in Studio/SDK
  • Android: Real Time Image Processing
  • How to check if position if valid in StreetView in Android
  • Android convert ImageReader Image to YCbCr_420_SP (NV21) byte array using render script?
  • how to implement digest authentication using volley?
  • 2 Solutions collect form web for “How to capture finger movement direction in android phone?”

    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()) {
        case(MotionEvent.ACTION_DOWN):
            x1 = event.getX();
            y1 = event.getY();
            break;
    
        case(MotionEvent.ACTION_UP): {
            x2 = event.getX();
            y2 = event.getY();
            dx = x2-x1;
            dy = y2-y1;
    
            // Use dx and dy to determine the direction
            if(Math.abs(dx) > Math.abs(dy)) {
                if(dx>0)
                  directiion = "right";
                else
                  direction = "left";
            } else {
                if(dy>0)
                  direction = "down";
                else
                  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();
                break;
            case MotionEvent.ACTION_MOVE:
                int newX = motionEvent.getX();
                int newY = motionEvent.getY();
    
                int deltaX = oldX - newX;
                int deltaY = oldY - newY;
    
                if(Math.abs(deltaY)>Math.abs(deltaX))
                  //Motion in Y direction.
                else
                  // Motion in X direction.
                break;
             }
    

    There are lots of other method available on the MotionEvent object for use as well:
    http://developer.android.com/reference/android/view/MotionEvent.html

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.