Using Reflection to set MARQUEE on Label

thedesolatesoul

Expert
Licensed User
Longtime User
As I understand the Label is an extension of textview which exposes the setEllipsize method which allows text to scroll if it is too long.

The argument to the function is however an enumerated type and I do not know how to pass that through reflection.
TextView | Android Developers)



B4X:
Dim r As Reflector 
r.Target = lbl
r.RunMethod2("setEllipsize","MARQUEE", "java.lang.Object")

I have tried java.lang.Enum as well.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Globals
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout(1)
   Label1.Text = "this is a very long text."
   SetEllipsize(Label1, "END")
End Sub
Sub SetEllipsize(TextView As Label, Mode As String)
   Dim r As Reflector
   r.Target = TextView
   r.RunMethod2("setLines", 1, "java.lang.int")
   r.RunMethod2("setHorizontallyScrolling", True, "java.lang.boolean") 
   r.RunMethod2("setEllipsize", Mode, "android.text.TextUtils$TruncateAt")
End Sub
Note that all of these settings are required.
 
Upvote 0

ChrShe

Member
Licensed User
Longtime User
Wondering if I could get a little help with using this in a ListView.
If I step through, I see that everything is being called and that there are no errors popping up. However, neither the liText, nor the liDesc get modified. I end up with what's in the attached screenshot.
I've also attached the .bas file, in case that's needed.

Here's a snippet that shows basically what I'm doing. In my actual code, the WHListItemData parts are populated with text grabbed from a downloaded web page.:
B4X:
Sub Globals
   Type WHListItemData (liText As String, liDesc As String, liHTML As String)
End Sub
Sub FillListView
   Dim lvi as WHListItemData
   lvi.liText = "Some text that's longer than the ListView is wide."
   lvi.liDesc = "More textthat could be longer than the ListView is wide."
   lvi.liHTML = "Some HTML that's loaded into a webview when the item is tapped."

   lvWHappening.AddTwoLines2(lvi.liText, lvi.liDesc, lvi)

   Dim lab1 As Label
   lab1 = lvWHappening.TwoLinesLayout.Label
   lab1.Typeface = Typeface.SANS_SERIF
   lab1.TextSize = 18
   lab1.TextColor = Colors.Cyan
   SetEllipsize(lab1, "END")

   Dim lab2 As Label
   lab2 = lvWHappening.TwoLinesLayout.SecondLabel
   lab2.Typeface = Typeface.SANS_SERIF
   lab2.TextSize = 14
   lab2.TextColor = Colors.LightGray
   SetEllipsize(lab2, "END")
End Sub
Sub SetEllipsize(TextView As Label, Mode As String)
   Dim r As Reflector
   r.Target = TextView
   r.RunMethod2("setLines", 1, "java.lang.int")
   r.RunMethod2("setHorizontallyScrolling", True, "java.lang.boolean") 
   r.RunMethod2("setEllipsize", Mode, "android.text.TextUtils$TruncateAt")
   r.RunMethod2("setSelected", True, "java.lang.boolean")
End Sub

Thanks so very much!!!
~Chris
 

Attachments

  • ListViewscreen.png
    ListViewscreen.png
    12.1 KB · Views: 873
  • actCalendar.bas
    5 KB · Views: 358
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Is it possible to utilize this for a multi-line label, so that the ... appears at the end of the last visible line? Also, how do you display rest of the text that is replaced by the ... ?
 
Upvote 0

lazreg

Active Member
Licensed User
Longtime User
Here:
B4X:
Sub Globals
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout(1)
   Label1.Text = "this is a very long text."
   SetEllipsize(Label1, "END")
End Sub
Sub SetEllipsize(TextView As Label, Mode As String)
   Dim r As Reflector
   r.Target = TextView
   r.RunMethod2("setLines", 1, "java.lang.int")
   r.RunMethod2("setHorizontallyScrolling", True, "java.lang.boolean")
   r.RunMethod2("setEllipsize", Mode, "android.text.TextUtils$TruncateAt")
End Sub
Note that all of these settings are required.

i used this code , but the label doesn't move !!!!!!!!!!!!!!! reflection lib. ver. is 2.4
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
To scroll the text in Labels have a look at Scrolling Labels.

MARQUEE only truncates the end of the text WRONG
END truncates the end of the text with three dots
START truncates the beginning of the text with three dots
MIDDLE truncates the middle of the text with three dots
All are single line.

Attached a small demo program.

EDIT: 2013.10.28
Modified the code according to posts #17 - 20
MARQUEE scrolls the text when it's longuer than the Label width.
With the code in post #17 or post #20.
 

Attachments

  • LabelEllipsize.zip
    7.9 KB · Views: 439
Last edited:
Upvote 0

socialnetis

Active Member
Licensed User
Longtime User
Hey, I do have an effect when using MARQUEE, but the label needs to have certain focus (you can just mark it as "selected"), this is the code I use:
B4X:
Sub SetEllipsize(TextView As Label, Mode As String)
    Dim r As Reflector
    r.Target = TextView
    r.RunMethod2("setLines", 1, "java.lang.int")
    r.RunMethod2("setHorizontallyScrolling", True, "java.lang.boolean")
    r.RunMethod2("setEllipsize", Mode, "android.text.TextUtils$TruncateAt")
    r.RunMethod2("setSelected", True, "java.lang.boolean")
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Thank you for the information :)!
This code works also:
B4X:
    Dim r As Reflector
    r.Target = TextView
    r.RunMethod2("setSingleLine", True, "java.lang.boolean")
    r.RunMethod2("setEllipsize", Mode, "android.text.TextUtils$TruncateAt")
    r.RunMethod2("setSelected", True, "java.lang.boolean")
r.RunMethod2("setSelected", True, "java.lang.boolean") is the 'trick' !
I've updated the example in post #16.
 
Upvote 0
Top