Android Question Determining If Text is Truncated

epiCode

Active Member
Licensed User
Is there a way to check if text in a label is truncated ?

or in other words if ellipses are showing
 
Solution
With Label1 declared as B4XView and MultiLine
B4X:
Dim su As StringUtils 'Add this library in project
If su.MeasureMultilineTextHeight(Label1.As(Label), Label1.Text) > Label1.Height Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
If SingleLine Label (always declared as B4xView)
B4X:
Dim bc As B4XCanvas
bc.Initialize(Label1)
Dim r As B4XRect = bc.MeasureText(Label1.Text, Label1.Font)
If r.Width > Label1.Width Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
Depending on TextSize, Label size and so on it can be more or less accurate.
With Label1 declared as B4XView and MultiLine
B4X:
Dim su As StringUtils 'Add this library in project
If su.MeasureMultilineTextHeight(Label1.As(Label), Label1.Text) > Label1.Height Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
If SingleLine Label (always declared as B4xView)
B4X:
Dim bc As B4XCanvas
bc.Initialize(Label1)
Dim r As B4XRect = bc.MeasureText(Label1.Text, Label1.Font)
If r.Width > Label1.Width Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
Depending on TextSize, Label size and so on it can be more or less accurate.
 
Upvote 0
Solution
With Label1 declared as B4XView and MultiLine
B4X:
Dim su As StringUtils 'Add this library in project
If su.MeasureMultilineTextHeight(Label1.As(Label), Label1.Text) > Label1.Height Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
If SingleLine Label (always declared as B4xView)
B4X:
Dim bc As B4XCanvas
bc.Initialize(Label1)
Dim r As B4XRect = bc.MeasureText(Label1.Text, Label1.Font)
If r.Width > Label1.Width Then
    Log("TRUNCATED")
Else
    Log("OK")
End If
Depending on TextSize, Label size and so on it can be more or less accurate.

Thanks @Sagenut

It is a single line
My existing code is like your second solution (though I am using inline java), I thought there could be a more simpler solution :(
 
Upvote 0
I thought there could be a more simpler solution
I can't think to something simple than 4-5 lines of code.
But maybe there is a better solution.
 
Last edited:
Upvote 0
Back
Top