Android: ellipse end - extra characters after ellipse

I created a text view as follows:

<TextView android:id="@+id/TextView_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:ellipsize="end" android:maxLines="3" android:textColor="@color/white" android:textIsSelectable="false" android:textSize="14sp" /> 

The text inside is set programmatically using the layout inflator, the text coming from an XML feed. I checked the source and there were no random characters or line breaks.

Testing this TextView on a Nexus 4 running Android 4.3. I see strange behavior.

String inside the ends: "Lorem ipsum dolor sit amet"

In Nexus, this is truncated in the right place, but instead of ending with the ellipsis symbol, like: "dol ...", I get "dol ... s" with the final half-hidden character breaking through the border.

Increasing the field then gives me "ipsum ... dol" at the end.

I tested this on my other Galaxy S3 test devices running 4.1.2, and Desire C is running 4.0.3, and not on them.

Has anyone else seen this behavior with an ellipsis: end? Any suggestions on what I could have done wrong or how can I get around this?

+8
android textview
source share
2 answers

Thanks to a lot of trial and error, I just found that this is due to the presence of the character '\ n'. There is some problem with ellipsize = "end" when there is a new line in the text, even if it is good after the ellipsis.

just:

 text = text.replace('\n',' '); 

fixed this problem for me

+6
source share

I have encountered a similar problem before. I recommend not using the "ellipsis" parameter with "maxLines". Add an ellipse label to textView and it can be controlled programmatically.

Markup

 <TextView android:id="@+id/multiLineText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="3"/> <TextView android:id="@+id/ellipsizeMarkText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:text="..."/> 

the code

 if(multiLineText.getLineCount() > multiLineText.getMaxLines()) { ellipsizeMarkText.setVisibility(View.VISIBLE); } else { ellipsizeMarkText.setVisibility(View.GONE); } 
0
source share

All Articles