Android automatic horizontally scrolling TextView

AndroidScrollTextview

Android Problem Overview


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);
		}
	};
}

Android Solutions


Solution 1 - Android

If you don't need to sub-class the TextView, you can try this in your layout file:

    <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"/>

Also, in your code use the following:

findViewById(R.id.serviceColorCode).setSelected(true);

[Answer edited based on comments]

Solution 2 - Android

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 3 - Android

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 4 - Android

// 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 5 - Android

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 - Android

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 7 - Android

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

handler.post(new Runnable() {

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

Solution 8 - Android

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

in .java:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVincent Mimoun-PratView Question on Stackoverflow
Solution 1 - AndroidRajathView Answer on Stackoverflow
Solution 2 - AndroidSujeet Kumar MehtaView Answer on Stackoverflow
Solution 3 - Androidalfo888_ibgView Answer on Stackoverflow
Solution 4 - AndroidA.G.THAMAYSView Answer on Stackoverflow
Solution 5 - AndroidDeepak PradhanView Answer on Stackoverflow
Solution 6 - AndroidDragoljub ZivanovicView Answer on Stackoverflow
Solution 7 - AndroidVitaliy AView Answer on Stackoverflow
Solution 8 - AndroidAlex IPView Answer on Stackoverflow