开发者

android append '…' at the end textview _EDIT

I have a lsit view and i that i need to add some text. in the adapter, I need to append ... at the end and i used the following when i give

android:ellipsize="end"

in the only shows 2 line eg:

asdnfsdfdsf asdfsdfasdf

sdfsd sdfsdf sdfsd ad...

And when i give

android:ellipsize="start"

it is as :

asdnfsdfdsf asdfsdfasdfd

...sdfsd sdfsdf sdfsd ad

The complete code i used:

<TextView  android:layout_width="wrap_content" android:background="#016A7D"

   android:layout_height="80dp" android:textColor="#000000"

    android:maxLines="4"          

    android:id="@+id/list_report_desciption" 

    android:ellipsize="end"

/> And the output i got is:

What exactly will a smarter planet

look like? How's IT changin开发者_StackOverflow社区g? A...

actually it contains more than 6 lines

Is there any thing i need to set so that i need to get 3 lines

Thanks


Text ellipsization seems buggy, accroding to this report: http://code.google.com/p/android/issues/detail?id=10554

A workaround allows ellipsizing one line of text, by setting the android:singleLine attribute to true. But this attribute is deprecated.


Nothing worked for me:-(
I just fixed the length. Cut the string in that length and append '...' after that.
Now its working for me.


I was looking for a simple solution for this myself, I ended up with something like:

private String getTextWithMoreIndicator(TextView textView, String text) {

    long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);

    if(maxVisibleChars < text.length()) {
        text = text.substring(0, (int)maxVisibleChars -3) + "..."; 
    }

    return text;
}


The solution you accepted will not scale correctly on all devices.

Reason : the width of "a" is lesser than "A", so, if you are truncating string based on some size (say 15 letters) and adding "..." through code. You might see results not expected.

Now coming to the solution:-

Solutions:

Solution # 1 : Add the following 3 attributes to your TextView and you'll get the result you want :)

<TextView
    android:ellipsize="end"
    android:lines="1"
    android:scrollHorizontally="true" />

Solution # 2 : Another workaround might be, you could decide to marquee text (the fancy animation moving your text from right to left). For that you need following attributes in your TextView xml:-

<TextView
    android:id="@+id/my_text_view"
    android:ellipsize="marquee"
    android:lines="1"
    android:scrollHorizontally="true"
    android:marqueeRepeatLimit="marquee_forever" />

And then in you code you need to get the TextView by its id and put-in the following line:-

TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setSelected(true);

#### Edit:- ####

I just figured out that for my solution to work in android versions greater than 2.3.x we need to add the following line in our TextView xml :-

    android:singleLine="true"

Although its a deprecated attribute, but you have to add this, otherwise marquee or "..." wont work.

Hope this answers the question :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜