B4J Question Howto Multiline Label dynamic height with xCustomListview?

fredo

Well-Known Member
Licensed User
Longtime User
Using this to measure the height of each text gives different values and adjusts the xCLV pane's height fine.

However, the height of Label1 stays constant, although the height of the panel is dynamically adjusted.

06-09-_2018_13-08-31.jpg

06-09-_2018_13-46-21.jpg

B4X:
Sub FillClv1
    CustomListView1.Clear
    Dim strText As String = ""
    For i=1 To 10
        strText = strText & " Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
        Dim panX As Pane = CreatePanel( ((i *8) +2) & " words. " & strText, CustomListView1.AsView.Width, "i" & i)       
        CustomListView1.Add(panX , "j" & i)
    Next
End Sub

Sub CreatePanel(Text As String, Width As Int, Value As String) As Pane
    Log("#-")
    Log("#-Sub CreatePanel, Text.len=" & Text.Length)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, 48dip)
    p.LoadLayout("Layout2")
    Label1.Text = Text
    '
    Dim intH As Int = Floor(jMeasureMultilineTextHeight2(fx.DefaultFont(15), Width, Text))
    Log("#-  x48, jMeasureMultilineTextHeight2, intH=" & intH)
    '
    ' ~- ~- ~- ~- ~- ~- ~- ~- ~- ~- ~-     
    
    ' Expected behavior: Adjust the height of Label1 dynamically to the value of intH
    
    ' Observed behavior: The height of Label1 is constant, although the height of the panel is dynamically adjusted.
    
    Label1.PrefHeight = intH  ' <--- wrong ???
    
    ' ~- ~- ~- ~- ~- ~- ~- ~- ~- ~- ~-     
    '
    p.Height = intH +30dip
    Return p
End Sub
'
Sub jMeasureMultilineTextHeight2(Font As Font, Width As Double, Text As String) As Double
    ' Erel --> https://www.b4x.com/android/forum/threads/measure-multiline-text-height.84331/
    Dim jo As JavaObject = Me
    Return jo.RunMethod("MeasureMultilineTextHeight", Array(Font, Text, Width))
End Sub
#if Java
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
public static double MeasureMultilineTextHeight(Font f, String text, double width) throws Exception {
  Method m = Class.forName("com.sun.javafx.scene.control.skin.Utils").getDeclaredMethod("computeTextHeight",
  Font.class, String.class, double.class, TextBoundsType.class);
  m.setAccessible(true);
  return (Double)m.invoke(null, f, text, width, TextBoundsType.LOGICAL);
  }
#End If


Is the assumption wrong that "Label1.PrefHeight = intH" sets the height of the label once the panel has been created?
 

Attachments

  • jClvItemHeights.zip
    9.2 KB · Views: 224

fredo

Well-Known Member
Licensed User
Longtime User
...see how AddTextItem is implemented...

07-09-_2018_07-42-29.jpg


I see. CreateLabel(...) creates a new label and calculates the height based on the scrollview content width.

B4X:
Private Sub CreateLabel(txt As String) As B4XView
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Alignment = DesignerLabel.Alignment
    lbl.Style = DesignerLabel.Style
    lbl.Font = DesignerLabel.Font
    lbl.WrapText = True
    lbl.Text = txt
    Dim jo As JavaObject = Me
    Dim width As Double = sv.ScrollViewContentWidth - 10dip
    lbl.PrefHeight = 20dip + jo.RunMethod("MeasureMultilineTextHeight", Array(lbl.Font, txt, width))
    Return lbl
End Sub
So far, so good.

Q1) Is the line "Dim item ..." necessary at the end of the sub? It looks like the local variable is not being used.
B4X:
    ...
    Dim item As CLVItem = GetRawListItem(Index)
    item.TextItem = True
End Sub

Q2) Is it possible to load a more complex layout and dynamically adjust the height of certain elements?
07-09-_2018_14-43-14.jpg
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is the line "Dim item ..." necessary at the end of the sub? It looks like the local variable is not being used.
It changes the TextItem property. It will affect the same CLVItem that is stored in CLV list.

The TextItem property is tested when the list is resized. It allows CLV to automatically resize text items.

Is it possible to load a more complex layout and dynamically adjust the height of certain elements?
Yes.
 
Upvote 0
Top