Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ellipsized a TextView which has LinkMovementMethod

I have a TexView which needs to be placed in maxim 2 lines and has a linkable text in it. If I set LinkMovementMethod to the text view I get a scrollable TextView and the ellipsize is ignored.

Xml code:

 <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dip"
            android:clickable="true"
            android:maxLines="2"
            android:ellipsize="end"

            android:scrollHorizontally="false"
            android:scrollbars="horizontal"
            android:isScrollContainer="false"/>

This is my activity:

public class MyActivity extends Activity {

    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.text_view);

        SpannableStringBuilder captionSpan = new SpannableStringBuilder();
        captionSpan.append("a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here");
        captionSpan.setSpan(new CustomClickableSpan(), 1, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv.setText(captionSpan);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }

}


public class CustomClickableSpan extends android.text.style.ClickableSpan {

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setColor(tp.linkColor);
        tp.setUnderlineText(false);
    }

    @Override
    public void onClick(View widget) {
    }

}

If I do not set the movement method everything is working fine.

like image 846
Paul Avatar asked Nov 27 '13 15:11

Paul


People also ask

How do I make a TextView look like a link?

This is the shortest solution: final CharSequence text = tv. getText(); final SpannableString spannableString = new SpannableString( text ); spannableString. setSpan(new URLSpan(""), 0, spannableString.

How do you check if a TextView's text exceeds the max lines?

@Davide If it exceeds the TextView's max lines which is set in the XML. To access in Java, it's TextView. getMaxLines(). So for example, if the text would need 10 lines but the max lines is 5 then lines 6-10 will be truncated and I would like to detect when this happens.

How do I assign text to TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

How do I add a newline to a TextView in android?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.


1 Answers

I found solution!
Instead setMovementMethod() use OnTouchListener.

String text = textView.getText().toString();
SpannableString spanText = new SpannableString(text);

//here set your spans to spanText

textView.setOnTouchListener(new TouchTextView(spanText));
textView.setText(spanText);

I got this onTouchEvent() from LinkMovementMethod class.

static class TouchTextView implements View.OnTouchListener {
    Spannable spannable;

    public TouchTextView (Spannable spannable){
        this.spannable = spannable;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if(!(v instanceof TextView)){
            return false;
        }
        TextView textView  = (TextView) v;
        if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= textView.getTotalPaddingLeft();
            y -= textView.getTotalPaddingTop();

            x += textView.getScrollX();
            y += textView.getScrollY();

            Layout layout = textView.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(textView);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(spannable,
                            spannable.getSpanStart(link[0]),
                            spannable.getSpanEnd(link[0]));
                }

                return true;
            } else {
                Selection.removeSelection(spannable);
            }
        }

        return false;
    }
}

And sure TextView should have this attributes:

android:maxLines="2"
android:ellipsize="end"
like image 112
Airfreshener Avatar answered Oct 23 '22 01:10

Airfreshener