Linkify.addLinks with Span not working

1.8k views Asked by At

I have textview with autoLink, but whenever i add custom span (ClickableSpan) to textview its auto link to web url and mobile number is not working. is there any easy way to solve this issue. Style is applied but click is not working.

3

There are 3 answers

0
Bincy Baby On BEST ANSWER

https://stackoverflow.com/a/39494610/4639479 I used this answer and worked fine

public static String[] extractLinks(String text) {
    List<String> links = new ArrayList<String>();
    Matcher m = Patterns.WEB_URL.matcher(text);
    while (m.find()) {
        String url = m.group();
        links.add(url);
    }
    return links.toArray(new String[links.size()]);
}
0
chandrakant sharma On

It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.

Use this code to get it work:

 public static Spannable linkifyHtml(String html, int linkifyMask) {
    Spanned text = Html.fromHtml(html);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}
0
Ellen Strnod On

Updated the prior answer for Html.fromHtml(html) deprecation, and added a check for scheme prefix in URL if not present (which, now, when targeting Android 12, results in "Activity not found for Intent" error message if the URL is not prefixed with a scheme):


    public static Spannable linkifyHtml(String html, int linkifyMask) {
        Spanned text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
        URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
        SpannableString buffer = new SpannableString(text);
        Linkify.addLinks(buffer, linkifyMask);

        if (currentSpans.length > 0) {
            // fix URLs of any links which don't have a scheme  
            URLSpan[] fixedSpans = new URLSpan[currentSpans.length];
            for (int i = 0; i < currentSpans.length; i++) {
                if (!currentSpans[i].getURL().contains("//:")) {
                    URLSpan fixed = new URLSpan("http://" + currentSpans[i].getURL());
                    log.debug("Fixed URL: " + currentSpans[i].getURL() + " --> " + fixed.getURL());
                    fixedSpans[i] = fixed;
                } else {
                    fixedSpans[i] = currentSpans[i];
                }
            }
            for (int i = 0; i < currentSpans.length; i++) {
                int end = text.getSpanEnd(currentSpans[i]);
                int start = text.getSpanStart(currentSpans[i]);
                buffer.setSpan(fixedSpans[i], start, end, 0);
            }
        }
        return buffer;
    }
}

This worked for me for both a bare "www.foo.com" URL and one that was an anchor link, in the same HTML, taking the results of this method and setting it into a TextView with android:linksClickable="true" and in the code, setting setMovementMethod(LinkMovementMethod.getInstance());.