tylercomp
tylercomp

Reputation: 132

How do i get the x,y coordinate for a screen touch?

Is there a simple way to obtain the x,y coordinate whenever someone touches the screen while my app is running? Just looking to store them in some integers.

Upvotes: 2

Views: 7447

Answers (2)

Jatin Raghav
Jatin Raghav

Reputation: 62

its very simple to get the coordinates position where the user touched on screen.

 @Override
public boolean onTouchEvent(MotionEvent event) {
    // MotionEvent object holds X-Y values
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        String text = "You click at x = " + event.getX() + " and y = " + event.getY();
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }

    return super.onTouchEvent(event);
}

Upvotes: 0

antonyt
antonyt

Reputation: 21883

Override onTouchEvent(MotionEvent event) and then call event.getX() and event.getY() to get the coordinate positions of where the user touched [they will be floats].

Upvotes: 5

Related Questions