0

I want to make my textview automatically scrollable even if there is not so much text to fit into the screen. Also I didn't found any library which can scroll textview horizontally, only vertically.

I created my textview and here is my xml code:

<TextView
        android:id="@+id/ticker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="My text"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textSize="22sp" />

In my activity I use:

TextView ticker = findViewById(R.id.ticker);
ticker.setSelected(true);

The main thing is that text scrolls if text length is more that can fit screen but I need to make scrolling with any text. Any advices?

Akram Baratov
  • 143
  • 1
  • 3
  • 13

3 Answers3

1

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
0

You have to measure screen weight and put animation like a marquee. Please refer to below link for the solution.

https://stackoverflow.com/a/16371310/9121785

G.Ronak
  • 34
  • 4
0
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollHorizontally="true"
        android:text="This is an Example of Horizontal TextView Scrolling This is an Example of Horizontal TextView Scrolling This is an Example of Horizontal TextView Scrolling"/></HorizontalScrollView>

above code will help you to make a textview scollable