How to trim text to fit label width in ListView?

CidTek

Active Member
Licensed User
Longtime User
I want to avoid wordwrap in my Listview and can't figure out a way to prevent text going over the pixel width of the label.
 

CidTek

Active Member
Licensed User
Longtime User
You can set the label width to be much larger than the ListView width. The text will not be wrapped.
Or you can use Canvas.MeasureString to calculate the actual string length and then cut it.

Thanks Erel, I went with the *really* extra wide label width to cover rotating hi res devices.

I like how sometimes the simple solution is often the best solution but it's good to know about Canvas.MeasureString though.
 
Upvote 0

LittleEddie

Member
Licensed User
Longtime User
On the Really extra wide label width option is ok except that the FastScroll Tab is no longer useable as it's out at the end of the Label and off the screen.

Ed
 
Upvote 0

CidTek

Active Member
Licensed User
Longtime User
On the Really extra wide label width option is ok except that the FastScroll Tab is no longer useable as it's out at the end of the Label and off the screen.

Ed

Good to know but my list is short enough I don't need FastScroll.
 
Last edited:
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try this, works for both labels:
B4X:
Dim r As Reflector
r.Target = ListeView1.TwoLinesLayout.Label
r.RunMethod2("setSingleLine", True, "java.lang.boolean")
r.RunMethod2("setEllipsize", "END", "android.text.TextUtils$TruncateAt")

r.Target = ListeView1.TwoLinesLayout.SecondLabel
r.RunMethod2("setSingleLine", True, "java.lang.boolean")
r.RunMethod2("setEllipsize", "END", "android.text.TextUtils$TruncateAt")
Best regards.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Thanks for your answer Klaus.

I tried the example you gave, but it doesn't work in my application. Maybe it's because I use the twolinesandbitmap?

I tried to put the reflector each time I create a new element in the listview, and I also tried to put it when I initialize the listview, but no luck.

Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I hadn't tested the code with a ListView, only with Labels and I supposed that it should work with ListView Labels.

Now I tested it and unfortunately the code doesn't work with ListView Labels.
Setting Elipsized to True has no effect.

@Erel,
Are the Labels in ListViews special ones ?

Attached my test code.

Best regards.
 

Attachments

  • TestsEllipsize.zip
    6.1 KB · Views: 307
Upvote 0
Top