-2

May I know how to limit the length of a text in listView ?

Below is the listView from wechat

enter image description here

and this is my listView

enter image description here

How to limit the length of text so it will display like this is the work description.Ple...

Any help would be greatly appreciated.

John Joe
  • 12,412
  • 16
  • 70
  • 135

6 Answers6

5

If you set the TextView to both singleLine and ellipsize to end, then you should have the desired result.

For example:

<TextView
   ...
   android:singleLine="true"
   android:ellipsize="end"
/>
John Joe
  • 12,412
  • 16
  • 70
  • 135
Knossos
  • 15,802
  • 10
  • 54
  • 91
3

Using filter in code (Programmatically in your java class)

InputFilter[] FilterArr = new InputFilter[1];
FilterArr[0] = new InputFilter.LengthFilter(10);
editEntryView.setFilters(FilterArray);

or in your xml file as below

android:maxLength="10"

combined with

android:singleLine="true"
android:ellipsize="end"
Madhur
  • 3,303
  • 20
  • 29
2

Try this:

int charactersLimit = 10;
String yourLargeString = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
String yourFinalString = yourLargeString.substring(0, Math.min(yourLargeString.length(), charactersLimit));
yourTextView.setText(yourFinalString + "...");
John Joe
  • 12,412
  • 16
  • 70
  • 135
Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32
  • Although this would work, I think that the OP would prefer to avoid the potential for the line to roll over to the next. Unless the font is monospaced, having a character limit is going to introduce a lot of variability in the height of the view. If for example, the letters just happen to be wider than the average sampling. – Knossos Jan 22 '16 at 10:32
2

you can use Code in java :

tv.setSingleLine();
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);

and xml Code :

android:singleLine="true"
android:ellipsize="end"
John Joe
  • 12,412
  • 16
  • 70
  • 135
Ali Aliasghar
  • 49
  • 1
  • 9
0

you can use code in java

       tv=(TextView)findViewById(R.id.txt_comentdata);
       tv.setMaxLines(3);
       tv.setEllipsize(TextUtils.TruncateAt.END)
0

You can use XML code:

android:singleLine="true"
android:maxLength="3"
android:ellipsize="end"
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103