Android Question Listview label height

LucaMs

Expert
Licensed User
Longtime User
I would like to adjust the height of a label in a listview.

I found answers that recommend using the MeasureMultilineTextHeight of the StringUtils library, but I would like to avoid adding another library to my project.

Could someone extrapolate the function and add it to the Code snippets forum?

Thanks
 

LucaMs

Expert
Licensed User
Longtime User
I tried this code:
B4X:
Dim cnv As Canvas
Dim lbl As Label = ListView1.TwoLinesLayout.SecondLabel
cnv.Initialize(lbl)
Dim CanvasStringHeight As Float
CanvasStringHeight = cnv.MeasureStringHeight(Note, lbl.Typeface, lbl.TextSize)
Log("CanvasStringHeight = " & CanvasStringHeight)

but I get this error:
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
...


for the statement: cnv.Initialize(lbl)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Klaus already did it, or not?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Did you add this in code or the Designer?

You will need to set the height of the ListView, which you don't seem to have done in the code you've posted.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes, I set the heightS in the code.

I think I get the error because I should refer to a specific item, but there is not a function to get it:
ListView.GetItem returns text (though there is RemoveAt, damn :))

Manfred, that routine does the opposite, fits the text height of the label, unfortunately
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
btw: luca, you only can set ONE height for Label and one height for SecondLabel in Listview.
Every item in the listview will have the same height (ListView<X>.TwoLinesLayout.Label.Height+ListView<X>.TwoLinesLayout.SecondLabel.Height)
You cannot set an different heigth for every listview-item

and this will be adapted to fontsize, color and so on too. Every item will look the same. It´s like "Little boxes" :D
 
Last edited:
  • Like
Reactions: eps
Upvote 0

eps

Expert
Licensed User
Longtime User
btw: luca, you only can set ONE height for Label and one height for SecondLabel in Listview.
Every item in the listview will have the same height (ListView<X>.TwoLinesLayout.Label.Height+ListView<X>.TwoLinesLayout.SecondLabel.Height)
You cannot set an different heigth for every listview-item

and this we be adapted to fontsize, color and so on too. Every item will look the same.

Mostly yes you can only set the items once... however you can set two different sizes, but it is a bit of a fiddle.

Use TwoLinesLayout and TwoLinesAndBitmap - the two can be different, for the same ListView.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
So, i should use a scrollview, since I can not have different heights for the items in the listview (I had forgotten this).

But how to calculate the height of a label depending on the text in it? (I have the impression that I have read the answer in the site, months ago)
 
Upvote 0

eps

Expert
Licensed User
Longtime User
ScrollView is the way to go for lots of items which are different heights. or customlistview or tableview or ULV (chargeable from Informatix).
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You cannot set the canvas to ListView1.TwoLinesLayout.SecondLabel because its Width = -1 by default to fit the parent view.

You could define:
B4X:
Dim bmp as Bitmap
bmp.InitializeMutable(2, 2)
cnv.Initialize(bmp)
Using a small bitmap for the canvas to minimize memory consumption.

In CustomListView you have AddTextItem which adjusts the height automatically.
 
  • Like
Reactions: eps
Upvote 0
Top