KIRAN K J
KIRAN K J

Reputation: 732

Get absolute coordinates of view inside popup window

I have a draggable popup window fragment. I need to get the x and y coordinates of that fragment in onSaveInstanceState(). I got the width and height of the fragment by using getView().getWidth() and getView().getHeight(). But if i use getView().getX() or getView().getY() giving 0.0. Is there any chance to get the coordinates?

Upvotes: 3

Views: 948

Answers (1)

azizbekian
azizbekian

Reputation: 62209

Use View#getLocationOnScreen(int[]) API:

Computes the coordinates of this view on the screen. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order.


    int[] location = new int[2];
    getView().getLocationOnScreen(location);
    // now location[0] contains x position
    // and location[1] contains y position

Upvotes: 1

Related Questions