How to get the screen Density on Android via code?

Asked

Viewed 92 times

1

I am developing an application where images and layouts adapt according to the logical density of the device.

You can get the screen dpi from the device through Java?

1 answer

0


It is possible to obtain the logical density through the code

 getResources().getDisplayMetrics().density;

He will return you:

0.75 - ldpi (low resolution)

1.0 - mdpi (average resolution)

1.5 - hdpi (high resolution)

2.0 - xhdpi (great resolution)

3.0 - xxhdpi (+optimal resolution)

4.0 - xxxhdpi (excellent)

enter image description here

reference: Density

  • 1

    "Screen pixel density" gives the impression that they are physical pixels, which is not the case. The term used in the documentation is "logical density". And a more precise way of saying it would be "The approximate relationship between a dip (Density-Independent pixel) and a physical pixel". Nor is it accurate because it only serves to group the densities in Buckets or groups (ldpi, mdpi, hdpi, etc.). To obtain the exact density the calculation is more complicated.

  • Thanks @Piovezan, edited the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.