Can I limit TextView's number of characters?

AndroidTextview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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;
}

Solution 3 - Android

Use below code in TextView

 android:maxLength="65"

Enjoy...

Solution 4 - Android

I did this using the maxEms attribute.

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

Solution 5 - Android

Solution 6 - Android

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

.

Solution 7 - Android

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)
 }

Solution 8 - Android

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.

Solution 9 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniTurkiView Question on Stackoverflow
Solution 1 - AndroidBoogerView Answer on Stackoverflow
Solution 2 - AndroidDarko PetkovskiView Answer on Stackoverflow
Solution 3 - AndroidGanesh KatikarView Answer on Stackoverflow
Solution 4 - Androidpratham kesarkarView Answer on Stackoverflow
Solution 5 - AndroidmirokuView Answer on Stackoverflow
Solution 6 - AndroidtaranjeetsapraView Answer on Stackoverflow
Solution 7 - AndroidBlundellView Answer on Stackoverflow
Solution 8 - AndroidcomputingfreakView Answer on Stackoverflow
Solution 9 - AndroidAndreasView Answer on Stackoverflow