TextView android:ellipsize="marquee" not working as expected

Andre picture Andre · Jul 1, 2013 · Viewed 21.6k times · Source

I have some custom elements in a ListView and each element is, up to know, just a TextView in a LinearLayout. I would like the text inside the TextView to be a single line scrolling horizontally when the text is too long. I read many posts on this and I came up with a solution that was supposed to work, but instead of having the full text scrolling I have the text cut to the length of the containing View and ended with the three dots. I don't want the three dots but the entire text needs to be scrolled.

This is the layout of the items in the list (list_item.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_item_selector"
    android:orientation="horizontal"
    android:paddingBottom="7dp"
    android:paddingLeft="15dp"
    android:paddingTop="7dp" >

    <TextView
        android:id="@+id/listText"
        style="@style/Text_View_Style_White"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever" 
        android:padding="3dp"

        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:textColor="@drawable/list_item_text_selector" />


</LinearLayout>

I tried also with android:focusable="true", android:scrollHorizontally="true" and android:maxLines="1" attributes but none of them is working. In the getView() method of the adapter (which extends a BaseAdapter) I use the setSelected(true) method on the TextView, before returning the View.

I can't figure out what the problem is. Any help will be highly appreciated.

Answer

kdenney picture kdenney · Apr 1, 2014

I had the same problem today and was able to figure it out. None of the listed solutions here worked so I thought I'd share what fixed it for me.

TL;DR: If you are dynamically setting the text of the TextView, try setting the required "marquee" properties in code instead of in the layout xml file.

Longer version: In my case, I had a GridView with an adapter and a TextView in each item. Some of the item's had text that was too long to fit in its "cell" of the grid, and thus I wanted all items that were too long to scroll a few times. Being that the TextView is in a GridView with an adapter, the text was obviously being set in code, from the current item of the adapter.

Through much painful debugging, I finally had the idea to set all of the marquee settings in code instead of in the layout xml file. This caused the 3 dots (...) to finally go away from the TextView and begin scrolling instead.

Here's what my layout file looks like now: (note that none of the properties listed above are set here)

<TextView
android:text="Placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:id="@+id/name"
/>

And here's what my adapter code looks like:

nameView.setText(name);
nameView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
nameView.setSingleLine(true);
nameView.setMarqueeRepeatLimit(5);
nameView.setSelected(true);