TextView: Add text after ellipsis or change ellipsis character
Asked Answered
G

1

8

What I am trying to do

I am trying to have a TextView with

android:ellipsize="end"
android:maxLines="3"

that instead of the "..." has a "... Show More" at the end.

Where I am

If the TextView would be single line this would be easy as I could use 2 TextViews, but I need it to have multiple lines and the "Show More" needs to be inline.

I have been searching for a way to change the ellipsis character that is added at the end but could not find anything.

The only solution I can think of is giving up on the automatic ellipsis, measure text and add the "... Show More" from code.

The question

Is there a way to replace what Android adds at the end (the "...") with something custom?

Goody answered 11/9, 2013 at 9:55 Comment(1)
check I post your answer.Indochina
I
12

Try this way

create this function

public  void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText) {

        if (tv.getTag() == null) {
            tv.setTag(tv.getText());
        }
        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {

                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (maxLine <= 0) {
                    int lineEndIndex = tv.getLayout().getLineEnd(0);
                    String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                    tv.setText(text);
                } else if (tv.getLineCount() >= maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                    tv.setText(text);
                }
            }
        });

    }

how to use

This will write "SeeMore" instead "..." at the end of 4th line

makeTextViewResizable(tv, 4, "SeeMore");

Now not need to write these lines in xml

android:ellipsize="end"
android:maxLines="3"
Indochina answered 11/9, 2013 at 10:4 Comment(4)
I appreciate your answer. After searching for a solution I found out that the Android ellipsize does not work for multiple lines ( known old bug link). I ended up using one of the solutions for the bug, and modified it to fit my needs.Goody
I am marking this as the accepted answer as it solves the problem, but for other people landing on this page: the solution does not work for text with spans (if you are using Html.fromHtml for instance) and if the text is in a ListView you might get jittery results.Goody
Base on your code, we can modify a little bit to meet our expectation. Thanks a lot!Drusi
What is the purpose of set tag?Confirmation

© 2022 - 2024 — McMap. All rights reserved.