Is there a way to determine android physical screen height in cm or inches?

AndroidScreenResolutionInches

Android Problem Overview


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 Solutions


Solution 1 - Android

Use the following:

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(mWidthPixels/dm.xdpi,2);
    double y = Math.pow(mHeightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("debug","Screen inches : " + screenInches);

When mWidthPixels and mHeightPixels are taken from below code

private void setRealDeviceSizeInPixels()
{
    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);


    // since SDK_INT = 1;
    mWidthPixels = displayMetrics.widthPixels;
    mHeightPixels = displayMetrics.heightPixels;

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
    {
        try
        {
            mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        }
        catch (Exception ignored)
        {
        }
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
    {
        try
        {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
        }
        catch (Exception ignored)
        {
        }
    }

See this post for reference: https://stackoverflow.com/questions/1016896/how-to-get-screen-dimensions/15699681#15699681

Solution 2 - Android

for getting the current size use Math.round at the end.

 DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    double y = Math.pow(dm.heightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("debug","Screen inches : " + screenInches);

screenInches=  (double)Math.round(screenInches * 10) / 10;

Solution 3 - Android

android developers screen info.

use xdpi * widthPixels and ydpi * heightPixels might get you what you want i think.

Solution 4 - Android

Following code snippet will help you to get Screen Size in Inches

public static double getScreenSizeInches(Activity activity){
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    // since SDK_INT = 1;
    int mWidthPixels = displayMetrics.widthPixels;
    int mHeightPixels = displayMetrics.heightPixels;

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
        try{
            mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        } catch (Exception ignored) {}
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17) {
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
        } catch (Exception ignored) {}
    }

    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(mWidthPixels / dm.xdpi, 2);
    double y = Math.pow(mHeightPixels / dm.ydpi, 2);
    return Math.sqrt(x + y);
}

Hope this help.

Solution 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathanView Question on Stackoverflow
Solution 1 - AndroidBghaakView Answer on Stackoverflow
Solution 2 - AndroidgilixView Answer on Stackoverflow
Solution 3 - AndroidgingerbreadboyView Answer on Stackoverflow
Solution 4 - AndroidPankaj LilanView Answer on Stackoverflow
Solution 5 - AndroidleegorView Answer on Stackoverflow
Solution 6 - AndroidAnishView Answer on Stackoverflow
Solution 7 - AndroidCaseyBView Answer on Stackoverflow