Disable Word Wrap in an Android Multi-Line TextView
Asked Answered
I

4

31

I am working on a Month View with an advanced swipe (requires current, next and previous to be loaded to allow each view to stick to your finger) which means I have many views which causes things to be a little bit slow.

            |          | #<-- screen bounderies
 ' previous ' current  '   next   ' #<-- three months loaded
    ' previous ' current  '   next   ' #<-- three months when the user drags their finger

Because of this I want to represent multiple events in a single TextView. When the user taps one of the days on the Month View (small screen), it will open the day view for that day.

On each day in the month view (typically a single day gets one seventh of the width of the screen and a little less than one seventh vertically) I would like to avoid this

|    31|
|10a:  |
| Testi|
|ng    |
|11a:  |

Instead I want

|    31|
|10a:  |
| Testi|
|11a:  |
| Anoth|

Notice that the ng is cut off instead of wrapping to the next line. This is what I am looking for.

Icelander answered 28/2, 2011 at 18:48 Comment(2)
https://mcmap.net/q/378661/-prevent-textview-from-wrapping-in-parentMayle
It needs to support line returns while still prohibiting word wrap.Icelander
S
49

In Java:

setHorizontallyScrolling(boolean) 

In theory, android:scrollHorizontally should do the same in XML, but there is a bug in android that stops it working.

Streamliner answered 8/10, 2012 at 16:6 Comment(1)
This stops the wrapping but I also had to add setMovementMethod(new ScrollingMovementMethod()) to the TextView in order for the horizontal scrolling to work. And android:scrollbars="horizontal" in the layout file to show the scrollbar.Chromoplast
W
18

android:singleLine-----this is for xml

setTransformationMethod(TransformationMethod)-----this is for java

Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.

I'm not sure if this will work, but it worked for me when I wanted my TextView not to wrap in my ListView

Whap answered 30/3, 2011 at 14:51 Comment(2)
Sorry, this answer does not look sufficient. The above boxes with 5 lines are a single textview. It needs to support line returns while still prohibiting word wrap.Icelander
This is not answering the question at all. I don't understand why this has been upvoted so much.Aesop
K
2

Currently the best answer for XML is

android:maxLines="1"

Because android:singleLine is now depreciated

Kampala answered 4/8, 2016 at 11:48 Comment(0)
G
-1

in java code

textView1.setMovementMethod(new ScrollingMovementMethod());

in XML file

android:minLines="5"

does the trick.

Goldeneye answered 13/9, 2017 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.