Question

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?

Was it helpful?

Solution

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: Get screen dimensions in pixels

OTHER TIPS

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;

android developers screen info.

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top