How to get the screen density programmatically in android?
I mean: How to find the screen dpi of the current device?
This should help on your activity ...
void printSecreenInfo(){
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.i(TAG, "density :" + metrics.density);
// density interms of dpi
Log.i(TAG, "D density :" + metrics.densityDpi);
// horizontal pixel resolution
Log.i(TAG, "width pix :" + metrics.widthPixels);
// actual horizontal dpi
Log.i(TAG, "xdpi :" + metrics.xdpi);
// actual vertical dpi
Log.i(TAG, "ydpi :" + metrics.ydpi);
}
OUTPUT :
I/test( 1044): density :1.0
I/test( 1044): D density :160
I/test( 1044): width pix :800
I/test( 1044): xdpi :160.0
I/test( 1044): ydpi :160.42105
Yet another answer:
/**
* @return "ldpi", "mdpi", "hdpi", "xhdpi", "xhdpi", "xxhdpi", "xxxhdpi", "tvdpi", or "unknown".
*/
public static String getDensityBucket(Resources resources) {
switch (resources.getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
return "ldpi";
case DisplayMetrics.DENSITY_MEDIUM:
return "mdpi";
case DisplayMetrics.DENSITY_HIGH:
return "hdpi";
case DisplayMetrics.DENSITY_XHIGH:
return "xhdpi";
case DisplayMetrics.DENSITY_XXHIGH:
return "xxhdpi";
case DisplayMetrics.DENSITY_XXXHIGH:
return "xxxhdpi";
case DisplayMetrics.DENSITY_TV:
return "tvdpi";
default:
return "unknown";
}
}
This also works:
getResources().getDisplayMetrics().density;
This will give you:
0.75 - ldpi
1.0 - mdpi
1.5 - hdpi
2.0 - xhdpi
3.0 - xxhdpi
4.0 - xxxhdpi
ref: density
ref 2
Blundell's answer as a static helper method:
private static String getDensityName(Context context) {
float density = context.getResources().getDisplayMetrics().density;
if (density >= 4.0) {
return "xxxhdpi";
}
if (density >= 3.0) {
return "xxhdpi";
}
if (density >= 2.0) {
return "xhdpi";
}
if (density >= 1.5) {
return "hdpi";
}
if (density >= 1.0) {
return "mdpi";
}
return "ldpi";
}
Actualy if you want to have the real display dpi the answer is somewhere in between if you query for display metrics:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpiClassification = dm.densityDpi;
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
densityDpi * 160 will give you the values/suggestion which density you should use
0.75 - ldpi - 120 dpi
1.0 - mdpi - 160 dpi
1.5 - hdpi - 240 dpi
2.0 - xhdpi - 320 dpi
3.0 - xxhdpi - 480 dpi
4.0 - xxxhdpi - 640 dpi
as specified in previous posts
but dm.xdpi
won't give you always the REAL dpi of given display:
Example:
Device: Sony ericsson xperia mini pro (SK17i)
Density: 1.0 (e.g. suggests you use 160dpi resources)
xdpi: 193.5238
Real device ppi is arround 193ppi
Device: samsung GT-I8160 (Samsung ace 2)
Density 1.5 (e.g. suggests you use 240dpi resources)
xdpi 160.42105
Real device ppi is arround 246ppi
so maybe real dpi of the display should be Density*xdpi .. but i'm not sure if this is the correct way to do!
public static String getDensity(Context context) {
String r;
DisplayMetrics metrics = new DisplayMetrics();
if (!(context instanceof Activity)) {
r = "hdpi";
} else {
Activity activity = (Activity) context;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (metrics.densityDpi <= DisplayMetrics.DENSITY_LOW) {
r = "ldpi";
} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM) {
r = "mdpi";
} else {
r = "hdpi";
}
}
return r;
}