TextView android:ellipsize=“marquee” not working as expected

雨燕双飞 提交于 2019-12-03 10:48:56

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);

Key is here to do setSelected(true); on text view.. Of course

android:ellipsize="marquee"
android:scrollHorizontally="true"

need to be set in xml as well. Without all of this marquee will not happen. Ever.

As @iDroid Explorer pointed out, posting and accepting the answer can be of help to someone else. Adapting from my reply to Tom's comment:

I found the solution to my problem. After some attempts I've realized that the problem was the android:selectAllOnFocus="true" line (Read @Tom's explanation for probabale reason). I just removed that line and now everything is working very well, the text is complete and scrolling like desired when it is too long for the containing view.

Either put your LinearLayout or your TextView inside of a HorizontalScrollView. This will allow the text to be displayed in full and allow for scrolling right and left.

Try setting your layout_width to "match_parent". The TextView needs a specific width for the ellipsis to work and won't work with "wrap_content".

I was Facing Same Issue With my textView Now got reason why it is showing ...at the end of text.!

               <TextView
                android:id="@+id/MarqueeText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="2dp"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:maxLines="1"
                android:textColor="@color/text_gray"
                android:textSize="14sp"
                android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END" />

and After Initialization In my Activity

MarqueeText.setSelected(true);

I found the solution to my problem. After some attempts I've realized that the problem was the android:textIsSelectable="true". I just removed that line and now everything is working very well, the text is complete and scrolling like desired when it is too long for the containing view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!