1
I am trying to change the style of the ellipsis in Textview, the default style is "...", I would like to change to "(...)".
I developed a method, but it only works when the user clicks and updates the View
Does anyone know how I can change?
The code of the method:
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 decided 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;
}
}
Quite complicated this task, would it not be worth looking at the source code of the
TextView
to see how he does?– Wakim