Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the screen density programmatically in android?

How to get the screen density programmatically in android?

I mean: How to find the screen dpi of the current device?

like image 671
Praveen Avatar asked Jul 02 '10 14:07

Praveen


People also ask

How do you find the density of a screen?

You can get info on the display from the DisplayMetrics struct: DisplayMetrics metrics = getResources(). getDisplayMetrics(); Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size.

Which is the screen densities in Android?

36x36 (0.75x) for low-density (ldpi) 48x48 (1.0x baseline) for medium-density (mdpi) 72x72 (1.5x) for high-density (hdpi) 96x96 (2.0x) for extra-high-density (xhdpi)

How can get device width and height in Android programmatically?

int width = getWindowManager(). getDefaultDisplay(). getWidth(); int height = getWindowManager(). getDefaultDisplay().


1 Answers

You can get info on the display from the DisplayMetrics struct:

DisplayMetrics metrics = getResources().getDisplayMetrics(); 

Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size. So the metrics.densityDpi property will be one of the DENSITY_xxx constants (120, 160, 213, 240, 320, 480 or 640 dpi).

If you need the actual lcd pixel density (perhaps for an OpenGL app) you can get it from the metrics.xdpi and metrics.ydpi properties for horizontal and vertical density respectively.

If you are targeting API Levels earlier than 4. The metrics.density property is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpi can be calculated

int densityDpi = (int)(metrics.density * 160f); 
like image 100
joshperry Avatar answered Sep 30 '22 00:09

joshperry