75

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?

iTurki
  • 16,042
  • 20
  • 85
  • 131

9 Answers9

175

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"/>
Booger
  • 18,259
  • 7
  • 52
  • 71
  • 2
    "then use the ellipsize=marquee to add a "..." automatically to the end of any line" – Booger Feb 05 '12 at 14:27
  • Sounds promising. I'll give it a try. – iTurki Feb 05 '12 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 '14 at 05: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. – Sagar Devanga Aug 19 '15 at 08:01
  • I think layout_width has a relation to trigger ellipsize property. Well explained example, Please follow the [link](https://stackoverflow.com/questions/9149846/can-i-limit-textviews-number-of-characters/61593663#61593663) – taranjeetsapra May 05 '20 at 06: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;
}
Darko Petkovski
  • 3,824
  • 12
  • 50
  • 112
8

Use below code in TextView

 android:maxLength="65"

Enjoy...

Ganesh Katikar
  • 2,532
  • 1
  • 23
  • 28
3

I did this using the maxEms attribute.

 <TextView
    android:ellipsize="end"
    android:maxEms="10"/>
pratham kesarkar
  • 3,606
  • 3
  • 16
  • 27
2

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

.

taranjeetsapra
  • 497
  • 7
  • 17
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

miroku
  • 176
  • 2
  • 10
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)
 }
Blundell
  • 73,122
  • 30
  • 204
  • 230
-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.

Community
  • 1
  • 1
computingfreak
  • 4,604
  • 1
  • 34
  • 47
-4

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

Andreas
  • 1,597
  • 14
  • 17
  • 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 '15 at 09:13