Skip to content Skip to sidebar Skip to footer

Ellipsize Not Working With Textview

This 2 works

Solution 1:

Here is information from documentation of TextView

public void setEllipsize (TextUtils.TruncateAt where)Added in API level 1

Causes words in the text that are longer than the view is wide to be ellipsized instead of broken in the middle. You may also want to setSingleLine() or setHorizontallyScrolling(boolean) to constrain the text to a single line. Use null to turn off ellipsizing. If setMaxLines(int) has been used to set two or more lines, only END and MARQUEE are supported (other ellipsizing types will not do anything).


However I've made very simple ellipsize="middle" for multiline. Of course it should be more upgraded in free time but here it is.

This is class of widget to paste into root of package

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

publicclassMiddleMultilineTextViewextendsTextView {

    privateStringSYMBOL=" ... ";
    privatefinalintSYMBOL_LENGTH= SYMBOL.length();

    publicMiddleMultilineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (getMaxLines() > 1) {
            intoriginalLength= getText().length();
            intvisibleLength= getVisibleLength();

            if (originalLength > visibleLength) {
                setText(smartTrim(getText().toString(), visibleLength - SYMBOL_LENGTH));
            }
        }
    }

    private String smartTrim(String string, int maxLength) {
        if (string == null)
            returnnull;
        if (maxLength < 1)
            return string;
        if (string.length() <= maxLength)
            return string;
        if (maxLength == 1)
            return string.substring(0, 1) + "...";

        intmidpoint= (int) Math.ceil(string.length() / 2);
        inttoremove= string.length() - maxLength;
        intlstrip= (int) Math.ceil(toremove / 2);
        intrstrip= toremove - lstrip;

        Stringresult= string.substring(0, midpoint - lstrip) + SYMBOL + string.substring(midpoint + rstrip);
        return result;
    }

    privateintgetVisibleLength() {
        intstart= getLayout().getLineStart(0);
        intend= getLayout().getLineEnd(getMaxLines() - 1);
        return getText().toString().substring(start, end).length();
    }
}

This is custom widget to use in layout:

<com.example.middlemultiline.MiddleMultilineTextView
        android:id="@+id/item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:maxLines="4"
        android:text="Months earlier, in May 2007, a typically busy time for construction work, he sat home for two weeks without any jobs lined up, the first time that had ever happened in all the years he’d been an independent contractor. It was an early indication that hard times were ahead. By fall, he tried to find a steady job with a construction company but by then no one was hiring. And now he no longer had the extra income to support his wife’s entrepreneurial effort — a coffee vending machine business — so that went under too." />

Result:

preview

Code was based on: https://stackoverflow.com/a/8798989/619673https://stackoverflow.com/a/831583/619673

Solution 2:

Actually android:ellipsize="end" itself means you have a content with elip working at end so must have to use android:singleLine="true" See documentation here

Solution 3:

I also made a custom TextView regarding to @deadfish answer. If you don't want to lose your words you may use this; It removes the previous word of the last word in the sentence if It does not fit the line. like;

This is a good sentence. -> This is a .. sentence.

privateString smartTrim__word_cut(String input,int visible){
    int visibleLength = visible;
    int textSize = input.length();//input.length();Boolean hasDot = false;
    String visibleText = "";

    while(textSize > visibleLength)
    {
        // To ArrayListList<String> l2 = new ArrayList<>(Arrays.asList(input.split(" ")));

        if(!hasDot){
            l2.set(l2.size()-2, "..");
            hasDot = true;
        }
        else{
            l2.remove(l2.size()-3);
        }

        StringBuilder listString = new StringBuilder();
        for (String s : l2){
            listString.append(s+" ");
        }
        visibleText = listString.toString().trim();
        textSize = visibleText.length();
        input = visibleText;
    }
    return visibleText;
}

NOTE: It may also cut the first sentence in some cases and make it like; ".. sentence". Needs to be improved.

Post a Comment for "Ellipsize Not Working With Textview"