Android Question Measure Height of a String?

ilan

Expert
Licensed User
Longtime User
hi

i found lots of thread on how to measure the height of a string but i want something different.
i want to add a label to a clv. i know the width of the clv and i want to add a single label to each clv item. but how can i know the Height of the label?
the width of the label is the same as the clv width but how do i measure the height of the label so the text will fit inside?

i need to measure the string with his typeface and textsize that should fit in a specific width (clv width) but height is unknown!

any help would be very much appreciated :)

thanx
 

TILogistic

Expert
Licensed User
Longtime User
Do you want something like this?
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
@ilan

This is exactly what measureMultiLinetext does. Given a width, font and text, calculate the height.

Here is a sample project, I posted before.

 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Do you want something like this?
AddTextItem or InsertAtTextItem.
These methods add a panel with a label to CLV and adjust the height of the label to the content.

see the source code for customlistview.
B4X:
    Dim pnl As B4XView = CreatePanel("")
    Dim lbl As B4XView = CreateLabel(Text)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
thanx guys, i found the answer 5 minutes after writing this post and it was infront of my eyes:

1636489019223.png


StringUtils does exactly what i wanted, it gives me back the height i need to set to the label and this is what i needed although its not very accurate but its good enough for me. (the reason it is less accurate may be because of me using Hebrew letters but i am not sure, anyway it is a good solution for my problem :) )

thank you all!
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
See code source customlistview (b4A)

StringUtils
lbl.Height = su.MeasureMultilineTextHeight(lbl, txt)
B4X:
Private Sub CreateLabel(txt As Object) As B4XView
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = DesignerLabel.Gravity
    lbl.TextSize = DesignerLabel.TextSize
    lbl.Typeface = DesignerLabel.Typeface
    lbl.SingleLine = False
    lbl.Text = txt
    lbl.Width = sv.ScrollViewContentWidth - 10dip
    lbl.Height = su.MeasureMultilineTextHeight(lbl, txt)
    Return lbl
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

thanx but in my case the label height is to big when there is more text so your solution will make it bigger.
it is less important to me. i dont need it to be to much accurate but i wonder how Whatsapp is doing the calculation. you can see it doesnot matter what language you are using the height is very precise according to the text.


cc8e960ef8de0561f5b88bde6863d61da8c2e454-tc-img-preview.jpg
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
if i am not wrong i remember that @Erel has an example for such a chat application but i could not find it. i am sure he used xclv for that.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Upvote 0

ilan

Expert
Licensed User
Longtime User
example:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    For i = 0 To 10
        CustomListView1.AddTextItem(getrandomtext(Rnd(100,300)),i)
    Next
End Sub

Private Sub getrandomtext(num As Int) As String
    Dim allowedChars As String = "אבגדהוזחטיכלמנסעפצקרשתךףן 1234567890"
    Dim returnTxt As String
    For i = 0 To num
        Dim index As Int = Rnd(0,allowedChars.Length)
        returnTxt = returnTxt & allowedChars.CharAt(index)
    Next
    Return returnTxt
End Sub

1636542465656.png
 
Upvote 0
Top