Search code examples
androidmultithreadingtextviewmarquee

Text View not scrolling from left to right across screen when thread is running


I have a problem where my textview is supposed to scroll from left to right motion in one line, which does not happen when I run a thread. It runs perfectly when I comment my thread function, but then starts to buffer when the thread runs. The thread is only suppose to control the seekbar, so I'm not sure why it's affecting the textview element. I've provided my xml and activity. Any idea how to keep the thread, but still have a way to marquee the textview?

XML code for text view-

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long text goes here"
        android:textSize="25sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_centerHorizontal="true"
        android:textAllCaps="false"
        android:textColor="@android:color/black"
        android:layout_marginTop="7dp"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal" />

Relevant Activity functions-

 private fun validateReceivedValues() {
    val title = intent.getStringExtra("title")
    songNameText.text = title
    songNameText.isSelected = true  //this allows the marquee to occur
    artistNameText.text = artist
    totalTime = duration

    thread()  //commenting this out means the text will marquee, otherwise by calling this it will constantly buffer every second
}

private fun thread() {
    val musicMethodsHandler = Handler()
    val musicRun = object : Runnable {
        override fun run() {
            if (musicBound) {
                positionBar.max = totalTime
                val musicCurTime = musicSrv!!.getSeek()
                positionBar.progress = musicCurTime
                val elapsedTime = createTimeLabel(musicCurTime)
                elapsedTimeLabel.text = elapsedTime
                val remainingTime = createTimeLabel(totalTime - musicCurTime)
                remainingTimeLabel.text = "-$remainingTime"
            }
            musicMethodsHandler.postDelayed(this, 1000)
        }
    }
    musicMethodsHandler.postDelayed(musicRun, 1000)
}

Solution

  • I was able to fix this by putting the text view in its own layout so that it doesn't become a sibling of the progress bar. Hope someone finds this information useful.

    <LinearLayout
            android:id="@+id/marquee_text_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/logo">
            <TextView
                android:id="@+id/songname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Song"
                android:textSize="25sp"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@android:color/black"
                android:layout_marginTop="7dp"
                android:singleLine="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal" />
        </LinearLayout>