Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The solution goes like this - </p> <p>Call setLinks() with you textview and the text to be added.</p> <pre><code>setLinks(textView, text); </code></pre> <p>setLinks() function is as - </p> <pre><code>void setLinks(TextView tv, String text) { String[] linkPatterns = { "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\"&gt;\\]\\)]*[^\\. ,'\"&gt;\\]\\)])", "#[\\w]+", "@[\\w]+" }; for (String str : linkPatterns) { Pattern pattern = Pattern.compile(str); Matcher matcher = pattern.matcher(tv.getText()); while (matcher.find()) { int x = matcher.start(); int y = matcher.end(); final android.text.SpannableString f = new android.text.SpannableString( tv.getText()); InternalURLSpan span = new InternalURLSpan(); span.text = text.substring(x, y); f.setSpan(span, x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(f); // tv.setOnLongClickListener(span.l); } } tv.setLinkTextColor(Color.BLUE); tv.setLinksClickable(true); tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setFocusable(false); } </code></pre> <p>and the InternalURLSpan class goes like this - </p> <pre><code>class InternalURLSpan extends android.text.style.ClickableSpan { public String text; @Override public void onClick(View widget) { handleLinkClicked(text); } } </code></pre> <p>handleLinkClicked() is as - </p> <pre><code>public void handleLinkClicked(String value) { if (value.startsWith("http")) { // handle http links } else if (value.startsWith("@")) { // handle @links } else if (value.startsWith("#")) { // handle #links } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload