Tiago Costa
Tiago Costa

Reputation: 4251

How to know the screen position where the user touched the screen?

In my app I draw an image covering the entire screen. I want to now how can I know where the user touched the screen?

Thanks

Upvotes: 0

Views: 1486

Answers (2)

Unoti
Unoti

Reputation: 1293

Here is a snippet of one of my games that does this. There are different ways to do it, but here I did it by subclassing View:

public class WorldView extends View {
   ...

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    Log.d(TAG,"touch event "+action+" x="+event.getX()+" y="+event.getY());
    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE)
    {
        int x = (int)event.getX();
        int y = (int)event.getY();
        Log.d(TAG,"setting target to "+x+","+y);
    }
    else
        return super.onTouchEvent(event);

    return true;
}

Upvotes: 0

antonyt
antonyt

Reputation: 21883

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

Upvotes: 1

Related Questions