Tori Tori - 2 months ago 18
Android Question

How to get x,y coordinates

One of the activities in my Android app has a relative layout with two imageviews, which are set up in an xml layout file. How can I programmatically get x,y location of the imageviews

Will these coordinates be different dependent on screen size and density?

What I want to do is have the user touch image1 which will then be animated to move to the location of image2.

Obviously, since I placed these images in the xml I know their x,y coordinates, but I'm concerned that these coordinates will vary dependent on the screen size and density.

Answer

You can use View.getTop(), View.getBottom(), View.getLeft(), and View.getRight(). These will return the location of the top, bottom, left and right edge of the View relative to the parent.

Comments