Textview marquee programmatically

Mark James picture Mark James · Jan 29, 2017 · Viewed 8.9k times · Source

Trying to populate an textview(s) from an array. I managed to get the desired effect via XML via the code below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/marque_scrolling_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
        android:textSize="24sp" />
</LinearLayout>

But I need several of these in an array - so I believe creating the textviews programmatically may be the answer, but cant get them to marquee. This is the code im working on.

String[] strings = {"Mark", "James", "Paul"};
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);


        for(String s : strings)
        {
             TextView newTextView = new TextView(this);
             newTextView.setText(s);
             newTextView.setSingleLine(true);
             newTextView.setEllipsize(TextUtils.TruncateAt.END);       
             newTextView.setHorizontallyScrolling(true);
             newTextView.setLines(1);
             newTextView.setMarqueeRepeatLimit(-1);
             newTextView.setSelected(true);  
             newTextView.setTextSize(24);
             layout.addView(newTextView);
        }

Answer

ZeroOne picture ZeroOne · Jan 30, 2017

Try this.. It work

TextView testing = (TextView) this.findViewById(R.id.testing);
testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
testing.setMarqueeRepeatLimit(-1);
testing.setSingleLine(true);
testing.setSelected(true);