Android Question get width of text in a label by reflection

Discussion in 'Android Questions' started by ArminKH, Jan 28, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    Hi
    I want measure the width of a text in a label just by using reflection or java object lib
    Because by using canvas for persian or arabic language texts(combined by english text or engilsh numbers) some times that return wrong value
    Please help i think reflection lib is better for doing this
    Tnx for your response
     
  2. stevel05

    stevel05 Expert Licensed User

    Can you provide an example project where the values are incorrect?
     
    ArminKH likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    It doesn't matter if you use reflection or the standard methods. Eventually they all call the same native API.
     
    ArminKH likes this.
  4. ArminKH

    ArminKH Well-Known Member

    See this screen shot
    The text is fit in label and the measure width is for label1.text.trim 2015_01_29_11.34.22.png
     
  5. ArminKH

    ArminKH Well-Known Member

    Ok erel but what is the solution?if i add any char to this text,the text width is lower than width of label but the height of text be double
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You should better explain the problem and post the relevant code.
     
    ArminKH likes this.
  7. ArminKH

    ArminKH Well-Known Member

    OK Excuse me 4 my english
    please see screenshot,my text is one line and is completely fit in label and width of text is equal with width of label
    but after measuring width of text by using canvas the result is different(see result in screenshot),and text width is not equal with label's width
    i think when the text completely is fit in a view,the text width and view width must be equal.is this wrong?
    if you add this character "ـ" to end of text the line count will be 2 and height of text increased but the text width is still less than label's width(tested on 5'' device with density 1.5 and label width is 502 on my device)
    and now my problem is this:
    if my text is completely fit in label why my measured text width is less than label's width?
    and if my text width is less than label's width why my line count and height of text after add an other character are increased?
    please see my attached project
    tnx again erel and stevel
     

    Attached Files:

    • P.zip
      File size:
      7.3 KB
      Views:
      65
    Last edited: Jan 29, 2015
  8. ArminKH

    ArminKH Well-Known Member

    @Erel
    Erel after hours i find out what is wrong
    Please see text of label from designer
    The characters between چرا and (1 is unusual
    That is not space and is similar to add space by using Tab key on keyboard
    And after many test now i know the canvas cant measure the width of this special part of text and return 0 for it
    Erel i just copy text from ms word app and paste in label.that number(first number in text)is created by ms word auto number creator and i dont know what is that part

    And now after many hours i dont know what is solution for solving this problem
    Please help because this is necessary for me

    Tnx agaaaaaaain
    2015_01_29_11.34.22.png
     
    Last edited: Jan 29, 2015
  9. stevel05

    stevel05 Expert Licensed User

    If it's a Tab character you can replace it within the string i.e. Str.Replace(Tab," ") or similar
     
    ArminKH likes this.
  10. ArminKH

    ArminKH Well-Known Member

    I dont know exactly what is that but i think that is similar to tab
    Dear stevel my goal is just measuring width of text,when i dont know the width of this part so i dont know replace that with how many space !!!
     
  11. ArminKH

    ArminKH Well-Known Member

    And at last i think each tab is equal to 3 space and we can add the width of 3 space to text for each tab
    But i dont know this issue just is for tab or not?
     
  12. stevel05

    stevel05 Expert Licensed User

    If it is a Tab, then the value could vary depending on the source of the text. There is no way of knowing what the original size would have been, you'll just have to make a choice, or set a variable in case you want to change it for different sources.
     
    ArminKH likes this.
  13. ArminKH

    ArminKH Well-Known Member

    Now i test replacing tab with other characters but my try is lost :( because the tab width is not completely equal with 3 space's width
    I think the tab has realy different story
     
    Last edited: Jan 30, 2015
  14. stevel05

    stevel05 Expert Licensed User

    Perhaps it's not a Tab character.

    Check the Ascii value of the offending character.

    Code:
    For i = 0 to str.Length - 1
        
    Log(Asc(str.CharAt(i)))
    Next
     
    ArminKH likes this.
  15. ArminKH

    ArminKH Well-Known Member

    No that is tab because
    Label1.Text.Contains(Tab) returned true
    But i cant replace it !!!
     
  16. ArminKH

    ArminKH Well-Known Member

    @Erel
    Any idea? About measuring the width of Tab or other special character ???!!!
    Please help me this is necessary for me :(
     
    Last edited: Jan 30, 2015
  17. ArminKH

    ArminKH Well-Known Member

    Last edited: Jan 30, 2015
  18. stevel05

    stevel05 Expert Licensed User

    That is a class specific to the Eclipse software and not a generic java class.

    Tab is a marker in the text that would be formatted within each application however the application is set up to deal with it. The best you can do is specify how you want it to be dealt within your implementation. I have tested replacing the Tab character, it works as expected here.

    See attached.
     

    Attached Files:

    • P1.zip
      File size:
      7.3 KB
      Views:
      93
    ArminKH likes this.
  19. ArminKH

    ArminKH Well-Known Member

    Ok thank stevel :)
     
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