Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting dp in decimals in Android

Tags:

android

I am trying to set the distance between 2 elements in Android. The margin I have set is 1dp and I was thinking that the distance is more than what I wanted it to be. I thought that 0.5dp would be just fine. Is it possible to set the margin in decimals as I am trying? If so, what is the minimum dp that is supported?

like image 488
Gautam Krishnan Avatar asked Feb 20 '13 12:02

Gautam Krishnan


People also ask

Can a dp be Decimal?

Yeah, you can use 0.5dp or what ever decimal number you want, but be careful. When your dp converted to pixels they multiplied by display logic density and rounded up.

How do I get Android to only have 2 decimal places?

You can use String. format("%. 2f", d) , your double will be rounded automatically. Save this answer.


1 Answers

Yeah, you can use 0.5dp or what ever decimal number you want, but be careful. When your dp converted to pixels they multiplied by display logic density and rounded up.

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

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

like image 166
Leonidos Avatar answered Sep 18 '22 15:09

Leonidos