Android – Screen Size, Orientation and Pixel Density

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

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

Related Posts

1 COMMENTS

Leave a reply