0

I have a TextView in my app that is defined with marquee_forever:

                <TextView
                android:id="@+id/networkNameTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/wefiPrimaryColor"
                android:textSize="15sp"
                android:textStyle="bold" />

It works perfectly on Xiaomi Android 8.1 but on a Galaxy Note Android 10 it doesn't work. Instead I get android:ellipsize.

Is there a new parameter required or is this a problem with Android 10 or Galaxy Note?

Reza Abedi
  • 419
  • 4
  • 16
theblitz
  • 6,683
  • 16
  • 60
  • 114
  • 1
    Not an OS version thing i guess . have you tried some of tricks mentioned [here](https://stackoverflow.com/questions/3332924/textview-marquee-not-working)? – ADM Dec 22 '20 at 12:04
  • I tried the ones that involve changes to the XML. I'll try the rest and see what happens. – theblitz Dec 22 '20 at 12:10
  • Seems that adding tvMarquee.setSelected(true); does the trick. NO idea why. – theblitz Dec 22 '20 at 12:48
  • That's why I said trick in first place . Because I am not sure either have to look at the source code . Pls mark it as duplicate since your problem is solved . – ADM Dec 22 '20 at 12:50
  • Not often you get to vote to close your open question :) – theblitz Dec 22 '20 at 13:03
  • Does this answer your question? [TextView Marquee not working](https://stackoverflow.com/questions/3332924/textview-marquee-not-working) – ADM Dec 22 '20 at 13:40

1 Answers1

0

You have two ways to do that. 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.

Reza Abedi
  • 419
  • 4
  • 16