Question

I am not able to implement a Marquee Text View in android.

Yes, this question is similar to many other questions related to Marquee. I have read most of them. I tried implementing Marquee the way many of the answers are. But still my code doesn't work.

Please help me resolve the issue. I have tried everything. Nothing worked.

Thanks.

XML code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@color/mainBg"
    tools:context=".MainActivity" >

<TextView android:layout_width="fill_parent"
    android:id="@+id/marquee"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:lines="1"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:text="@string/home"
    android:fontFamily="monospace"
    android:textStyle="bold"
    android:paddingBottom="20dp" />
</LinearLayout>

JAVA Code :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView marqText  = (TextView)findViewById(R.id.marquee);
    marqText.setSelected(true);
}
Was it helpful?

Solution 2

Try adding android:singleLine = "true" to the TextView to keep the text on only one line.

OTHER TIPS

If TextView fill whole width you need to add these lines of code

<TextView
    .
    .
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

also do not forget to set selected for TexrtView as below

textview.setSelected(true);

and if TextView does not fill width the only thing is to call this method and pass TextView to it.

    fun addMarquee(textView: TextView) {
    textView.viewTreeObserver.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            val pixels = textView.measuredWidth - 1
            val params = textView.layoutParams
            params.width = pixels
            textView.layoutParams = params
            textView.isSelected = true
            textView.ellipsize = TextUtils.TruncateAt.MARQUEE
            textView.isSingleLine = true
            textView.marqueeRepeatLimit = -1
            textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })
}

NOTE: this method only work when textView width is wrap_content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top