Last active
October 20, 2015 03:06
-
-
Save SemonCat/854e16ffe63c61aedf8b to your computer and use it in GitHub Desktop.
Android ScreenInches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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