Using regular expressions to highlight links in a text (Linkify vs Patterns)

210 views Asked by At

Why, with this approach, we will only use the last pattern:

Linkify.addLinks(someSpannable, Linkify.PHONE_NUMBERS)
Linkify.addLinks(someSpannable, Linkify.EMAIL_ADDRESSES)

In this case, both phone and email links will be highlighted:

Linkify.addLinks(someSpannable, Patterns.PHONE, null)
Linkify.addLinks(someSpannable, Patterns.EMAIL_ADDRESS, null)
1

There are 1 answers

1
hakim On

Based on the documentation: https://developer.android.com/reference/android/text/util/Linkify#addLinks(android.text.Spannable,%20int)

public static final boolean addLinks (Spannable text, int mask)

Scans the text of the provided Spannable and turns all occurrences of the link types indicated in the mask into clickable links. If the mask is nonzero, it also removes any existing URLSpans attached to the Spannable, to avoid problems if you call it repeatedly on the same text.

in the other hand https://developer.android.com/reference/android/text/util/Linkify#addLinks(android.text.Spannable,%20int,%20java.util.function.Function%3Cjava.lang.String,%20android.text.style.URLSpan%3E)

public static final boolean addLinks (Spannable text, 
            int mask, 
            Function<String, URLSpan> urlSpanFactory)

Scans the text of the provided Spannable and turns all occurrences of the link types indicated in the mask into clickable links. If the mask is nonzero, it also removes any existing URLSpans attached to the Spannable, to avoid problems if you call it repeatedly on the same text.