Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I limit TextView's number of characters?

What I have now is a ListView of TextView elements. each TextView element displays a text (the text length varies from 12 words to 100+). What I want is to make these TextViews display a portion of the text (let's say 20 word or roughly 170 chars).

How to limit the TextView to a fixed number of characters?

like image 689
iTurki Avatar asked Feb 05 '12 14:02

iTurki


People also ask

How do I limit text 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.

What is EMS in TextView?

ems is a unit of measurement. The name em was originally a reference to the width of the capital M. It sets the width of a TextView/EditText to fit a text of n 'M' letters regardless of the actual text extension and text size. Eg : android:ems Makes the EditText be exactly this many ems wide.


1 Answers

Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.

<TextView      android:id="@+id/secondLineTextView"      android:layout_width="wrap_content"     android:layout_height="wrap_content"      android:maxLines="1"      android:maxLength="10"      android:ellipsize="end"/> 
like image 51
Booger Avatar answered Oct 06 '22 00:10

Booger