Skip to content

Instantly share code, notes, and snippets.

@Phocacius
Created January 9, 2015 08:34
Show Gist options
  • Save Phocacius/6c935c84c86238587ab0 to your computer and use it in GitHub Desktop.
Save Phocacius/6c935c84c86238587ab0 to your computer and use it in GitHub Desktop.
Get the smallestWidth property in density pixels for the current device. >= 600 dp is a 7' tablet, >= 720 dp is a 10' tablet.
public int getSmallestWidthDensity(Activity context) {
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
return (int) Math.min(widthDp, heightDp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment