8

I really cannot figure out why, but I am only able to get ellipsize working on maxLines=2 and more. I am displaying a few words of description and then a long string with no spaces.

This is how the TextView looks like:

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#757575"
                android:text="@string/gcm_not_registered"
                android:maxLines="1"
                android:ellipsize="end"
                android:id="@+id/login_gcmRegistrationTextView"/>

I then programatically set a text to it, but depending on the maxLines limitation, I get two different results:

maxLines=1 maxlines=2

The only thing that changed was the maxLines, why isn't the line filled in the first picture as well?

Martin Melka
  • 7,177
  • 16
  • 79
  • 138

3 Answers3

8

There are two ways to fix it:

  1. Try to change android:ellipsize="end" attribute to android:ellipsize="marquee".
  2. Try to remove android:maxLines="1" android:ellipsize="end" attributes and add android:singleLine="true" attribute.
erakitin
  • 11,437
  • 5
  • 44
  • 49
  • The 2) worked. But I wonder why, when I didn't say I wanted the text to be ellipsized, it shouldn't be. I also read that it is deprecated, but looking all over the documentation I can't find a word of it. So thanks. – Martin Melka Jun 22 '14 at 14:12
5

This code works for me:

In the xml add:

  • Attribute ellipsize: marquee
  • Attribute lines: 1

In java:

<yourTextView>.setHorizontallyScrolling(true);
<yourTextView>.setSelected(true);

If there is another item that request the "focus" you lose the marquee effect. The textView need the state selected to prevent this.

wendigo
  • 1,973
  • 2
  • 17
  • 21
0

Actually the problem is with the spannable text, if you set spannable text this wouldn't work. Other than this below code works for me

<TextView
    android:id="@+id/tv_second"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:fontFamily="sans-serif-condensed"
    android:textColor="#b3277b"
    android:background="#f7ecff"
    android:layout_below="@id/tv"
    android:text="This is first line\nThis is second line\nThis is third line"
    android:ellipsize="end"
    android:maxLines="2"
    />
Asheesh
  • 565
  • 6
  • 21