Bug? canvas.MeasureStringWidth return wrong value

Discussion in 'Bugs & wishlist' started by ArminKH, Feb 10, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    hi erel
    i think some times canvas return wrong values for MeasureStringWidth
    i think when the text completely is fit in a view,then the width of text must be less or equal to width of view.is this wrong?
    and if not please test attached sample

    if every thing is right on your device please see attached screen shot(Tested on android 4.2.1 With 5" screen and density 1.5)

    is there any way to measure string width by using reflection or other tools?
    tnx erel

    Attached Files:

  2. ArminKH

    ArminKH Well-Known Member

    and in addition canvas can't measure the width of Tab in a text :)
    good night
  3. stevel05

    stevel05 Expert Licensed User

  4. Erel

    Erel Administrator Staff Member Licensed User

    Canvas.MeasureStringWidth calls the native Paint.measureText method to measure the width.

    After I removed the following line:
    I see a difference of less than 1dip between the two results. It seems like a rounding error as suggested by the link @stevel05 posted.

    Note that you can use the AutoTextSizeLabel to accomplish a similar solution.
    ArminKH likes this.
  5. ArminKH

    ArminKH Well-Known Member

    Please see my attached screenshot in post 1
    The difference is more than 1 or 2
    After i remove that part of code,the difference is 4 between text width and label width !
    In addition the above code is necessary 4 last loop because my label width is increasing while my line count is > one and in last loop label1.width=label1.width+1dip and is a bit more than text width,so i reduce it by -1dip
    why u delete it?
    Erel when i set label1.width to -2 and measure width by using getWidth method in java object every thing is right but i dont want to use this
    has label1.width=-2 equal in java????any method for return same value
    Last edited: Feb 10, 2015
  6. ArminKH

    ArminKH Well-Known Member

    tnx 4 your response
    My english is not good
    i use getTextBound method by javaobject but b4a said this method not found
    Excuse me i'm not familar with java
    Can u please edit my code by above solution?
    just I want know width of text without any additional thing
  7. ArminKH

    ArminKH Well-Known Member

  8. stevel05

    stevel05 Expert Licensed User

    The B4j code cannot be used in Android as it relies on JavaFX methods and objects.

    Measuring text without displaying it in Android is problematic, the only method I have found that works consistently is using the Canvas Measure text. If you've found a more accurate method I suggest you use that, and hide the view used to measure the string off the edge of the screen.

    As you've asked for it, here is some code to use the getTextBounds method of paint.

    Dim Paint As JavaObject
    Dim Bounds As Rect
    Dim r As Reflector
               r.Target = r.GetContext
               r.Target = r.RunMethod(
               r.Target = r.RunMethod(
    Log((Bounds.Right - Bounds.Left)* (r.GetField("xdpi")/ 160))
    Dim PFM As JavaObject = Paint.RunMethod("getFontMetrics",Null)
    Last edited: Feb 10, 2015
    ArminKH likes this.
  9. ArminKH

    ArminKH Well-Known Member

    ok tnx :)
    when an issue solved new problem appared.
    but thank u mr stevel your answer every time is complete and helpful and i think u are my teacher and i can every time learn new thing from your knowledge
    excuse me 4 my english
    best regard
  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