How to determine Android screen size (dimensions) and orientation

The following Java source code shows how to determine the screen size (dimensions) of the Android device your application is running on:

Display display = getWindowManager().getDefaultDisplay();
String displayName = display.getName();  // minSdkVersion=17+
Log.i(TAG, "displayName  = " + displayName);

// display size in pixels
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.i(TAG, "width        = " + width);
Log.i(TAG, "height       = " + height);

// pixels, dpi
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
int widthPixels = metrics.widthPixels;
int densityDpi = metrics.densityDpi;
float xdpi = metrics.xdpi;
float ydpi = metrics.ydpi;
Log.i(TAG, "widthPixels  = " + widthPixels);
Log.i(TAG, "heightPixels = " + heightPixels);
Log.i(TAG, "densityDpi   = " + densityDpi);
Log.i(TAG, "xdpi         = " + xdpi);
Log.i(TAG, "ydpi         = " + ydpi);

// deprecated
int screenHeight = display.getHeight();
int screenWidth = display.getWidth();
Log.i(TAG, "screenHeight = " + screenHeight);
Log.i(TAG, "screenWidth  = " + screenWidth);

// orientation (either ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT)
int orientation = getResources().getConfiguration().orientation;
Log.i(TAG, "orientation  = " + orientation);

Just copy and paste that code into an Activity (or Fragment), run it, and look at the logcat output to find the dimensions of your screen in pixels, DPI density, and also the screen orientation.

Screen size/dimensions of Nexus 5 emulator

Running this code on an Android Nexus 5 emulator I see the following dimension-related information:

displayName  = Built-in Screen
width        = 1080
height       = 1776
widthPixels  = 1080
heightPixels = 1776
densityDpi   = 480
xdpi         = 480.0
ydpi         = 480.0
screenHeight = 1776
screenWidth  = 1080
orientation  = 1

Screen size/dimensions of Nexus 9 emulator

And here’s the screen size/dimension information I see when running the code on an Android Nexus 9 emulator:

displayName  = Built-in Screen
width        = 1536
height       = 1952
widthPixels  = 1536
heightPixels = 1952
densityDpi   = 320
xdpi         = 320.0
ydpi         = 320.0
screenHeight = 1952
screenWidth  = 1536
orientation  = 1

Summary

In summary, if you needed to see how to get the screen size/dimension and orientation information about an Android device in your Java code, I hope these examples are helpful.