Fit TextView text on one line with WRAP_CONTENT LayoutParams
Asked Answered
P

3

5

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 :~)

Penhall answered 6/2, 2015 at 1:41 Comment(2)
check out [this][1] answer to a similar question [1]: #2508298Wideangle
@JoeB. Hi Joe, thanks for the suggestion, but I don't want to ellipsize the text if its long. I would like the container(in this case, a LinearLayout) to expand and accommodate the TextView's width. I am not sure how to go about this. Can you help?Penhall
W
5

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.

Wideangle answered 6/2, 2015 at 5:38 Comment(0)
D
6

@Joe B. Answer is right. but android:singleLine="true" is deprecated. You need to use

android:maxLines="1"
Derayne answered 11/10, 2017 at 5:26 Comment(0)
W
5

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.

Wideangle answered 6/2, 2015 at 5:38 Comment(0)
O
2

You are looking for

android:singleLine="true" 
Outfoot answered 6/2, 2015 at 5:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.