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.
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.
来源:https://stackoverflow.com/questions/17402911/textview-androidellipsize-marquee-not-working-as-expected