Alê Oliveira
Alê Oliveira

Reputation: 6439

How to get screen DPI

I'm trying to get the screen's dpi but so far it isn't working. I tried:

    DisplayMetrics dm = new DisplayMetrics();
    mainContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
    SCREEN_DPI = dm.densityDpi;

But both on Samsung Galaxy Tab 10.1 and on Samsung Galaxy S I9000 the SCREEN_DPI is equal to 160. I also tried SCREEN_DPI = dm.density, but I get the value 1.0 to both cases.

Any help would be greatly appreciated.

Upvotes: 4

Views: 11851

Answers (3)

benkc
benkc

Reputation: 3382

Try getRealMetrics() instead:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);

This API was officially added in API 17, but in my limited testing it's worked correctly even on 4.0 devices, so it may have been a hidden API before that.

Official documentation here, though there seems to be more reasons that the other API doesn't do what you expect than the text there would imply.

Upvotes: 1

Alê Oliveira
Alê Oliveira

Reputation: 6439

According to @CommonsWare: "Samsung is welcome to categorize them (the devices) to be in whatever density bucket they wish". So, in this case, both devices are in the same density bucket.

Upvotes: 0

Paul Burke
Paul Burke

Reputation: 25584

Duplicate of this question

Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales that to the actual screen size. So the density property will be one of those constants (120, 160, or 240 dpi).

If you need the actual density (perhaps for an OpenGL app) you can get it from the xdpi and ydpi properties for horizontal and vertical density respectively.

Upvotes: 2

Related Questions