0

I am working on an Android application and I try to understand which DPI is my display. The vendor said is an HDPI display but I get a complete different result. This is my code:

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

String text = "Width: " + metrics.widthPixels
        + "\nHeight: " + metrics.heightPixels
        + "\nDpi: " + metrics.densityDpi
        + "\nXDPI: " + metrics.xdpi
        + "\nYDPI: " + metrics.ydpi
        + "\nRDpi: " + (int)(metrics.density * 160f);

And the device print the following information:

Width: 320
Height: 320
Dpi: 160
XDPI: 262
YDPI: 262
RDPI: 160

So it seems that the vendor consider XDPI and XDPI the parameters to state that the display is an HDPI but in reality it is an MDPI. Am I wrong?

vincentzhou
  • 710
  • 6
  • 18
Raffaeu
  • 6,694
  • 13
  • 68
  • 110

3 Answers3

1

A good way to get the DPI is to generate a string.xml file for every density.

Create folders called values-mdpi, values-hdpi, values-xhdpi... and add a strings.xml in everyone, with a string called density with the value of the density (mdpi for strings.xml located in values-mdpi, etc)

then reading R.string.density will give you the correct density

JesusS
  • 1,645
  • 1
  • 18
  • 31
  • I can confirm that using this approach I get back the string MDPI, so the vendor use the XDPI and YDPI to sell the display as an HDPI (260) but in reality is an MDPI – Raffaeu May 21 '15 at 08:14
0

120dpi = ldpi 160dpi = mdpi 240dpi = hdpi

So you are on mdpi

roiberg
  • 13,629
  • 12
  • 60
  • 91
0

As stated by joshperry The metrics.density property is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpi can be calculated

int densityDpi = (int)(metrics.density * 160f);
Community
  • 1
  • 1
Key_coder
  • 534
  • 3
  • 27