Android Question What is the best way to automatically set label height?

Widget

Well-Known Member
Licensed User
Longtime User
Is this the correct way to automatically set the label height for a given width?

Label1.Width = 100dip
Label1.Height = -2


Or is there a better (easier) way to auto-adjust the Label1 height for a given width?

TIA
 

Widget

Well-Known Member
Licensed User
Longtime User
The correct way is to use StringUtils.MeasureMultilineTextHeight.

Note that you can use CustomListView.AddText. You will get a scrollable text with the correct height.

So every time I update the text value for a label, I should use:
Label1.Height = StringUtils.MeasureMultilineTextHeight(Label, NewText)​
to calculate the height?

Why don't you like:
Label1.Height = -2​

which will automatically adjust the label height? Is it restricted only to certain Android versions? Or is there a problem with it on some devices?
I prefer to use -2 because I don't have to remember to explicitly resize the height after I change the text, textsize, typeface, width etc..

TIA
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
If you use Label1.Height = -2 then you cannot GET actual label height.
For example:
B4X:
Label1.Height = -2
Log(Label1.Height)
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
If you use Label1.Height = -2 then you cannot GET actual label height.
For example:
B4X:
Label1.Height = -2
Log(Label1.Height)

Yes you can measure the label height with :
StringUtils.MeasureMultilineTextHeight(Label, Label1.Text) :rolleyes:

The reason I prefer to use Label1.Height=-2 is because I will assign values to labels 10x more often than I need to measure its height.
I prefer to know that the text is displayed properly every time and not cut off horizontally because its height is not large enough.

It can be done either way, I just like to make it as automatic as possible.
If there are limitations to using Label.Height=-2, then I'd certainly like to know about it now so I can do it properly. :D
 
Last edited:
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
like this?

B4X:
Label1.width = 100dip
Label1.Height = Label1.Width - 2dip

The trick is to adjust the label to the proper height to accommodate the text. Otherwise you will see only the top half of the text.
A label may be initially defined as 20dip, but it may not be high enough for the current TextSize, Typeface, or when the number of lines of text is changed. The text will get truncated and that is not good. Labels can be very very finicky when displaying text.

In your example, you create a label that is almost square. This won't work if the label has a colored background (too large) or the label has a click event because if the label background color is transparent, when the user clicks well below the visible text, it will still trigger the Label_Click event. The height of the label must be just right to accommodate the text that it contains and there are 2 ways to do it.

Label1.Height = -2 'Auto adjust label height to fit the text

or

Label1.Text = "Some New Text"
ResizeLabelHt(Label1) 'Use the code below every time the text, typeface, textisize changes

B4X:
Sub ResizeLabelHt(aLabel As Label)
    If aLabel.IsInitialized And aLabel.Parent <> Null Then
        Private SU As StringUtils                    'Should make SU a global variable
        Private NewHt As Int
        NewHt = SU.MeasureMultilineTextHeight(aLabel, aLabel.Text)
        If aLabel.Height <> NewHt Then
            aLabel.Height = NewHt
        End If
    End If
End Sub

I have included a small test app to show the two approaches.
Which is better? I don't really know. They both create labels of exactly the same dimensions.
There may be a reason for not using Label1.Height=-2 so the label height is auto-adjusted.
 

Attachments

  • DemonstrateLabelAutoHeight.zip
    9 KB · Views: 309
Upvote 0
Top