Android Question Determining the height of text after ellipsis

Kwame Twum

Active Member
Licensed User
Longtime User
How do you determine the height of text after it's been ellipsized?
I'm trying to determine the height using StringUtils' MeasureMultilineTextHeight method but it gives me the height of the entire text

B4X:
Sub GetEllipsisHeight(Str As String) As Int
    Dim l As Label:l.initialize("")
    l.Text = Str.trim:l.Typeface = c.NormalFont:l.TextSize = 14
    Ellip(l,"END",3)
    Activity.AddView(l,10%x,0,80%x,-2) 'Displays text Ellipsis perfectly
    Dim su As StringUtils
    Dim ui As Int = su.MeasureMultilineTextHeight(l,l.Text)
    Return ui 'Returns the height of the original text
End Sub

Sub Ellip(textview As Label,mode As String,lines As Int)
    Dim jo = textview As JavaObject
    jo.RunMethod("setEllipsize", Array As Object(mode))
    jo.RunMethod("setLines", Array As Object(lines))
End Sub

Any way to achieve this?
 

klaus

Expert
Licensed User
Longtime User
The problem is:
- Ellisize is single line by definition !
- You measure Multiline.

My suugestion is:
B4X:
Private bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
Private cvs As Canvas
cvs.Initialize2(bmp)
Private ui As Int = cvs.MeasureStringHeight("Ag", l.Typeface, l.TextSize)
Why "Ag" for the text ?
You get the total height of any text in single line.
 
Upvote 0

Kwame Twum

Active Member
Licensed User
Longtime User
Thanks @klaus
I used the following to get the number of lines that the label would require
B4X:
Ceil((charwidth*txtToMeasure.Length)/Activity.Width) 'txtToMeasure is the label's content

Sub charwidth(cv As ChatView) As Int
    Private bmp As Bitmap
    bmp.InitializeMutable(2dip, 2dip)
    Dim cvs As Canvas
    cvs.Initialize2(bmp)
    Return cvs.MeasureStringwidth("A",RegFont,15)
End Sub

Then multiplied that by the result of your code to get the height of the entire content.
This way, I'm able to track Ellipsis line height I need
 
Upvote 0
Top