Android Tutorial Convert Integer to DIP

Discussion in 'Tutorials & Examples' started by JonPM, Jun 19, 2012.

  1. JonPM

    JonPM Well-Known Member Licensed User

    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)
    Code:
    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:
    Code:
    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: Jun 19, 2012
    arced, KZero, RauchG and 5 others like this.
  2. Inman

    Inman Well-Known Member Licensed 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.
     
    TedDog likes this.
  3. JonPM

    JonPM Well-Known Member Licensed User

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

    Inman Well-Known Member Licensed 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!
     
  5. JonPM

    JonPM Well-Known Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    Under the hood the 'dip' units use this Java method. So the results should be the same.
     
    pauleffect likes this.
  7. corwin42

    corwin42 Expert Licensed 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.
     
  8. Inman

    Inman Well-Known Member Licensed User

    Ah yes, I probably did that way
     
  9. jaminben

    jaminben Member Licensed 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
     
  10. jaminben

    jaminben Member Licensed 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:
     
  11. JonPM

    JonPM Well-Known Member Licensed User

    Can you post your project (or a small example)?
     
  12. jaminben

    jaminben Member Licensed 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:
     
  13. Osi

    Osi Member Licensed 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: Dec 23, 2013
  14. Erel

    Erel Administrator Staff Member Licensed User

    This code is not needed. Please start a new thread for your question.
     
  15. ilan

    ilan Expert Licensed User

    so cool, is it possible to use it for textsize too?
     
  16. AZKANSOY

    AZKANSOY Member Licensed User

    Does this work?

    Code:
    Sub IntToDIP(Integer As Int) As Int 
       
    Dim DIP As Int
       DIP = Integer * 
    1dip
       
    Return DIP
    End Sub
     
    Alberto Iglesias likes this.
  17. klaus

    klaus Expert Licensed User

  18. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Works wonderful and for B4i and B4A!! Thanks!
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    That code is incorrect and will return wrong results for non-integer scales.

    Use DipToCurrent instead.
     
  20. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    but it works with B4i and B4a?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice