How do android screen coordinates work?

AndroidAndroid AnimationCoordinate Systems

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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

Android screen coordinate

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYasir KhanView Question on Stackoverflow
Solution 1 - AndroidAAnkitView Answer on Stackoverflow
Solution 2 - AndroidvidsView Answer on Stackoverflow
Solution 3 - AndroidGk Mohammad EmonView Answer on Stackoverflow