Listview TextSize & Item Heights

devlei

Active Member
Licensed User
Longtime User
Recently I posted a similar question, and from the responses, I came up with what I thought would be a solution to match text size to label & item heights irrespective of device. It worked great on the emulator (320x480 scale=1 160dpi), but on my Galaxy S (480x800 233ppi) the bottom portion of text is cut off.

My solution was to use TextSize to set Label.Height to set ItemHeight as given below. Can anyone help me to get this right so that for any given textsize, the text will fit into the listview item with the amount of padding specified?

B4X:
Sub SetLVHeights4TextSize(LV As ListView, TwoLine As Boolean, Text1Size As Float, Text2Size As Float, Padding As Float)
   If TwoLine = True Then
      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
 

klaus

Expert
Licensed User
Longtime User
You can use MeasureMultilineTextHeight from the StringUtils library to get the height for the longest text you want to display.
Be aware that the layouts are the same for all items in the listview.
If you want different heights for the items you should use a ScrollView.

Best regards.
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Thanks, Klaus, but I only want a single line of "unwrapped" text for the listview item.

All the other views on the activity are sized relative to activity height/width, but the listview item height must at least be the height of a finger-press irrespective of device/screen. How can I achieve this?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you have an example that calculates the Label.Height according to the TextSize, but with a minimum ListView.ItemHeight .
In that case the program calculates the text size that fits this ListView.ItemHeight.
B4X:
Sub Globals
    Dim ListView1 As ListView
    Dim StringUtil As StringUtils
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim i As Int
    Dim minItemHeight, txtHeight, minTextHeight, txtPadding As Float
    Dim lbl As Label
    
    ListView1.Initialize("ListView1")
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
    lbl = ListView1.SingleLineLayout.Label
    
    lbl.TextSize = 15
    lbl.Color = Colors.LightGray
    lbl.Width = ListView1.Width
    txtHeight = StringUtil.MeasureMultilineTextHeight(lbl, "Ag")
    
    minItemHeight = 45dip
    txtPadding = 0.07 * minItemHeight
    minTextHeight = minItemHeight - 2 * txtPadding
    
    If txtHeight < minTextHeight Then
        lbl.TextSize = 100
        txtHeight = StringUtil.MeasureMultilineTextHeight(lbl, "Ag")
        Do Until txtHeight < minTextHeight
            lbl.TextSize = lbl.TextSize - 1
            txtHeight = StringUtil.MeasureMultilineTextHeight(lbl, "Ag")
        Loop
    End If
    
    lblHeight = Max(minTextHeight, txtHeight)
    lbl.Top = txtPadding
    ListView1.SingleLineLayout.ItemHeight = lblHeight + 2 * txtPadding
    lbl.Gravity = Gravity.CENTER_VERTICAL
    For i = 1 To 20
        ListView1.AddSingleLine(i & "  Test1 text1 Test2 text2 Test3 text3 Test4 text4 Test5 text5 Test6 text6")
    Next
End Sub
Hope this is what you are looking for.

Best regards.
 

Attachments

  • ListViewLabelHeight.zip
    5.8 KB · Views: 680
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Klaus, thank you so much for your help - I really appreciate it!

I am going to try adapt your example to put in my code module to be able to quickly use to set any ListView Height.

Have a great weekend!

Dave
 
Upvote 0
Top