NatheemYousf
NatheemYousf

Reputation: 97

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

Upvotes: 0

Views: 6452

Answers (2)

Kona Suresh
Kona Suresh

Reputation: 1854

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;

Upvotes: 1

Asif Patel
Asif Patel

Reputation: 1764

Use metrics.xdpi and metrics.ydpi.

Upvotes: 0

Related Questions