2

I have a TextView, that should be max 15 characters and at the end it should have three dots. With my XML it cuts 15 characters but doesn't add three dots ... at the end.

IMPORTANT: I know it works when I limit the text with line count - android:maxLines="2". But I need limit it with max characters count, as stated in question.

<TextView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:maxLength="15"
    android:ellipsize="end"
    tools:text="123456789qwertyuiopasdfghjklzxcvbnm"
    android:textSize="50sp"
/>

The result:

enter image description here

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
  • @Amardeep , I'seen this before posting question, but it is not duplicate, in that cases they limit with android:maxLines="2", and as I stated I want to limit by `android:maxLength=15`. – Misha Akopov Jan 17 '18 at 12:01

2 Answers2

5

I'm not sure how or if you can do what you want in XML.

At least i don't know how - but i have an alternative solution for you.

public void ellipsizeText(TextView tv){
    int maxLength = 13;
    if(tv.getText().length() > maxLength){
        String currentText = tv.getText().toString();
        String ellipsizedText =  currentText.substring(0, maxLength - 3) + "...";
        tv.setText(ellipsizedText);
    }
}

So i just made it as a method that take a textview in as a parameter. It's really quite simple, you change the maxLength to the desired max length of the string. In this case it is set to 13 since it replaces the last 3 chars with "...", which then results in a String that is still 13 chars long, but with the first 10 being the actual text and the last 3 being "...".

In your main project you would change maxLength to 150 (or 153, if you want 150 visible chars from the string)

Hope you can use this, and if you got any questions let me know.

Jesper Purup
  • 384
  • 2
  • 13
  • Thanks for reply ! Yes I need it in xml, I've writen substring code already and I am ashamed because of it. But I think **Android** must have XML solution for this, because it is not correct to do this in code. – Misha Akopov Jan 17 '18 at 12:48
  • Gonna follow along to see if someone comes up with a solution. This was what i had for now ;) – Jesper Purup Jan 17 '18 at 13:03
  • Yep, I've searched everything and didn't find needed solution. Anyway, upvote from me for efforts you made with code :) Maybe it will help others if there is no XML solution – Misha Akopov Jan 17 '18 at 13:08
1

Try this one solution:

 <TextView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:ellipsize="end"
    android:singleLine="true"
    android:maxLines="1"
    android:text="123456789qwertyuiopasdfghjklzxcvbnm"
    android:textSize="50sp"
    />

Hope this help you...if you need any help you can ask

Mahesh Vayak
  • 1,056
  • 12
  • 25
  • Yep I know it works when I limit text with line count, but as stated I limit with character count `android:maxLength=15` – Misha Akopov Jan 17 '18 at 12:07
  • VERY IMPORTANT: ellipsize="end" only works when maxLength is set to a value that is MORE THAN the number of characters on one line. You can use 'wrap_content' if you align the TextView's end and start with any other view(s). – Mahesh Vayak Jan 17 '18 at 12:09
  • Actually in real project I have maxLength= 150(much more than the number of characters on one line) and still no success. – Misha Akopov Jan 17 '18 at 12:58