Posted on

LinkifyCompat, get Urls

import android.support.v4.text.util.LinkifyCompat;

@Nullable
    public void find(@NonNull String text) {
        List<String> urls = getLinkifiedUrls(text);
        for (String url : urls) {
          //
        }
    }

    private List<String> getLinkifiedUrls(String text) {
        SpannableString ss = new SpannableString(text);
        LinkifyCompat.addLinks(ss, Linkify.WEB_URLS);
        URLSpan[] urls = ss.getSpans(0, ss.length(), URLSpan.class);
        ArrayList<String> result = new ArrayList<>();
        for (URLSpan url : urls) {
            result.add(url.getURL());
        }
        return result;
    }
Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.