Search code examples
androidtextviewandroid-linearlayoutword-wrap

Fit TextView text on one line with WRAP_CONTENT LayoutParams


Say I have a TextView with some dynamic text. This TextView is being placed inside a LinearLayout with WRAP_CONTENT LayoutParams for both width & height.

Problem is that some of the text is - breaking onto second(or third) line - wrapping. I want to avoid this.

I have tried to replace all ' '(space chars) with non-breaking space character. This hasn't helped. The text still breaks. I also cannot set the TextView's maxLines=1 because there are some newline chars in the String-data.

I am not sure if any code samples need to be included. If there;s something specific, please do ask me in a comment.

Any help will be greatly appreciated :~)


Solution

  • Giving the TextView the attribute android:singleLine="true" will constrain the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines. Without the android:ellipsize attribute, the text will be horizontally scrollable if it doesn't all fit on the screen. With the android:layout_width="wrap content" attribute given to the TextView's parent layout, it should resize itself accordingly.