Reputation: 3572
Whenever a text (That I set programmatically from an Activity) is longer than n charachters, I want to cut off and limit the texts length and add 3 dots at the end of the textview
. Text is initially set to empty (just "") in xml
, and is set from activity later.
This is how I tried:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:maxLength="10"
android:ellipsize="end"
android:maxLines="1"/>
This is how it ends up looking with a random text, and I set the maxlength to 10 charachters:
I was expecting it to look like jrkfkgkglg...
because the text is jrkfkgkglggfgirng
, but the dots are not added at the end. Any suggestions?
Upvotes: 2
Views: 770
Reputation: 116
For this you have to remove the attribute
android:maxLines="1"
and add the new attribute
android:singleLine="true"
and in addition make your textview width match_parent. It will work properly when you will add this then you don't need to use anything else.
Upvotes: 1
Reputation: 7651
You are telling your edit text to be maximum 10 chars in length, in your screenshot, you have a string with that length so if you will try to add anything to that string it won't get added.
You can remove the android:maxLength="10"
limit and check your text from your code programmatically, something like this:
if (yourText.length() > 10) {
yourText = yourText.substring(0, 9); //get the first 10 chars
textView.setText(yourText + "..."); //display
} else {
textView.setText(original string < 10chars);
}
Upvotes: 1
Reputation: 1021
In order for this to work, your width must be match_parent
or have a any other size defined instead of being wrap_content
.
Upvotes: 2