Mateus
Mateus

Reputation: 409

Gesture - Show coordinates when finger motion

I need to create an Activity that while drag your finger across the screen, display the XY coordinates (where the finger goes). Could anyone help me?

Upvotes: 1

Views: 2209

Answers (1)

Joe
Joe

Reputation: 2659

OnTouch

You need to implement an OnTouchListener for whatever view you want to recognize the drag.

Then in the onTouchListener you need to display the X and Y coordinates. I believe you can get those via MotionEvent.getRawX() and MotionEvent.getRawY()

You can use the MotionEvent.getAction() method to find out when a drag is occurring. I believe the constant is MotionEvent.ACTION_MOVE. Here is some psuedo-code:

Add OnTouchListener interface

public class XYZ extends Activity implements OnTouchListener

Register the listener in the onCreate method

public void onCreate(Bundle savedInstanceState)
{
    //other code

    View onTouchView = findViewById(R.id.whatever_id);
    onTouchView.setOnTouchListener(this);
}

Implement the onTouch method

public boolean onTouch(View view, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_MOVE)
    {
        float x = event.getRawX();
        float y = event.getRawY();
        //  Code to display x and y go here
    }
}

Upvotes: 3

Related Questions