4

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.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Alesqui
  • 6,417
  • 5
  • 39
  • 43
  • 1
    I have this problem with emulator. I think this problem is related to library not device. – Bob Aug 08 '12 at 06:27

3 Answers3

2

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.

Community
  • 1
  • 1
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • 2
    No, it is not duplicated. The way I ALREADY am doing is EXACTLY like that post says. Like I explained, it didn't work: I get the same density for devices with different densities. – Alesqui Nov 18 '11 at 18:19
  • 2
    @Alesqui: Then the devices are in the same density bucket. Samsung is welcome to categorize them to be in whatever density bucket they wish. – CommonsWare Nov 18 '11 at 18:34
  • 1
    Just going by the numbers, the I9000 ydpi is 250, the xdpi is 200; I'm assuming that due to the Pentile subpixel arrangement they classify it into the lower dpi category. The Tab seems like it would naturally fall into mdpi. – Kevin Coppock Nov 18 '11 at 19:06
1

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.

benkc
  • 3,292
  • 1
  • 28
  • 37
0

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.

Alesqui
  • 6,417
  • 5
  • 39
  • 43