Android Question Expand a Label in CustomListView

Smee

Well-Known Member
Licensed User
Longtime User
Hi All

I have read everything I can on the forums including this link https://www.b4x.com/android/forum/t...matically-set-label-height.77849/#post-493054
but i am unable to find anything that works. I have actually tried to increase the size (Height) of the panel manually but it does not work either.

I have a clv with an image and 2 labels. They all display fine when anything is added but I cannot get the labels to increase their size automatically. Sometimes the label will have 2 lines of text and sometimes 4.
The code has been taken from the example and is

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
      Activity.AddView(p, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    p.RemoveView
    Label1.Text = Text
    lblMessage.Text=msgTitle & CRLF & msgBody
'    clvMessage.AddTextItem(msgTitle & CRLF & msgBody,"")
    If NextRecord Mod 2 = 0 Then
            lblMessage.Color=Colors.Blue
            Label1.Color=Colors.Cyan
    Else
            lblMessage.Color=Colors.White
            Label1.Color=Colors.White
    End If
'    If NextRecord=23 Then Log(lblMessage & msgBody)
    iv1.SetBackgroundImage(bmpName)
    Return p
End Sub

I have tried .clvMessage.AddTextItem but that just puts a text label on the panel without the other views.
Can anyone point me in the direction of a working example to fix this. Also I do not understand the logic behind adding the panel (p) and then removing it. I would have thought that all the views on the panel would be removed too. I am obviously missing something here.
Appreciate any help with this
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Also I do not understand the logic behind adding the panel (p) and then removing it.
The panel size should be set before you call Panel.LoadLayout. The purpose of this code is to set the panel's size.

You can use this code to set the label's height:
B4X:
Dim su As StringUtils
Dim minHeight As Int = su.MeasureMultilineTextHeight(lbl, Text)
lbl.Height = Max(50dip, minHeight)
The next step is to resize the panel's height based on this height.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the reply,I will give this a go and see what happens
Thanks again
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
The panel size should be set before you call Panel.LoadLayout. The purpose of this code is to set the panel's size.
The next step is to resize the panel's height based on this height.

I have tried to set the panel's height but I am unable to see what I am missing. As I understand it this should set the panel height
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.

    Activity.AddView(p, 0, 0, Width, Height)

I have tried increasing the height by calculating a new height. I have even changed the code to test it to become

B4X:
    Activity.AddView(p, 0, 0, Width, Height+200dip)

The panel height does not change no matter what value I attribute to the height
What the hell am I missing here?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
This is the code as it is now

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    Activity.AddView(p, 0, 0, Width, Height+500dip)
    p.LoadLayout("CellItem")
    p.RemoveView
    Label1.Text = Text
    lblMessage.Color=Colors.White
    Label1.Color=Colors.White
    lblMessage.Text =msgTitle & CRLF & msgBody
    iv1.SetBackgroundImage(bmpName)
    Return p
End Sub

This is the line calling it
B4X:
        clvMessage.InsertAt(0, CreateListItem(DisplayTime , clvMessage.AsView.Width, 70dip),90dip, $"Item !!!"$)

This is the result from sending 4 lines
 

Attachments

  • Screenshot_2017-06-06-20-38-17.png
    Screenshot_2017-06-06-20-38-17.png
    29.1 KB · Views: 260
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
The third parameter to CLV.InsertAt is the height parameter. It is set to 90dip in your code.

There are two different heights:
1. The item's height.
2. The panel's height.

Both should be the same.
Thanks Erel,
have tried that in earlier test but it does not fix the problem. I will play with it for a couple of hours and if I still cannot solve it I will post back
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Using your stringutils code I am passing 2 different heights to the CreateListItem sub. The first is 1728 and the second is 4626. However the labels are the same height. I am unable to work out how to change the height of the panel
B4X:
        clvMessage.InsertAt(0, CreateListItem(DisplayTime , clvMessage.AsView.Width, Label1.Height+10dip),Label1.Height+10dip, $"Item !!!"$)
I thought from your earlier post that both would be passed as parameters so how does it change the panel height? Which is not happening. the code for the display sub is
B4X:
Sub CreateListItem(Text AsString, Width As Int, Height As Int) AsPanelDim p AsPanel
p.Initialize("")Activity.AddView(p, 0, 0, Width, Height)
p.LoadLayout("CellItem")
p.RemoveView
Label1.Text = Text
lblMessage.Color=Colors.White
Label1.Color=Colors.White
lblMessage.Text =msgTitle & CRLF & msgBody
iv1.SetBackgroundImage(bmpName)Return pEnd Sub

I should mention that the label1 in the calling routine is a local variable and the Label1 in the sub routine is defined in the designer in CellItem

Thanks for any replies
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks Erel I will tomorrow, it's getting too late now. However I have noticed some thing very strange. I set all the values of the panel to 0 and nothing changes. The labels and images still appear as before with the same size. Is this normal?
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    Dim su As StringUtils
    p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.

    Activity.AddView(p, 0, 0, 0, 0)
    p.LoadLayout("CellItem")
    p.RemoveView
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Hi @Erel
Attached a small project that demonstrates the problem. Thanks for your assistance
 

Attachments

  • test.zip
    12.7 KB · Views: 226
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Did you try cleaning your project before running it? (Under Tools > Clean Project)
I think i may have run the old project, I thought it was strange that @klaus screenshots were so different to mine. Going to try it again
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
@klaus Thanks very much that works now :) :) :)

I see it works but I do not understand how it works properly when all the Panel dimensions are set to 0.

B4X:
p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.

    Activity.AddView(p, 0, 0, 0, 0)
    p.LoadLayout("CellItem")

Can anyone explain this?

Many Thanks anyway
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is easier to set the layout with the visual designer. You don't need to do any change to the layout in your code.

See the attached project.
The relevant code is:
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
   Dim p As Panel
   p.Initialize("")
   Dim stubLabel As Label 'Only used for the string measurement
   stubLabel.Initialize("")
   p.AddView(stubLabel, 0, 0, Width, 0)
   Activity.AddView(p, 0, 0, Width, Max(60dip, su.MeasureMultilineTextHeight(stubLabel, Text)))
   p.LoadLayout("CellItem")
   lblMessage.Text = Text
   stubLabel.RemoveView
   p.RemoveView
   Label1.Text = DateTime.Time(DateTime.Now)
   Return p
End Sub

CellItem layout:
SS-2017-06-07_09.57.24.png


Designer script:
B4X:
iv1.VerticalCenter = 50%y
Label1.VerticalCenter = 50%y
 

Attachments

  • test1.zip
    12.5 KB · Views: 218
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Erel,
Your code is interesting!
I have only one problem:
In the stubLabel you calculate the label height with the whole CustomListView width.
But the text is in lblMessage which is smaller.
This means that if a line in lblMessage is longer than lblMessage.Width the height is wrong.
That's why I recalculated the layout in CreateListItem.

upload_2017-6-7_12-37-53.png
upload_2017-6-7_12-39-18.png
 
Upvote 0
Top