How to get the screen density programmatically in android?
I mean: How to find the screen dpi of the current device?
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!