Linkify.addLinks with Span not working
Asked Answered
L

3

6

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.

Limitative answered 8/9, 2017 at 8:5 Comment(1)
Post a code, where you demonstrate what have you tried so far.Yesman
L
4

https://mcmap.net/q/1913725/-textview-catch-clicking-on-link-when-autolink-is-set-to-quot-web-quot 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()]);
}
Limitative answered 12/9, 2017 at 5:23 Comment(0)
S
0

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;
}
Stoneham answered 11/9, 2017 at 11:16 Comment(0)
C
0

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());.

Conviction answered 2/9, 2022 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.