Android Question [Solved] Horizontal customlistview with different text width in the item

asales

Expert
Licensed User
Longtime User
I tried to recreated this code from Erel to a horizontal list with texts in different widths, without success:
https://www.b4x.com/android/forum/threads/b4x-xui-minisearchview-autocomplete-field.93564/

I have a fixed list with some texts with differents sizes, like this:
B4X:
US Dollar
Argentine Peso
Australian Dollar
Boliviano
Brazilian Real
Euro

I don't need search or highlight in the text, only display this texts in a rounded panel that fit the width of the text:
clv_hor1.jpg


Thanks in advance for any help.
 

emexes

Expert
Licensed User
edit: in retrospect, doing it this way will likely end up being more complicated than using an already-scrollable control... but I'll leave the post here just in case it prompts ideas applicable to similar use cases (never hurts to have an extra spanner choice in the programming toolbox)

... fixed list with some texts with differents sizes
... need ... only display this texts in a rounded panel that fit the width of the text
If the list is small(ish) and fixed, then it might be quicker to just whip it up hardwired in Designer using labels with rounded corners, on a single panel so that you can move/scroll the entire block left and right as a single entity, eg:

upload_2019-7-23_15-52-43.png


upload_2019-7-23_15-49-57.png
 
Last edited:
Upvote 0

asales

Expert
Licensed User
Longtime User
The texts of the list are fixed, but they can change places.

Almost there with this code:
B4X:
For i = 0 To li.Size - 1
   Dim item As String = li.Get(i)
   CLV1.Add(CreateItem(item), i)
Next

Private Sub CreateItem(Text As String) As Panel
   Dim p As B4XView = xui.CreatePanel("")
   p.SetLayoutAnimated(0, 0, 0, 100dip, 36dip)
   p.LoadLayout("cell")  'a panel with a label inside
   
   Dim p1 As Panel = p.GetView(0)
   Dim lb As Label = p1.GetView(0)

   Dim xlbl As B4XView = lb
   fnt = xlbl.Font

   lb.Text = Text

   Dim TextWidth As Int = MeasurementCanvas.MeasureText(Text, fnt).Width

   p.SetLayoutAnimated(0, 0, 1dip, TextWidth + 10dip, 36dip)
   p1.Width = TextWidth + 8dip
   
   Return p
End Sub
 
Upvote 0
Top