Android – Screen Size, Orientation and Pixel Density

Do you need the width, height or orientation of the screen in your Android application? Try this:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
                      .getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
int screenOrientation = display.getOrientation();

While we are here, take a look at DisplayMetrics class because we can get info from it easily. Do you need some density of pixels info? Try this:

DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float screenDensity = metrics.density; // 160dpi -> 1, 120dpi -> .75
int screenDensityDpi = metrics.densityDpi; // 120, 160 or 240

No comments

Trackbacks/Pingbacks

  1. Android – Handling Screen Orientation | IndyVision.Net - [...] If you need to find at runtime the screen orientation or other screen properties take a look at the ...

Leave a Reply

You must be logged in to post a comment.