Search code examples
androidgesturegesture-recognitiongestures

How to get the Coordinates of a view using Rect?


Referring to the image posted below, Can any one tell me what does it mean that rect.letf = 0 and rect.top = 0 while the other methods of the object rect shows values? how to get the coordinates of the topLeft topRight BottomLeft BottomRight using rect.

enter image description here


Solution

  • Those coordinates are correct. Top and Left on a Rect are relative to containing ViewGroup. A view that sits on the top left corner of a ViewGroup will always have 0 as the Rect.top and Rect.Left since the top left corner of a View is coordinate 0,0.

    If you want the coordinates relative to the main Activity or the screen use View.getLocationInWindow() or View.getLocationOnScreen() respectively.