0

How to get Device DPI programmatically. i need that value not a device like HDPI or MDPI

    DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                 break;
     case DisplayMetrics.DENSITY_HIGH:
                 break;
}

this code shows only High or low but i need Value

NatheemYousf
  • 97
  • 1
  • 2
  • 11
  • 1
    Possible duplicate of [getting the screen density programmatically in android?](http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android) – Madhukar Hebbar Sep 20 '16 at 09:03

2 Answers2

1
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

// will either be DENSITY_LOW, DENSITY_MEDIUM or DENSITY_HIGH
int dpiClassification = dm.densityDpi;

// these will return the actual dpi horizontally and vertically
 float xDpi = dm.xdpi;
 float yDpi = dm.ydpi;
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
0

Use metrics.xdpi and metrics.ydpi.

Asif Patel
  • 1,744
  • 1
  • 20
  • 27