Setting dp in decimal places in Android

I am trying to set the distance between two elements in Android. I installed 1dp and I thought the distance was greater than I wanted. I thought 0.5dp would be just fine. Is it possible to set margin in decimal as I try? If so, what is the minimum dp that is supported?

+8
android
source share
1 answer

Yes, you can use 0.5dp or whatever decimal you want, but be careful. When your dp is converted to pixels, they are multiplied by the density of the display logic and rounded.

If your dp = 0.1 than for hdpi display, you get (int) (0.1 * 1.5 + 0.5) = 1px. (1.5 - logical density for hdpi display)

If your dp = 0.5 to display mdpi you get (int) (0.5 * 1 + 0.5) = 1px (1 is the logical density for displaying mdpi)

+9
source share

All Articles