Skip to content

Instantly share code, notes, and snippets.

@SemonCat
Created February 12, 2015 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SemonCat/531b94fe7ae4075e1658 to your computer and use it in GitHub Desktop.
Save SemonCat/531b94fe7ae4075e1658 to your computer and use it in GitHub Desktop.
private int getScreenInches(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
float widthDpi = displayMetrics.xdpi;
float heightDpi = displayMetrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));
return (int) Math.round(diagonalInches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment