Android Tutorial Convert Integer to DIP

Saw a request for this so I thought I'd share. Below you will find a way of converting an integer to a DIP (Density Independent Pixel). This may be useful in situations where you have a variable that you would like to use as width/height of a view in DIP.

IMPORTANT: Relfection library is required

Copy this code into your project (either into Main module or a code module)
B4X:
Sub IntToDIP(Integer As Int) As Int
   Dim r As Reflector
   Dim scale As Float
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getDisplayMetrics"))
   scale = r.GetField("density")
  
   Dim DIP As Int
   DIP = Integer * scale + 0.5
   Return DIP
End Sub

Example usage:
B4X:
myRandomVar = 192
myLabel.width = IntToDip(myRandomVar)

I only have a couple devices to test it on, and it seems to be working correctly. Please reply here if you have any problems.
 
Last edited by a moderator:

Inman

Well-Known Member
Licensed User
Longtime User
Interesting. I was under the impression that you could multiply the integer with 1dip to get it's dip equivalent. Thanks for the code.
 

JonPM

Well-Known Member
Licensed User
Longtime User
Interesting. I was under the impression that you could multiply the integer with 1dip to get it's dip equivalent. Thanks for the code.

If you want to do it the easy way! LOL, i like your way better...
 

Inman

Well-Known Member
Licensed User
Longtime User
But there is some issue with my method. Sometime back I tested it on a cheap Chinese phone running Android 2.2, with 240x320 screen and shockingly on that device 1dip=0!
 

JonPM

Well-Known Member
Licensed User
Longtime User
But there is some issue with my method. Sometime back I tested it on a cheap Chinese phone running Android 2.2, with 240x320 screen and shockingly on that device 1dip=0!

Ok, so maybe this code could be of some use to someone :)
 

corwin42

Expert
Licensed User
Longtime User
But there is some issue with my method. Sometime back I tested it on a cheap Chinese phone running Android 2.2, with 240x320 screen and shockingly on that device 1dip=0!

I think this was because you stored the value in an Int?

1dip on a 240x320 device should be 0.75. If you store it in an Java Int variable then it will get 0.
 

Inman

Well-Known Member
Licensed User
Longtime User
I think this was because you stored the value in an Int?

1dip on a 240x320 device should be 0.75. If you store it in an Java Int variable then it will get 0.

Ah yes, I probably did that way
 

jaminben

Member
Licensed User
Longtime User
Hi JonPM,

I was happily using your code from above with everything working as it should but since I updated my phone to Android 4.0.4 it doesn't seem to work anymore. Well its working but my scale is now 2 (not sure what it was before) which results in the text being placed at 978dip (off screen) rather than 489dip.

Any ideas?

Cheers

Ben
 

jaminben

Member
Licensed User
Longtime User
As above really... everytime I try and use the code from the first post it doubles the original int value, my scale is coming back as 2 (Samsung Galaxy S3) and ends up pushing stuff off screen.

:sign0163:
 

jaminben

Member
Licensed User
Longtime User
It seems to be working again now... I also posted in another thread which was semi related to my problem and Klaus suggested a fix.

Thanks for the reply though JonPM... If I see anymore issues I'll repost here.

:sign0098:
 

Osi

Member
Licensed User
Longtime User
Hello,

The code in the topic does not work on the Galaxy Tab 3 10.1, Android 4.2.2 returns the lower third of the text.
 
Last edited:
Top