Reputation: 132
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
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
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