8

I have set up 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 programatically by a layout inflator, with the text coming from an XML feed. I've checked the source and there are no stray characters or line breaks.

Testing this TextView on a Nexus 4 running Android 4.3 I'm seeing weird behaviour.

The string inside ends: ""Lorem ipsum dolor sit amet"

On the Nexus this is truncating at the right place, but instead of ending with the ellipsis character like: "dol...", I'm getting "dol...s" with the final character half obscured as it breaks through the margin.

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

I've checked this on my other test Devices a Galaxy S3 running 4.1.2 and a Desire C running 4.0.3 and it isn't present on those.

Has anybody else seen this behaviour with ellipsize:end? Any suggestions for what I might have done wrong or how I can get around this?

2 Answers 2

6

Through a lot of trial and error, I just found that it is caused by having a '\n' character. There is some sort of problem with ellipsize="end" when there is a new line somewhere in the text, even if it is well after the ellipsize.

doing simply:

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

fixed this problem for me

2
  • Thank You. That's done the trick for me too. I had multiple line breaks and didn't need to replace them with spaces, just get rid of them so I used: text.replaceAll("\n","")
    – Keab42
    Sep 30, 2013 at 8:55
  • 2
    This works but what if you want to keep your newline chars in?
    – kassim
    Jan 24, 2014 at 12:24
0

I faced similar problem before. I recommend that do not use 'ellipsize' option with 'maxLines'. Add a ellipsize mark textView and it can be controlled programically.

Layout

<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="..."/>

Code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.