shackra
shackra

Reputation: 376

How to limit the width of TextView programatically so it behaves as a marquee?

I'm creating a list of items from a JSON file to display them on my app. But I cannot set the TextView correctly so parts of longer texts are hidden without overflowing to multiple lines and behave as a marquee.

So far, this is my code:

val jsondata = Klaxon().parseArray<MTGSets>(jsonstr)
        for(mtgset: MTGSets in jsondata.orEmpty()) {
            val hlayout = LinearLayout(this)
            val setTitle = TextView(this)
            setTitle.text = mtgset.name
            setTitle.ellipsize = MARQUEE
            setTitle.maxWidth = 60.toPx()
            setTitle.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
            hlayout.addView(setTitle)
            cardsetlist.addView(hlayout)
        }

But the text breaks in multiple lines instead:

screenshot of my app showing the issue

EDIT

This is the kind of result I'm looking for but programatically, as you can see:

The result I'm looking to accomplish

The XML:

<TextView
                            android:id="@+id/textView4"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:ellipsize="marquee"
                            android:isScrollContainer="false"
                            android:marqueeRepeatLimit="marquee_forever"
                            android:singleLine="true"
                            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus gravida. " />

Upvotes: 0

Views: 65

Answers (1)

fweigl
fweigl

Reputation: 22038

textView.maxLines = 1

or

textView.singleLine = true

Upvotes: 1

Related Questions