Change the ellipsis style in TextView

1

I'm trying to change the style of the ellipsis in TextView, the default style is "...", I would like to change to "(...)".

I've developed a method, but it only works when the user clicks and refreshes the View

Does anyone know how I can change?

The method code:

ublic static String getEllipsizedText(TextView textView, String originalText, boolean label){

String ellipsised = TextUtils.ellipsize(textView.getText(), textView.getPaint(), (float)textView.getWidth(), TextUtils.TruncateAt.END).toString();

for(int i = 0; i < 5; i++){
    if(i < originalText.length() && i < ellipsised.length() && originalText.charAt(i) != ellipsised.charAt(i))
        return originalText;
}

int totalLine = (textView.getMeasuredHeight() / textView.getLineHeight());

int maxChars = ellipsised.length() * totalLine;

textView.setEllipsize(null);
textView.setSingleLine(false);

if(totalLine <= 1 && label == false){
    return originalText;
}

textView.setMaxLines(totalLine);

if(originalText.length() > maxChars){   

    int size = maxChars - 1;
    while(size >= 0){
        if(size < maxChars - 6 && originalText.charAt(size) == ' ')
            break;
        else
            size--;
    }

    return originalText.subSequence(0, size) + " (...)";
}
else{
    return originalText;
}

}

The call code

textview.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

    @Override
    public boolean onPreDraw() {
        textview.setText(FormatFactory.getEllipsizedText(textview, text, false));
        textview.getViewTreeObserver().removeOnPreDrawListener(this);
        textview.postInvalidate();
        textview.refreshDrawableState();
        mainView.invalidate();
        mainView.postInvalidate();
        mainView.refreshDrawableState();
        return true;
    }
});

EDIT

I solved it as follows:

public class MyTextView extends TextView{

private boolean changed = false;

public MyTextView(Context context) {
    super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
    super.onTextChanged(text, start, before, after);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if(changed == false){
        setText(getEllipsizedText(getText().toString()));
        changed = true;
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);      
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

@Override
public void setText(CharSequence text, TextView.BufferType type){
    super.setText(getEllipsizedText(text.toString()), type);
}

private int getMaxLinesCompat(){
    return getResources().getInteger(R.integer.mymaxlines);
}

private final String getEllipsizedText(String originalText){

    setEllipsize(null); // Impede que o textview coloque ellips automaticamente
    setSingleLine(false); // Impede que o textview fique em uma só linha

    if(getPaint().measureText(originalText) < getMeasuredWidth())
        return originalText;        

    int totalLine = (getMeasuredHeight() / getLineHeight()); // Pega o número maximo de linhas que cabem no textview

    if(totalLine > 1)
        totalLine--;

    while(totalLine > getMaxLinesCompat())
        totalLine--;

    int size = getPaint().breakText(originalText, true, getMeasuredWidth(), null) * totalLine;

    size -= 7;

    if(size < originalText.length()){
        while(size > 0){
            if(originalText.charAt(size) == ' '){
                return originalText.substring(0, size) + " (...)".replace("\r", "").replace("\t", "");
            }
            else
                size--;
        }
    }
    return originalText;
}

}

    
asked by anonymous 24.06.2014 / 17:31

2 answers

2

Take a look at this class:

Change the value of the ELLIPSIS variable

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.widget.TextView;

public class EllipsizingTextView extends TextView {
    private static final String ELLIPSIS = "...";

    public interface EllipsizeListener {
        void ellipsizeStateChanged(boolean ellipsized);
    }

    private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();
    private boolean isEllipsized;
    private boolean isStale;
    private boolean programmaticChange;
    private String fullText;
    private int maxLines = -1;
    private float lineSpacingMultiplier = 1.0f;
    private float lineAdditionalVerticalPadding = 0.0f;

    public EllipsizingTextView(Context context) {
        super(context);
    }

    public EllipsizingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addEllipsizeListener(EllipsizeListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        ellipsizeListeners.add(listener);
    }

    public void removeEllipsizeListener(EllipsizeListener listener) {
        ellipsizeListeners.remove(listener);
    }

    public boolean isEllipsized() {
        return isEllipsized;
    }

    @Override
    public void setMaxLines(int maxLines) {
        super.setMaxLines(maxLines);
        this.maxLines = maxLines;
        isStale = true;
    }

    public int getMaxLines() {
        return maxLines;
    }

    @Override
    public void setLineSpacing(float add, float mult) {
        this.lineAdditionalVerticalPadding = add;
        this.lineSpacingMultiplier = mult;
        super.setLineSpacing(add, mult);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int before, int after) {
        super.onTextChanged(text, start, before, after);
        if (!programmaticChange) {
            fullText = text.toString();
            isStale = true;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isStale) {
            super.setEllipsize(null);
            resetText();
        }
        super.onDraw(canvas);
    }

    private void resetText() {
        int maxLines = getMaxLines();
        String workingText = fullText;
        boolean ellipsized = false;
        if (maxLines != -1) {
            Layout layout = createWorkingLayout(workingText);
            if (layout.getLineCount() > maxLines) {
                workingText = fullText.substring(0, layout.getLineEnd(maxLines - 1)).trim();
                while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > maxLines) {
                    int lastSpace = workingText.lastIndexOf(' ');
                    if (lastSpace == -1) {
                        break;
                    }
                    workingText = workingText.substring(0, lastSpace);
                }
                workingText = workingText + ELLIPSIS;
                ellipsized = true;
            }
        }
        if (!workingText.equals(getText())) {
            programmaticChange = true;
            try {
                setText(workingText);
            } finally {
                programmaticChange = false;
            }
        }
        isStale = false;
        if (ellipsized != isEllipsized) {
            isEllipsized = ellipsized;
            for (EllipsizeListener listener : ellipsizeListeners) {
                listener.ellipsizeStateChanged(ellipsized);
            }
        }
    }

    private Layout createWorkingLayout(String workingText) {
        return new StaticLayout(workingText, getPaint(), getWidth() - getPaddingLeft() - getPaddingRight(),
                Alignment.ALIGN_NORMAL, lineSpacingMultiplier, lineAdditionalVerticalPadding, false);
    }

    @Override
    public void setEllipsize(TruncateAt where) {
        // Ellipsize settings are not respected
    }
}

Available here

    
28.06.2014 / 03:55
0

You can use the replace () method. This way:

String s1 = "Android...";
String s2 = s1.replace("...","(...)");
    
26.06.2014 / 23:31