setEllipsize - Make a Scrolling Marquee, using scrollHorizontally, in a TextView

How to Make a Scrolling Marquee, using scrollHorizontally, in a TextView

setEllipsize was added in API Level 1
Related XML Attribute: android:ellipsize - Make a Scrolling Marquee, using scrollhorizontally, in a TextView


1. Add a TextView.

2. Limit the number of lines and enable horizontal scrolling by adding scrollHorizontally to your TextView. 

    <TextView

        ...
        android:scrollHorizontally = "true"
    />


3. In the main.xml file, add the below line

  <TextView

    ....
       android:text="This is a really, really, really long line of text so you can see it scroll.This is a really, really, really long line of text so you can see it scroll."
  />

4. Find the TextView using ID, in the MainActivity.java file. 

5. Add this import to the imports section. 


         import android.text.TextUtils.TruncateAt;

6. Add the below code to the onCreate method.

myTextView.setEllipsize(TruncateAt.MARQUEE);

7. If no other widgets are on the screen, this TextView may have focus by default, and may scroll automatically. However, if there are more widgets on the screen, when another widget gets focus, the scrolling will stop on the TextView because it has lost focus. To keep the focus on the TextView there are four options: (Focus can be set before or after setEllipsize. Order does not matter.)

      a. add requestFocus - Set Focus to a TextView, using requestFocus, in Java

        b. add setSelected(true) - Set Focus to a TextView, using Java - * works, but changes the color of the text because the              text is selected **

      These do NOT work
        c. add  android:state_selected - Set Focus to a TextView, using state_selected, in XML - **does not work **



        d. add  requestFocus - Set Focus to a TextView, using requestFocus, in XML - ** does not work **

8. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html