'Android automatic horizontally scrolling TextView

I am trying to implement a single-line text view that will scroll automatically. But I unfortunatly cannot get it to work. The AutoScrollTextView is declared inside a LinearLayout (width and height = fill_parent). The class basically uses a Handler that calls itself to scroll by a given amount. I have simplified the code to only show a text view that should be scrolling by 5 pixels every second.

The log output is correct, the getScrollX() method returns the appropriate scrollX position.

If I don't call requestLayout(), nothing gets drawn. invalidate() has no effect.

Would anybody have a clue?

public class AutoScrollTextView extends TextView {

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        setEllipsize(null);
        setText("Single-line text view that scrolls automatically if the text is too long to fit in the widget");
    }

    // begin to scroll the text from the original position
    public void startScrolling() {
        scrollHandler.sendEmptyMessage(0);
    }

    private Handler scrollHandler = new Handler() {
        private static final int REFRESH_INTERVAL = 1000;

        public void handleMessage(Message msg) {
            scrollBy(5, 0);
            requestLayout();
            Log.debug("Scrolled to " + getScrollX() + " px");
            sendEmptyMessageDelayed(0, REFRESH_INTERVAL);
        }
    };
}


Solution 1:[1]

After these xml code as answered by @rajat

<TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>

We need to set

TextView tv=(TextView)findViewById(R.id.textview1);
  tv.setSelected(true); 

which finally made mine work

Solution 2:[2]

My solution works:

<TextView
     android:id="@+id/titolotxt"
     android:layout_width="..."
     android:layout_height="..."
     android:ellipsize="marquee"
     android:gravity="left"
     android:marqueeRepeatLimit="marquee_forever"
     android:singleLine="true"
     android:text="@string/titolo"/>

And the TextView have to be setted selected: textView.setSelected(true); by code in onCreate for example.

Solution 3:[3]

// this TextView will marquee because it is selected
TextView marqueeText1 = (TextView) findViewById(R.id.marquee_text_1);
marqueeText1.setSelected(true);

<TextView
    android:id="@+id/marquee_text_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    android:textSize="24sp"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true" />

Solution 4:[4]

In order to move text, besides adding attributes:

android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"

it is necessary for View to be in focus, so this can be done in two ways:

  1. Programmatically:

    tv.setSelected (true);
    
  2. Do everything in XML by adding requestFocus tag:

    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
         android:singleLine="true"
         android:marqueeRepeatLimit="marquee_forever"
         android:focusable="true"
         android:clickable="true"
         android:focusableInTouchMode="true"
         android:scrollHorizontally="true">
         <requestFocus />
    </TextView>
    

Solution 5:[5]

Hint: Your Text should be big in length otherwise it won't work for small-length text if it does not exceed your screen size.

<  TextView
        android:id="@+id/support"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" enter the large text that wont fit on screen "
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />

and in your activity

 TextView textView=findViewById(R.id.support);
        textView.setSelected(true);

Solution 6:[6]

Sometimes invalidate wont work until you call invalidate in main thread like follows:

handler.post(new Runnable() {

        @Override
        public void run() {
            yourView.invalidate();
        }
    });

Solution 7:[7]

setMovementMethod(new ScrollingMovementMethod()) with setHorizontallyScrolling(true) work in my case.

in .java:

tv = findViewById(R.id.textViewID);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setHorizontallyScrolling(true);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CinCout
Solution 2 Saurabh Bhandari
Solution 3 A.G.THAMAYS
Solution 4 Dragoljub Zivanovic
Solution 5
Solution 6 Vitaliy A
Solution 7