I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond.
Is there a method somewhere in the Android API for this?
I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond.
Is there a method somewhere in the Android API for this?
android developers screen info.
use xdpi * widthPixels and ydpi * heightPixels might get you what you want i think.
I have tried most of the ways mentioned in the other answers, but those methods fail on particular devices. So here is bit of my contribution to solving this problem. The code is written in Kotlin
Get the DisplayMetrics object :
val manager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val displayMetrics = DisplayMetrics()
manager.getDefaultDisplay().getMetrics(displayMetrics)
Calculate the Screen width in inches as follows :
val widthInDotPoints : Double = (displayMetrics.widthPixels * (160 * 1.0 /displayMetrics.densityDpi))
val widthInInches : Double = width / displayMetrics.xdpi
Here, I have calculated the width of device in terms of dot points(dp) using widthPixels and then converted it to width in terms of inches. I have used 160 .i.e DisplayMetrics.DENSITY_MEDIUM as conversion factor to convert the widthPixels to widthInDotPoints.
Similarly, calculate the Screen Height in inches :
val heightInDotPoints : Double = (displayMetrics.heightPixels * (160 * 1.0 /displayMetrics.densityDpi))
val heightInInches : Double = height / displayMetrics.ydpi
I used this to get the real size
final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
windowManager.getDefaultDisplay().getRealMetrics(metrics);
}
else {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int realHeight = metrics.heightPixels; // This is real height
int realWidth = metrics.widthPixels; // This is real width
You need to use the screen density to calculate this.
Context.getResources().getDisplayMetrics().density
According to the documentation:
The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.
This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).