I have a customTextView that extends TextView.
I want to override the setText such that if some text length is greater than max line count then I have to show some other text instead of the text from getText.
This is my code
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setLinksClickable(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
String text = String.valueOf(getText());
Spanned htmlFormatedText = OmniTextUtil.getHyperLinked(text);
setText(htmlFormatedText);
List<CharSequence> charSequenceList = getLines(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
if(getLineCount() > getMaxLines())
{
for(int i =0; i < getMaxLines(); i++)
{
text = text + charSequenceList.get(i);
}
text = text.substring(0, text.length() -3) + "...";
setText(text);
}
}
}
public static List<CharSequence> getLines(TextView view) {
final List<CharSequence> lines = new ArrayList<>();
final Layout layout = view.getLayout();
if (layout != null) {
// Get the number of lines currently in the layout
final int lineCount = layout.getLineCount();
// Get the text from the layout.
final CharSequence text = layout.getText();
// Initialize a start index of 0, and iterate for all lines
for (int i = 0, startIndex = 0; i < lineCount; i++) {
// Get the end index of the current line (use getLineVisibleEnd()
// instead if you don't want to include whitespace)
final int endIndex = layout.getLineEnd(i);
// Add the subSequence between the last start index
// and the end index for the current line.
lines.add(text.subSequence(startIndex, endIndex));
// Update the start index, since the indices are relative
// to the full text.
startIndex = endIndex;
}
}
return lines;
}
}
II can able to set my text but on scrolling it again becomes normal text...