2

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...

Shankar
  • 49
  • 1
  • 5

1 Answers1

-1

If you just want to add the three dotted line at the end of the text then you can do it simply. try below code

//programmatically

        text_view.setEllipsize(TextUtils.TruncateAt.END);
        text_view.setMaxLines(2);//set number of line what ever you want.
        text_view.setText("your long text");

//using xml

 <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="2"
            android:maxLines="2"
            android:text="your long text"
            android:textColor="@android:color/black" />

If you want to check the whether text is too long or not try this link .Hope it will help you.

Community
  • 1
  • 1
Chandra Sharma
  • 1,339
  • 1
  • 12
  • 26