78

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?

10 Answers 10

179

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"/>
5
  • 2
    "then use the ellipsize=marquee to add a "..." automatically to the end of any line"
    – Booger
    Feb 5, 2012 at 14:27
  • Sounds promising. I'll give it a try.
    – iTurki
    Feb 5, 2012 at 14:31
  • 5
    @Booger android:ellipsize="marquee" don't append ellipsis (...) at the end, rather, it covers the end of cut off text as a sign of limit. android:ellipsize="end" will add the ellipsis (...) at the end, while android:ellipsize="start" will append ellipsis at the start
    – ajdeguzman
    Feb 26, 2014 at 5:58
  • @ajdeguzman what should i do, if i want the remaining text on the second line. I have made the maxLines = 2 and have not included the ellipsize parameter.I know there is something known as em also but i didnt get a clear explanation on how it works. Aug 19, 2015 at 8:01
  • I think layout_width has a relation to trigger ellipsize property. Well explained example, Please follow the link May 5, 2020 at 6:47
9

If your not interested in xml solutions maybe you can do this:

String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello

...

public String getSafeSubstring(String s, int maxLength){
  if(!TextUtils.isEmpty(s)){
    if(s.length() >= maxLength){
      return s.substring(0, maxLength);
    }
  }
  return s;
}
0
8

Use below code in TextView

 android:maxLength="65"

Enjoy...

6

I did this using the maxEms attribute.

 <TextView
    android:ellipsize="end"
    android:maxEms="10"/>
3

I am sharing an example where I have set maxLength=1 i.e. 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.

Please Note: layout_width which is 120dp i.e. after 120dp any text exceeding will triggrer "ellipsize=end" property

paste the below code directly to check.

<TextView
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:maxLength="40"
    android:text="Can I limit TextView's number of characters?"
    android:textColor="@color/black"
    android:textSize="12sp"
    android:textStyle="bold" />

.

2

You can use setEllipsize method of TextView class http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)

With the constants of TextUtil class for added the suspension points http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html

1

Programmatic Kotlin.

Cut off the start of the text:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
 }

Cut off the end of the text:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
 }
1

Here is the correct Solution. Keep remember, to set width to WRAP CONTENT. and ems is not based on Max Charater. EMS based on width. If ems value width will exceed the text length, then automatically (...) will be added at last of textview.

android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"
-1

As mentioned in https://stackoverflow.com/a/6165470/1818089 & https://stackoverflow.com/a/6239007/1818089, using

android:minEms="2"

should be enough to achieve the goal stated above.

-4

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound.

2
  • 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.
    – kapilgm
    Nov 23, 2015 at 9:13
  • that's alot of work, don't you think?! Feb 15 at 14:47

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.