Ellipsis

JesseW

Active Member
Licensed User
Longtime User
How can I make a label show the ellipsis character ... when the text is too large to fit inside it?

thanks... :)
 

JesseW

Active Member
Licensed User
Longtime User
Perhaps I wasn't clear.. When the text/string data is too large to fit inside a label, it is simply truncated. I am looking for a way for the truncation to end with an ellipsis (...) instead of just being chopped off, so the user of my app can tell explicitly there is more data than what is shown.

For example, if the text of a label is "now is the time for all good men" but the label is wide enough to show only "now is the time for a", it will appear as such

[now is the time for a]

but I'd like it to appear as

[now is the time fo...]

With a little research I found ellipsis in textview and jlabels, but no mention of label views. And B4A doesn't have textview views that I can use, so I'm stuck. I tried to setellipsize in a label using a reflector object, like you would in a textview, but it doesn't work as I wrote it :(

Here is my attempt:

B4X:
   Dim p As Panel
   Dim l As Label
   l.Initialize("trans")
   l.Text = "now is the time for all good men to come to the aid of their country"
   l.Gravity = Gravity.LEFT
   l.TextSize = 20
   l.Color = Colors.Transparent
   l.TextColor = Colors.White
   l.Tag = p
   Dim obj As Reflector
   obj.Target = l
   'the definition is setEllipsize(TextUtils.TruncateAt)
   'TextUtils.TruncateAt is an Enum, which End is listed first
   obj.RunMethod2("setEllipsize", "0", "java.lang.int")
   p.AddView(l, 3%x, 0, 65%x, 30dip)

The error I'm getting on the device is:

B4X:
An error has occurred in sub:
main_addtrans(B4A line: 62)
obj.RunMethod2("setEllipsize", 
"0", "java.lang.int")
java.lang.
NoSuchMethodException:
setEllipsize
Continue?
[Yes] [No]

Can anyone help with a work-around??

Help???
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
And B4A doesn't have textview views that I can use, so I'm stuck.
I wanted this and Erel pointed out that a Label can be used as a generic TextView in the same way that a View can be used as a generic view.

You are getting a NoSuchMethodException because

obj.RunMethod2("setEllipsize", "0", "java.lang.int")

is looking for a "setEllipsize(int) method which doesn't exist. As you say the correct one is setEllipsize(TextUtils.TruncateAt)

The correct invocation would be something like.

obj.RunMethod2 ("setEllipsize", "END", "android.text.TextUtils.TruncateAt")

I haven't tried this for real as I've just got back from a few days away and I've got other stuff to catch up with, but it should work as the Reflection library does support Enums.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Thanks Andrew.. I noticed while in your help system that labels are B4A wrappers for textviews. I will try your suggestion when I get home.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Andrew, I kept my test as simple as possible. Here is what I tried:

B4X:
Sub Globals
   Dim l As Label
   Dim r As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
   l.Initialize("")
   l.Text = "Now is the time for all good men to come to the aid of their country."
   r.Target = l
   r.RunMethod2 ("setEllipsize", "END", "android.text.TextUtils.TruncateAt")
   Activity.AddView(l, 50%x - 10dip, 50%y, 20%x, 30dip)   
End Sub

The RunMethod2 is a cut and paste from your example above, with the object name changed.

and here is the error I received...

ellipsizeerror.jpg


This was on the emulator running HVGA os v1.6

thanks for your help..
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Thanks again Andrew for your time and trouble. Your correction made the error go away, but there is still no ellipsis at the end. In fact, there is no difference with the runmethod intact or remarked out. using setEllipsize() seems to make no difference at all on a B4A Label.

I think I need a deeper understanding of Java and Android before I see something I want and grab ahold and run with it, expecting it to work the way I want it to ... Obviously there is more to this subject than meets the eye initially. I will use Erel's suggestion and programmatically test, and adjust, the text in the label field. It's an old trick I know well from my VB days.

Thanks again...
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
but there is still no ellipsis at the end.
It worked for me cutting and pasting your exact code, just replacing the "." with a "$". It actually gave me two lines in the label with the ellipsis at the end. Reducing the height of the label showed a single line, again with the ellipsis at the end. It was the same both on the emulator set to 240, 320,1 and my 480,800,1.5 phone.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Hmmm... I had noticed the label was 2 lines so I decreased the label height to 20dip before I tested it. Well, there is hope. I need to figure out how to enforce a single line label. Should I make the height the same as the textsize?
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Andrew, finally got it!!! I had to use a RunMethod("setSingleLine") before the other runmethod, and now it works flawlessly... Many thanks!!!

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim l As Label
   Dim r As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
   l.Initialize("")
   l.Text = "Now is the time for all good men to come to the aid of their country."
   r.Target = l
   r.RunMethod("setSingleLine")
   r.RunMethod2("setEllipsize", "END", "android.text.TextUtils$TruncateAt")
   Activity.AddView(l, 25%x, 50%y, 53%x, 20dip)   
End Sub
 
Upvote 0
Top