Android: Autoscrolling text in TextView

I created a series of tabs with a fixed width of 100 pixels. The tab contains an image with text below it. If the text is too long to fit, I want it to scroll automatically. I need only one line. I tried the following, but that did not work. I am using Android 2.3:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="3dp"
    android:layout_marginTop="3dp"
    android:background="#ff737373"
    android:gravity="center"
    android:minWidth="64dp"
    android:orientation="vertical"
    android:padding="3dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="tabImage" >
    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:maxWidth="100px"
        android:tag="tabCaption"
        android:textColor="#ffd9d9d9"
        android:textSize="16sp" />

</LinearLayout>

Any idea why this is not working? I came across a solution from another post, and there the user indicated that it was working.

+5
source share
4 answers

In your activity you need to add whether you want to highlight text

  TextView tv=(TextView)findViewById(R.id.textview1);
  tv.setSelected(true);
+11
source
<TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:gravity="center"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="Auto text scroller" 
        android:textSize="50sp"
        />
0
source

Remember that if the text is shorter than the width of the borders defined for it, it will NOT scroll. In this case, you can expand the text with something like:

-. ,,

String charsInBreak = "   ";
 while (bounds.width() < this.m_width)
 {
    m_text = (m_text + charsInBreak + m_text);
    paint.getTextBounds(m_text, 0, m_text.length(), bounds);
 } 

And if you want your text to stand out forever:

m_textView.setMarqueeRepeatLimit(-1);
0
source

All Articles