Aniket
Aniket

Reputation: 2214

get views starting x-y coordinates ontouch specific view

I am working on custom keyboard where i need XY coordinates of touched view.

First i tell you what i tried,

I have called getLeft() on view object, which gives me left edge measure in pixel and it gives x coordinate.

Then i called getRawX() & getRawY() which is working, but not fit for my purpose. Because as it gives XY coordinates on root layout, but it gives different measures as i touch different places on same view.

So how can i get starting and ending XY coordinate when i touched on view.

Upvotes: 0

Views: 1826

Answers (1)

Aditya Kushwaha
Aditya Kushwaha

Reputation: 837

In your xml you will have to capture the click by placing the following in the views where you want to know the x and y coordinates.

android:onClick="findXY"

And then in your java place a function

public void findXY(View view) {
    int[] posXY = new int[2];
    view.getLocationOnScreen(posXY);
    int x = posXY[0];
    int y = posXY[1];
}

Upvotes: 3

Related Questions