21

I have a TextView which sits on the left side of the screen and is set with gravity="right" and it is set with SingleLine = "true."

If by some chance the text in this view gets too long I want it to simply disappear off the left hand side of the view. I thought the configuration below would do that but what actually happens is the the long string disappears completely, presumably down and outside of the view somewhere.

How can I create a simple text view that contains a single line of text and keeps its layout even when something unexpected happens? ... or even something predictable.

<TextView
    android:id="@+id/tempF"
    android:text="@string/tempF"
    android:layout_width="146dp"
    android:layout_height="wrap_content"
    android:textColor="#cccccc"
    android:textStyle="bold"
    android:textSize="92dp"
    android:fontFamily="serif"
    android:gravity="right"
    android:layout_marginTop="60dp"
    android:layout_gravity="top|left"
    android:singleLine="true"
    />
NFG
  • 639
  • 1
  • 5
  • 15

6 Answers6

43

This is the purpose of "ellipsize" - truncating a portion of text to indicate additional text.

In you case, you may simply need to add something like:

android:ellipsize="end"

or you might need to go deeper:

Automatically ellipsize a string in Java

Or look at this:

android ellipsize multiline textview

The idea behind extending the class is that you can customize the behavior of the TextView when the text content exceeds the space provided. For example, you can give it the appearance the it "bleeds over" by removing padding, etc. An ellipsis is an indicator that is commonly used to explain "there's more text that you can't see" - this is how it would look:

This is really a really long...

(the 3 periods are the ellipsis - your text goes the opposite direction, but it should still work)

With a custom TextView, you could change the "..." to nothing or anything else you want (even an image, which I've done).

You could also marquee the text:

TextView Marquee not working

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • I really appreciate the answer but I can see that this concept is not going to work. All variants of android:ellpsize have the same result, but even if they didn't the appearance of an ellipse would be only slightly better than the nothing that I have. I have to assume from this that I need to maybe cause the view to extend past the edge of the screen and hope for the best. – NFG Aug 11 '14 at 15:55
  • Yep that appears to be the only way to do it. Doesn't seem too kosher to me but whatever. android:layout_width="500dp" android:layout_marginLeft="-350dp" – NFG Aug 11 '14 at 16:05
  • Look at my revised comment. You probably need a custom TextView. Or the use of marquee might help. Scrolling off the screen might do some unpredictable things. – Jim Aug 11 '14 at 16:08
  • Yes this is most likely the tidier solution. Thanks for the idea. IMO this is a problem that should not exist, but at least it is one with a solution. Thanks. – NFG Aug 11 '14 at 17:05
10

Add 2 properties in xml

android:singleLine="true"

android:ellipsize="end"
Robert
  • 5,278
  • 43
  • 65
  • 115
siddhartha shankar
  • 1,450
  • 13
  • 16
8

You should play with ellipsize attribute of the TextView.Check below:

<TextView
    android:id="@+id/tempF"
    android:text="@string/tempF"
    android:layout_width="146dp"
    android:layout_height="wrap_content"
    android:textColor="#cccccc"
    android:textStyle="bold"
    android:textSize="92dp"
    android:fontFamily="serif"
    android:gravity="right"
    android:layout_marginTop="60dp"
    android:layout_gravity="top|left"
    android:singleLine="true"
    android:ellipsize="end"
    />
rupesh jain
  • 3,410
  • 1
  • 14
  • 22
0
<TextView
    android:id="@+id/tv_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/colorGray"
    android:textSize="@dimen/_18dp"
    android:padding="@dimen/_2dp"
    android:singleLine="true"
    android:ellipsize="end"
    android:text="@string/desc" />
Manaus
  • 407
  • 5
  • 9
Sunil
  • 3,785
  • 1
  • 32
  • 43
0

Since android:singleLine is deprecated. We can use this line android:maxLines

Can do like this.

android:ellipsize="end"
android:maxLines="1"
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
-1
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

It works.

Hanisha
  • 849
  • 10
  • 8