Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView: Add text after ellipsis or change ellipsis character

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?

like image 971
Andrei Tudor Diaconu Avatar asked Sep 11 '13 09:09

Andrei Tudor Diaconu


People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

What is Ellipsize in TextView?

Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.

How do I add a newline to a TextView in Android?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

How do you add ellipses on Android?

Ellipsis is triple dots (...) at the end of the line. It is required when you want to keep your sentence in a single line. If your test reached the end of the line then automatically create ... at the end.


1 Answers

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"
like image 80
Biraj Zalavadia Avatar answered Sep 27 '22 18:09

Biraj Zalavadia