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 will work on API level 4 and higher.
If you want to retrieve the density from a Service it works like this:
Blundell's answer as a static helper method:
This also works:
This will give you:
0.75 - ldpi
1.0 - mdpi
1.5 - hdpi
2.0 - xhdpi
3.0 - xxhdpi
4.0 - xxxhdpi
ref: density
The following answer is a small improvement based upon qwertzguy's answer.
Try this...
In kotlin
You can call by
println("density: ${determineScreenDensityCode()}")
and the output will beSystem.out: density: xxxhdpi