Android Question Flexible/Disappearing Labels

epiCode

Active Member
Licensed User
Is there a way to have Labels which can expand or collapse or completely disappear based on length of text in them, allowing neighboring labels to occupy their space ?
 

epiCode

Active Member
Licensed User
What are you trying to do? You can use B4XCanvas to measure the required width. There are other options as well.
I have multiple labels in layout which are mapped to recorddata.
When a recorddata returns null or "" I do not want the label to occupy space, ideally it should be invisible (currently invisible still occupies space in layout even though not visible)

also if the text is longer the label should increase or decrease either horizontally or vertically to accommodate text in available view space.
 
Upvote 0

epiCode

Active Member
Licensed User
Untitledl.png


like in this example if label4 is empty, I expect label5 to occupy space for both 4 and 5.
 
Upvote 0

epiCode

Active Member
Licensed User
Why use separate labels? You can use a single one.
Other options:
- Use xCustomListView.AddTextItem.
- BBCodeVoew
I'm using separate labels because the need is not just to display the data but have user interact with the elements.
BBCode will not help - does not render RTL languages correctly
AddTextItem can be used but it occupies one full row even for elements which might have single characters (Aesthetically very unappealing)
 
Upvote 0

Marvel

Active Member
Licensed User
I did something similar in one of my past projects. What I did was manually calculate the label height and reposition the top of the label under them based on the measured height. The code is intentionally broken down to see each step, you can modify it and probably add if statements to hide empty labels and set their height to 0.

B4X:
Sub Class_Globals
    Private label3, label4, label5 As Label
End Sub

Sub ArrangeLabels
    'Measure label height based on the text they contain
    Private lbl3_height, lbl4_height, lbl5_height As Int
    lbl3_height = MeasureTextHeight(label3) + 16dip '16dip as spacing between labels
    lbl4_height = MeasureTextHeight(label4) + 16dip
    lbl5_height = MeasureTextHeight(label5) + 16dip
    
    'Arrange labels based on the height of the labels on top them.
    label4.top = label3.top + lbl3_height
    label5.top = label4.top + lbl4_height
End Sub   

Sub MeasureTextHeight(lbl As Label) As Int
    If lbl.Text.Length < 1 Then Return 0
    Private strngUtls As StringUtils
    Return strngUtls.MeasureMultilineTextHeight(lbl,lbl.Text)
End Sub
 
Upvote 0
Top