ListView Label.TextSize vs ItemHeight

devlei

Active Member
Licensed User
Longtime User
I use relative values (%Y) to set the ItemHeight for a ListView. I am struggling to get ListView.TwoLinesLayout.Label.TextSize and ListView.TwoLinesLayout.ItemHeight to match each other:
1. Is there a way to have TextSize also use relative values?
2. Does adjusting TextSize automatically affect ItemHeight and/or Label.Height?
3. How do I get Label & SecondLabel Closer together? There is so much space in between if not specified. Setting "Label.Height + SecondLabel.Height = ItemHeight" chops off text.

Any help will be appreciated!!
 

klaus

Expert
Licensed User
Longtime User
There is no automatic height adjustment according to the text size.

But you can:
- With Canvas.MeasureTextHeight measure the height of a given text.
- Set the height of the two labels according to the text height (plus some extra pixels).
- Set the TwoLinesLayout.SecondLabel.Top property to the TwoLinesLayout.Label.Height.
- Set the TwoLinesLayout.ItemHeight to the sum of the two label heights.

The two labels could have two different text sizes.

Best regards.
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Thanks, Klaus!

- Set the height of the two labels according to the text height (plus some extra pixels).
I understand this, but I need to do it the other way round, ie, set the TextSize according to the Label.Height (which has relative %Y value).

- Set the TwoLinesLayout.SecondLabel.Top property to the TwoLinesLayout.Label.Height.
The above point gives me a good direction, because when not set there seems to blank space between the two.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could do it that way:
- Define the ItemHeight
- Calculate the Label heights
- Beginning with a high TextSize, decrement this one in a loop, and measure the text height till this one becomes smaller than the label height.

Best regards.
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Once again, thanks Klaus, your suggestion is spot-on.

I am still unclear about one thing though: Item Height & Label Heights are measured in pixels whereas TextSize is measured in Font size points 10pts ,11pts ,12pts ,14pts etc. So if the Label.Height is 40 pixels how do I know how many points will be just smaller than that?
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
I think I have solved this issue, thanks Klaus for the assistance.

It seems it is better to use dip values for Label & Item Heights and TextSize even if the ListView and all the other views are positioned and sized relatively.

For those who want to specify your own TextSizes for Listview and then have the Labels Heights automatically adjusted (with your padding preferences), the following procedure might be helpful.

B4X:
Sub SetLVHeights4TextSize(LV As ListView, TwoLine As Boolean, Text1Size As Float, Text2Size As Float, Padding As Float)
   If TwoLine = True Then     'True for TwoLine False for SingleLine
      LV.TwoLinesLayout.Label.Top = 0
      LV.TwoLinesLayout.Label.TextSize = Text1Size
      LV.TwoLinesLayout.Label.Height = Text1Size + (2 * Padding)
      LV.TwoLinesLayout.SecondLabel.Top = LV.TwoLinesLayout.Label.Height
      LV.TwoLinesLayout.SecondLabel.TextSize = Text2Size
      LV.TwoLinesLayout.SecondLabel.Height = Text2Size + (2 * Padding)
      LV.TwoLinesLayout.ItemHeight = LV.TwoLinesLayout.Label.Height + LV.TwoLinesLayout.SecondLabel.Height
      LV.TwoLinesLayout.Label.Gravity = Gravity.CENTER_VERTICAL
      LV.TwoLinesLayout.SecondLabel.Gravity = Gravity.CENTER_VERTICAL
   Else
      LV.SingleLineLayout.Label.Top = 0
      LV.SingleLineLayout.Label.TextSize = Text1Size
      LV.SingleLineLayout.Label.Height = Text1Size + (2 * Padding)
      LV.SingleLineLayout.ItemHeight = LV.SingleLineLayout.Label.Height
      LV.SingleLineLayout.Label.Gravity = Gravity.CENTER_VERTICAL
   End If
End Sub
 
Last edited:
Upvote 0

rtesluk

Member
Licensed User
Longtime User
How about a color option

Jan 13 2012
07:50 Hours

I wonder if you can also control the color of the labels and textcolor also.

I tried to do that but the only color of the label was a mossy green and the text color was white.

Ray Tesluk :(
Port Hope ON
Canada

B4X:
Sub SetLVHeights4TextSize(LV As ListView, TwoLine As Boolean, Text1Size As Float, Text2Size As Float, Padding As Float, Lblcol As Int, Txtcol As Int)    
If TwoLine = True Then        
   LV.TwoLinesLayout.Label.Top = 0        
   LV.TwoLinesLayout.Label.Color = Colors.Blue 
   LV.TwoLinesLayout.Label.TextColor = Colors.Cyan 
   LV.TwoLinesLayout.Label.TextSize = Text1Size         
   LV.TwoLinesLayout.Label.Height = (Text1Size + (2 * Padding)) * Density        
   LV.TwoLinesLayout.SecondLabel.Top = LV.TwoLinesLayout.Label.Height        
   LV.TwoLinesLayout.SecondLabel.Color = Colors.Blue
   LV.TwoLinesLayout.SecondLabel.TextColor = Colors.Cyan 
   LV.TwoLinesLayout.SecondLabel.TextSize = Text2Size         
   LV.TwoLinesLayout.SecondLabel.Height = (Text2Size + (2 * Padding)) * Density        
   LV.TwoLinesLayout.ItemHeight = LV.TwoLinesLayout.Label.Height + LV.TwoLinesLayout.SecondLabel.Height        
   LV.TwoLinesLayout.Label.Gravity = Gravity.CENTER_VERTICAL        
   LV.TwoLinesLayout.SecondLabel.Gravity = Gravity.CENTER_VERTICAL    
Else        
   LV.SingleLineLayout.Label.Top = 0        
   LV.TwoLinesLayout.Label.Color = Colors.Blue 
   LV.TwoLinesLayout.Label.TextColor = Colors.Cyan 
   LV.SingleLineLayout.Label.TextSize = Text1Size        
   LV.SingleLineLayout.Label.Height = (Text1Size + (2 * Padding)) * Density        
   LV.SingleLineLayout.ItemHeight = LV.SingleLineLayout.Label.Height        
   LV.SingleLineLayout.Label.Gravity = Gravity.CENTER_VERTICAL    
End If
End Sub
 
Upvote 0
Top