174

I am working with Android Animation and I have found the Android coordinate system to be quite confusing so I am here to ask this question about how coordinates work in Android. I am following this image for moving one view to another but it seems it's not working:

image

1
  • in fact, what you've drawn is actually correct, just rotated 90 degrees counterclockwise.
    – Antek
    Apr 8, 2021 at 12:57

3 Answers 3

335

enter image description here

This image presents both orientation(Landscape/Portrait)

To get MaxX and MaxY, read on.

For Android device screen coordinates, below concept will work.

Display mdisp = getWindowManager().getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x; 
int maxY = mdispSize.y;

EDIT:- ** **for devices supporting android api level older than 13. Can use below code.

    Display mdisp = getWindowManager().getDefaultDisplay();
    int maxX= mdisp.getWidth();
    int maxY= mdisp.getHeight();

(x,y) :-

1) (0,0) is top left corner.

2) (maxX,0) is top right corner

3) (0,maxY) is bottom left corner

4) (maxX,maxY) is bottom right corner

here maxX and maxY are screen maximum height and width in pixels, which we have retrieved in above given code.

2
  • If we've got the View with layout_width="1" layout_height="1" would it mean that top left is (0,0) and bottom right is (1, 1)? Wouldn't that mean that View's width = 2 and height = 2? Contradiction. Feb 12, 2014 at 20:35
  • @AAnkit can you please confrim , this is valid for both screen orientations (Portrait and Landscape), does screen orientation change affects on (0,0) -> top left corner. (maxX,0) -> top right corner (0,maxY) -> bottom left corner (maxX,maxY) -> bottom right corner May 11, 2015 at 7:11
20

For Android API level 13 and you need to use this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int maxX = size.x; 
int maxY = size.y;

Then (0,0) is top left corner and (maxX,maxY) is bottom right corner of the screen.

The 'getWidth()' for screen size is deprecated since API 13

Furthermore getwidth() and getHeight() are methods of android.view.View class in android.So when your java class extends View class there is no windowManager overheads.

          int maxX=getwidht();
          int maxY=getHeight();

as simple as that.

9

This picture will remove everyone's confusion hopefully which is collected from there.

Android screen coordinate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.