Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end TextView with 3 dots by using maxLength

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.

I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size in dp, something that I don't want to do.

I know it is possible programmatically by trimming the string after the 15th character and then adding the 3 dots, but I prefer to do that by XML.

Any idea how to end the text with 3 dots and leave it wrap_content?

<TextView     android:id="@+id/inbox_contactName"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:lines="1"     android:maxLines="1"     android:ellipsize="end"     android:singleLine="true"     android:maxLength="15"     android:textColor="#0670b4"     android:textSize="16sp" /> 
like image 310
Yaniv Avatar asked Oct 02 '12 07:10

Yaniv


People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

How do I limit characters in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.

How do I insert bullet points in android?

The two Button s at the bottom have android:text="◄" and "►" .


1 Answers

This will solve your problem, Use ellipsize Property in your XML Code

android:ellipsize="end" <!-- This makes the magic ... thing --> android:maxEms="15" <!-- Limit of the Text --> android:singleLine="true" <!-- In case if you want everything in one line --> 

Edit: singleLine is deprecated. Use maxlines="1" instead.

like image 136
Kirk Avatar answered Oct 11 '22 05:10

Kirk