Search code examples
androidandroid-layoutandroid-viewandroid-popupwindowandroid-window

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?


Solution

  • 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