Skip to content

Instantly share code, notes, and snippets.

@steren
Created March 1, 2012 13:57
Show Gist options
  • Save steren/1949969 to your computer and use it in GitHub Desktop.
Save steren/1949969 to your computer and use it in GitHub Desktop.
Android screen size
// if API level > 13, we can use this:
// Point outSize = new Point();
// display.getSize(outSize);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int ht = displaymetrics.heightPixels;
int wt = displaymetrics.widthPixels;
// Determine screen size
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
}
// Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this,
"DENSITY_HIGH... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this,
"DENSITY_MEDIUM... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this,
"DENSITY_LOW... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
this,
"Density is neither HIGH, MEDIUM OR LOW. Density is "
+ String.valueOf(density), Toast.LENGTH_LONG)
.show();
}
// These are deprecated
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment