Using Reflection to set MARQUEE on Label

Discussion in 'Android Questions' started by thedesolatesoul, Mar 7, 2012.

  1. thedesolatesoul

    thedesolatesoul Expert Licensed 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)



    Code:
    Dim r As Reflector 
    r.Target = lbl
    r.RunMethod2(
    "setEllipsize","MARQUEE""java.lang.Object")
    I have tried java.lang.Enum as well.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Here:
    Code:
    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.
     
    Pooya1 and Merlot2309 like this.
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Thank you very much Erel. You are a :cool: guy!
    I will make a note of how to pass custom types that are not exposed in B4A to reflection.
     
  4. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Just a note:
    I also had to add the following to make it work even when not focused.
    Code:
    r.RunMethod2("setSelected"True"java.lang.boolean")
     
    Mashiane, Kwame Twum and Merlot2309 like this.
  5. ChrShe

    ChrShe Member Licensed 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.:
    Code:
    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
     

    Attached Files:

  6. Erel

    Erel Administrator Staff Member Licensed User

    You will not be able to use this solution with ListView. The label is only a model for the real labels created internally. You can use a ScrollView instead.
     
  7. Bill Norris

    Bill Norris Active Member Licensed 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 ... ?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that it is possible to use it with a multiline label.
     
  9. Roger Garstang

    Roger Garstang Well-Known Member Licensed User

    Docs say:

    "If setMaxLines(int) has been used to set two or more lines, END and MARQUEE* are only supported (other ellipsizing types will not do anything)."
     
  10. socialnetis

    socialnetis Active Member Licensed User

    Is there anyway in which the text scrolls itself horizontally if is longer than the label's width?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    There is no automatic way. You will need to implement the animation.
     
  12. socialnetis

    socialnetis Active Member Licensed User

    Its seems that the "MARQUEE" mode does what I want. So far its working well
     
  13. lazreg

    lazreg Active Member Licensed User

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

    lazreg Active Member Licensed User

  15. Erel

    Erel Administrator Staff Member Licensed User

    Please do not "bump" your posts.

    Setting the marquee property will cause the text to be truncated (with three dots). The text will not move.
     
  16. klaus

    klaus Expert Licensed 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.
     

    Attached Files:

    Last edited: Oct 28, 2013
    lazreg likes this.
  17. socialnetis

    socialnetis Active Member Licensed 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:
    Code:
    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
     
  18. klaus

    klaus Expert Licensed User

    What effect do you get ?
     
  19. socialnetis

    socialnetis Active Member Licensed User

    The text scrolls itself horizontally if is longer than the label's width. Which I think is what Lazreg is looking for.
     
  20. klaus

    klaus Expert Licensed User

    Thank you for the information :)!
    This code works also:
    Code:
    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice