javaandroidhdpi

How to get Device DPI programmatically?


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


Solution

  • 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;