I saw this post on how to check if text is ellipsized.
I want to programatically change the text if it's ellipsized
.
however, if I use callback in textView.post()
the text change will be seen in the UI for the user or cause performance issues.
side comment:
I have tried to measure text width and view width in treeObserver.preDraw()
but there is a case I see when debugging that both width are equal and nevertheless the text is replaced when it's not truncated.
replacing the condition to textWidth < viewWidth
makes few cases of non-ellipsized
text to still be replaced by shorter one.
final ViewTreeObserver[] viewTreeObserver = {myAccountView.getViewTreeObserver()};
viewTreeObserver[0].addOnPreDrawListener(
new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int chipWidth =
myAccountView.getMeasuredWidth()
- myAccountView.getPaddingLeft()
- myAccountView.getPaddingRight();
if (chipWidth > 0) {
myAccountView.setText(
setChipTextWithCorrectLength(
getContext().getString(R.string.desc_long_length),
getContext().getString(R.string.desc_meduim_length),
getContext().getString(R.string.short_length),
chipWidth));
viewTreeObserver[0] = myAccountView.getViewTreeObserver();
if (viewTreeObserver[0].isAlive()) {
viewTreeObserver[0].removeOnPreDrawListener(this);
}
}
return true;
}
});