Question

I built a simple application for Nexus 7. I used the following code to get the screen size in DP units.

this.getResources().getConfiguration().screenWidthDp;
this.getResources().getConfiguration().screenHeightDp;

where "this" is MainActivity context object.

I get these values: 600 dp for width and 888 dp for height.

Pixel density is tvdpi which is 213, and the ratio of dp to pixels is 1.33

I used this formula

pixels = dips * (density / 160) 

which gives me for height

pixels = 888 * (213 / 160) = 1182.15. 

I know that pixel size of the Nexus 7 screen is 800 x 1280. Where are the missing 100 pixels of height in this calculation? Or did I do something wrong?

Was it helpful?

Solution

Configuration.screenHeightDp() returns the dimensions of the available area of the screen.

Your calculated value, 1182, is close to the the height in pixels minus the navigation bar and status bar (1173) of the Nexus 7, in other words the resolution available for your app to use.

Full screen apps should be able to use the full 1280 resolution.

OTHER TIPS

The following should give you the actual display size as a Point:

    private Point getDisplaySize(Context context) {

    if (Build.VERSION.SDK_INT >= 17) {
        return getDisplaySizeMinSdk17(context);
    } else if (Build.VERSION.SDK_INT >= 13) {
        return getDisplaySizeMinSdk13(context);
    } else {
        return getDisplaySizeMinSdk1(context);
    }
}

@TargetApi(17)
private Point getDisplaySizeMinSdk17(Context context) {
    final WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();

    final DisplayMetrics metrics = new DisplayMetrics();
    display.getRealMetrics(metrics);

    final Point size = new Point();
    size.x = metrics.widthPixels;
    size.y = metrics.heightPixels;

    return size;
}

@TargetApi(13)
private Point getDisplaySizeMinSdk13(Context context) {

    final WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();

    final Point size = new Point();
    display.getSize(size);

    return size;
}

@SuppressWarnings("deprecation")
private Point getDisplaySizeMinSdk1(Context context) {

    final WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();

    final Point size = new Point();
    size.x = display.getWidth();
    size.y = display.getHeight();

    return size;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top