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?

iTurki
  • 16,292
  • 20
  • 87
  • 132

10 Answers10

178

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,579
  • 7
  • 55
  • 72
  • 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,892
  • 13
  • 53
  • 117
8

Use below code in TextView

 android:maxLength="65"

Enjoy...

Ganesh Katikar
  • 2,620
  • 1
  • 26
  • 28
6

I did this using the maxEms attribute.

 <TextView
    android:ellipsize="end"
    android:maxEms="10"/>
pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29
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" />

.

taranjeetsapra
  • 524
  • 7
  • 19
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
  • 75,855
  • 30
  • 208
  • 233
0

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.

Community
  • 1
  • 1
computingfreak
  • 4,939
  • 1
  • 34
  • 51
-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,617
  • 15
  • 18