106

I need to get the touch begin position (X, Y) , touch move position and touch end position of the screen in android.

Alex
  • 1,241
  • 3
  • 12
  • 15

4 Answers4

190
@Override
public boolean onTouchEvent(MotionEvent event)
{
    int x = (int)event.getX();
    int y = (int)event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }

    return false;
}

The three cases are so that you can react to different types of events, in this example tapping or dragging or lifting the finger again.

Black
  • 18,150
  • 39
  • 158
  • 271
dbrettschneider
  • 3,173
  • 1
  • 29
  • 28
38

Supplemental answer

Given an OnTouchListener

private View.OnTouchListener handleTouch = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("TAG", "touched down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("TAG", "moving: (" + x + ", " + y + ")");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("TAG", "touched up");
                break;
        }

        return true;
    }
};

set on some view:

myView.setOnTouchListener(handleTouch);

This gives you the touch event coordinates relative to the view that has the touch listener assigned to it. The top left corner of the view is (0, 0). If you move your finger above the view, then y will be negative. If you move your finger left of the view, then x will be negative.

int x = (int)event.getX();
int y = (int)event.getY();

If you want the coordinates relative to the top left corner of the device screen, then use the raw values.

int x = (int)event.getRawX();
int y = (int)event.getRawY();

Related

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • This works, however in my testing, the view to which the listener is added, should not be covered by other views. To get *absolute coordinates* of the touched position on screen, I added a framelayout as base view, and on top of it, the view with the listener. Don't forget to `return false` otherwise it will consume the touch. – lenooh Jan 06 '18 at 22:33
  • @lenooh, `getRawX()` and `getRawY()` should return the absolute coordinates of the touch position on any view the listener is added to. But you're right, if there is another view on top of it that returns `true`, then no lower views will be notified of touch events. – Suragch Jan 07 '18 at 12:52
  • Suragch: exactly. Since I use a FrameLayout, I set the view to "matchParent" so `getX()` and `getY()` work just fine (no need for `getRawX/Y()`). – lenooh Jan 07 '18 at 13:52
  • @Suragch Is there anyway to record touch even if it is covered by some other view or has children like buttons? – vignesh kumar Jul 03 '20 at 19:12
  • @vigneshkumar, views are notified of touch events from the bottom up (see the link at the bottom of my answer), so you could record the event in `onInterceptTouchEvent` and then return `false` so that the event is processed as normal. – Suragch Jul 04 '20 at 00:51
11
@Override
    public boolean onTouch(View v, MotionEvent event) {
       float x = event.getX();
       float y = event.getY();
       return true;
    }
nsL
  • 3,722
  • 3
  • 23
  • 40
Sephy
  • 50,022
  • 30
  • 123
  • 131
1

Here is the Koltin style, I use this in my project and it works very well:

this.yourview.setOnTouchListener(View.OnTouchListener { _, event ->
        val x = event.x
        val y = event.y

        when(event.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d(TAG, "ACTION_DOWN \nx: $x\ny: $y")
            }
            MotionEvent.ACTION_MOVE -> {
                Log.d(TAG, "ACTION_MOVE \nx: $x\ny: $y")
            }
            MotionEvent.ACTION_UP -> {
                Log.d(TAG, "ACTION_UP \nx: $x\ny: $y")
            }
        }
        return@OnTouchListener  true
    })
cjung
  • 11
  • 1