@Test public void testLinkAnnotation() { Annotation link = new Annotation("link", "foobar"); SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); ssb.setSpan(link, 1, 2, 0 ); RichTextView textView = new RichTextView(application); textView.setText(ssb); final CharSequence text = textView.getText(); assertTrue("Text should be spanned", text instanceof Spanned); assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class); Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); assertEquals("There should be one span " + Arrays.toString(spans), 1, spans.length); assertTrue("The span should be a LinkSpan", spans[0] instanceof LinkSpan); assertEquals("The LinkSpan should have id \"foobar\"", "foobar", ((LinkSpan) spans[0]).getId()); }
@Override public void setText(CharSequence text, BufferType type) { text = getRichText(getContext(), text); super.setText(text, type); boolean hasLinks = hasLinks(text); if (hasLinks) { setMovementMethod(TouchableLinkMovementMethod.getInstance()); } else { setMovementMethod(null); } setFocusable(hasLinks); setRevealOnFocusHint(false); setFocusableInTouchMode(hasLinks); }
RichTextView extends TextView implements OnLinkClickListener { @Override public void setText(CharSequence text, BufferType type) { text = getRichText(getContext(), text); super.setText(text, type); boolean hasLinks = hasLinks(text); if (hasLinks) { setMovementMethod(TouchableLinkMovementMethod.getInstance()); } else { setMovementMethod(null); } setFocusable(hasLinks); setRevealOnFocusHint(false); setFocusableInTouchMode(hasLinks); } }
RichTextView extends TextView implements OnLinkClickListener { @Override public void setText(CharSequence text, BufferType type) { text = getRichText(getContext(), text); super.setText(text, type); boolean hasLinks = hasLinks(text); if (hasLinks) { setMovementMethod(TouchableLinkMovementMethod.getInstance()); } else { setMovementMethod(null); } setFocusable(hasLinks); setRevealOnFocusHint(false); setFocusableInTouchMode(hasLinks); } RichTextView(Context context);  RichTextView(Context context, AttributeSet attrs); }
RichTextView extends TextView implements OnLinkClickListener { @Override public void setText(CharSequence text, BufferType type) { text = getRichText(getContext(), text); super.setText(text, type); boolean hasLinks = hasLinks(text); if (hasLinks) { setMovementMethod(TouchableLinkMovementMethod.getInstance()); } else { setMovementMethod(null); } setFocusable(hasLinks); setRevealOnFocusHint(false); setFocusableInTouchMode(hasLinks); } RichTextView(Context context);  RichTextView(Context context, AttributeSet attrs); static CharSequence getRichText(Context context, CharSequence text); @Override void setText(CharSequence text, BufferType type); @Override @SuppressWarnings("ClickableViewAccessibility") // super.onTouchEvent is called boolean onTouchEvent(MotionEvent event); void setOnLinkClickListener(OnLinkClickListener listener); OnLinkClickListener getOnLinkClickListener(); @Override boolean onLinkClick(LinkSpan span); }
RichTextView extends TextView implements OnLinkClickListener { @Override public void setText(CharSequence text, BufferType type) { text = getRichText(getContext(), text); super.setText(text, type); boolean hasLinks = hasLinks(text); if (hasLinks) { setMovementMethod(TouchableLinkMovementMethod.getInstance()); } else { setMovementMethod(null); } setFocusable(hasLinks); setRevealOnFocusHint(false); setFocusableInTouchMode(hasLinks); } RichTextView(Context context);  RichTextView(Context context, AttributeSet attrs); static CharSequence getRichText(Context context, CharSequence text); @Override void setText(CharSequence text, BufferType type); @Override @SuppressWarnings("ClickableViewAccessibility") // super.onTouchEvent is called boolean onTouchEvent(MotionEvent event); void setOnLinkClickListener(OnLinkClickListener listener); OnLinkClickListener getOnLinkClickListener(); @Override boolean onLinkClick(LinkSpan span);  }