Jay Wick
Jay Wick

Reputation: 13795

Prevent TextView from wrapping in parent

How do I prevent a TextView, or any visual object, from wrapping to the screen and instead have them chopped off the sides? Is there some XML attribute or code to do this or is it impossible to have anything overflow from the screen?

For example, you have this:

text being wrapped

But you really want this:

text being cropped

Any ideas?

Upvotes: 43

Views: 30214

Answers (5)

Pentium10
Pentium10

Reputation: 208042

check out android:maxLines="1" and if you want to add ending ... add also android:ellipsize="end"

<TextView
   android:id="@+id/name"
   android:text="i want this to crop not wrap"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:maxLines="1"
   android:ellipsize="end" />

android:singleLine="true" was deprecated in API Level 3.

Upvotes: 93

user2318360
user2318360

Reputation: 11

textView.setHorizontallyScrolling(true); worked for me.

Upvotes: 1

Eugene Kardash
Eugene Kardash

Reputation: 360

And in java code it's:

textView.setSingleLine();

Upvotes: 2

lbedogni
lbedogni

Reputation: 8007

You're looking for android:singleLine="true".

Upvotes: 11

Robby Pond
Robby Pond

Reputation: 73494

Have you checked out android:scrollHorizontally. There is also a HorizontalScrollView if that doesn't work.

Upvotes: 5

Related Questions