Linkify
public
class
Linkify
extends Object
java.lang.Object | |
↳ | android.text.util.Linkify |
Linkify take a piece of text and a regular expression and turns all of the
regex matches in the text into clickable links. This is particularly
useful for matching things like email addresses, web URLs, etc. and making
them actionable.
Alone with the pattern that is to be matched, a URL scheme prefix is also
required. Any pattern match that does not begin with the supplied scheme
will have the scheme prepended to the matched text when the clickable URL
is created. For instance, if you are matching web URLs you would supply
the scheme http://
. If the pattern matches example.com, which
does not have a URL scheme prefix, the supplied scheme will be prepended to
create http://example.com
when the clickable URL link is
created.
Summary
Nested classes | |
---|---|
interface |
Linkify.MatchFilter
MatchFilter enables client code to have more control over what is allowed to match and become a link, and what is not. |
interface |
Linkify.TransformFilter
TransformFilter enables client code to have more control over how matched patterns are represented as URLs. |
Constants | |
---|---|
int |
ALL
Bit mask indicating that all available patterns should be matched in methods that take an options mask |
int |
EMAIL_ADDRESSES
Bit field indicating that email addresses should be matched in methods that take an options mask |
int |
MAP_ADDRESSES
Bit field indicating that street addresses should be matched in methods that take an options mask. |
int |
PHONE_NUMBERS
Bit field indicating that phone numbers should be matched in methods that take an options mask |
int |
WEB_URLS
Bit field indicating that web URLs should be matched in methods that take an options mask |
Fields | |
---|---|
public
static
final
Linkify.MatchFilter |
sPhoneNumberMatchFilter
Filters out URL matches that don't have enough digits to be a phone number. |
public
static
final
Linkify.TransformFilter |
sPhoneNumberTransformFilter
Transforms matched phone number text into something suitable to be used in a tel: URL. |
public
static
final
Linkify.MatchFilter |
sUrlMatchFilter
Filters out web URL matches that occur after an at-sign (@). |
Public constructors | |
---|---|
Linkify()
|
Public methods | |
---|---|
static
final
boolean
|
addLinks(Spannable spannable, Pattern pattern, String defaultScheme, String[] schemes, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to a Spannable turning the matches into links. |
static
final
boolean
|
addLinks(Spannable text, Pattern pattern, String scheme)
Applies a regex to a Spannable turning the matches into links. |
static
final
void
|
addLinks(TextView text, Pattern pattern, String scheme)
Applies a regex to the text of a TextView turning the matches into links. |
static
final
void
|
addLinks(TextView text, Pattern pattern, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to the text of a TextView turning the matches into links. |
static
final
void
|
addLinks(TextView text, Pattern pattern, String defaultScheme, String[] schemes, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to the text of a TextView turning the matches into links. |
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. |
static
final
boolean
|
addLinks(Spannable spannable, Pattern pattern, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to a Spannable turning the matches into links. |
static
final
boolean
|
addLinks(TextView text, int mask)
Scans the text of the provided TextView and turns all occurrences of the link types indicated in the mask into clickable links. |
static
Future<Void>
|
addLinksAsync(TextView textView, TextLinks.Options options)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by |
static
Future<Void>
|
addLinksAsync(Spannable text, TextClassifier classifier, int mask)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by the link |
static
Future<Void>
|
addLinksAsync(Spannable text, TextClassifier classifier, TextLinks.Options options)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by |
static
Future<Void>
|
addLinksAsync(Spannable text, TextClassifier classifier, TextLinks.Options options, Executor executor, Consumer<Integer> callback)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by |
static
Future<Void>
|
addLinksAsync(TextView textView, TextLinks.Options options, Executor executor, Consumer<Integer> callback)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by |
Inherited methods | |
---|---|
Constants
ALL
int ALL
Bit mask indicating that all available patterns should be matched in methods that take an options mask
Constant Value: 15 (0x0000000f)
EMAIL_ADDRESSES
int EMAIL_ADDRESSES
Bit field indicating that email addresses should be matched in methods that take an options mask
Constant Value: 2 (0x00000002)
MAP_ADDRESSES
int MAP_ADDRESSES
Bit field indicating that street addresses should be matched in methods that
take an options mask. Note that this uses the
findAddress()
method in
WebView
for finding addresses, which has various
limitations.
Constant Value: 8 (0x00000008)
PHONE_NUMBERS
int PHONE_NUMBERS
Bit field indicating that phone numbers should be matched in methods that take an options mask
Constant Value: 4 (0x00000004)
WEB_URLS
int WEB_URLS
Bit field indicating that web URLs should be matched in methods that take an options mask
Constant Value: 1 (0x00000001)
Fields
sPhoneNumberMatchFilter
Linkify.MatchFilter sPhoneNumberMatchFilter
Filters out URL matches that don't have enough digits to be a phone number.
sPhoneNumberTransformFilter
Linkify.TransformFilter sPhoneNumberTransformFilter
Transforms matched phone number text into something suitable to be used in a tel: URL. It does this by removing everything but the digits and plus signs. For instance: '+1 (919) 555-1212' becomes '+19195551212'
sUrlMatchFilter
Linkify.MatchFilter sUrlMatchFilter
Filters out web URL matches that occur after an at-sign (@). This is to prevent turning the domain name in an email address into a web link.
Public constructors
Public methods
addLinks
boolean addLinks (Spannable spannable, Pattern pattern, String defaultScheme, String[] schemes, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to a Spannable turning the matches into links.
Parameters | |
---|---|
spannable |
Spannable : Spannable whose text is to be marked-up with links.This value must never be |
pattern |
Pattern : Regex pattern to be used for finding links.This value must never be |
defaultScheme |
String : The default scheme to be prepended to links if the link does not
start with one of the schemes given.This value may be |
schemes |
String : Array of schemes (eg http:// ) to check if the link found
contains a scheme. Passing a null or empty value means prepend defaultScheme
to all links. |
matchFilter |
Linkify.MatchFilter : The filter that is used to allow the client code additional control
over which pattern matches are to be converted into links.This value may be |
transformFilter |
Linkify.TransformFilter : Filter to allow the client code to update the link found.This value may be |
Returns | |
---|---|
boolean |
True if at least one link is found and applied. |
addLinks
boolean addLinks (Spannable text, Pattern pattern, String scheme)
Applies a regex to a Spannable turning the matches into links.
Parameters | |
---|---|
text |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
pattern |
Pattern : Regex pattern to be used for finding linksThis value must never be |
scheme |
String : URL scheme string (eg http:// ) to be
prepended to the links that do not start with this scheme.
This value may be |
Returns | |
---|---|
boolean |
addLinks
void addLinks (TextView text, Pattern pattern, String scheme)
Applies a regex to the text of a TextView turning the matches into links. If links are found then UrlSpans are applied to the link text match areas, and the movement method for the text is changed to LinkMovementMethod.
Parameters | |
---|---|
text |
TextView : TextView whose text is to be marked-up with linksThis value must never be |
pattern |
Pattern : Regex pattern to be used for finding linksThis value must never be |
scheme |
String : URL scheme string (eg http:// ) to be
prepended to the links that do not start with this scheme.
This value may be |
addLinks
void addLinks (TextView text, Pattern pattern, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to the text of a TextView turning the matches into links. If links are found then UrlSpans are applied to the link text match areas, and the movement method for the text is changed to LinkMovementMethod.
Parameters | |
---|---|
text |
TextView : TextView whose text is to be marked-up with linksThis value must never be |
pattern |
Pattern : Regex pattern to be used for finding linksThis value must never be |
scheme |
String : URL scheme string (eg http:// ) to be
prepended to the links that do not start with this scheme.This value may be |
matchFilter |
Linkify.MatchFilter : The filter that is used to allow the client code
additional control over which pattern matches are
to be converted into links.
This value may be |
transformFilter |
Linkify.TransformFilter This value may be |
addLinks
void addLinks (TextView text, Pattern pattern, String defaultScheme, String[] schemes, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to the text of a TextView turning the matches into links. If links are found then UrlSpans are applied to the link text match areas, and the movement method for the text is changed to LinkMovementMethod.
Parameters | |
---|---|
text |
TextView : TextView whose text is to be marked-up with links.This value must never be |
pattern |
Pattern : Regex pattern to be used for finding links.This value must never be |
defaultScheme |
String : The default scheme to be prepended to links if the link does not
start with one of the schemes given.This value may be |
schemes |
String : Array of schemes (eg http:// ) to check if the link found
contains a scheme. Passing a null or empty value means prepend defaultScheme
to all links. |
matchFilter |
Linkify.MatchFilter : The filter that is used to allow the client code additional control
over which pattern matches are to be converted into links.This value may be |
transformFilter |
Linkify.TransformFilter : Filter to allow the client code to update the link found.
This value may be |
addLinks
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.
Parameters | |
---|---|
text |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
mask |
int : Mask to define which kinds of links will be searched. |
Returns | |
---|---|
boolean |
True if at least one link is found and applied. |
addLinks
boolean addLinks (Spannable spannable, Pattern pattern, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
Applies a regex to a Spannable turning the matches into links.
Parameters | |
---|---|
spannable |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
pattern |
Pattern : Regex pattern to be used for finding linksThis value must never be |
scheme |
String : URL scheme string (eg http:// ) to be
prepended to the links that do not start with this scheme.This value may be |
matchFilter |
Linkify.MatchFilter : The filter that is used to allow the client code
additional control over which pattern matches are
to be converted into links.This value may be |
transformFilter |
Linkify.TransformFilter : Filter to allow the client code to update the link found.This value may be |
Returns | |
---|---|
boolean |
True if at least one link is found and applied. |
addLinks
boolean addLinks (TextView text, int mask)
Scans the text of the provided TextView and turns all occurrences of the link types indicated in the mask into clickable links. If matches are found the movement method for the TextView is set to LinkMovementMethod.
Parameters | |
---|---|
text |
TextView : TextView whose text is to be marked-up with linksThis value must never be |
mask |
int : Mask to define which kinds of links will be searched. |
Returns | |
---|---|
boolean |
True if at least one link is found and applied. |
addLinksAsync
Future<Void> addLinksAsync (TextView textView, TextLinks.Options options)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by options
into clickable links. If links are found, this method
removes any pre-existing TextLinks.TextLinkSpan
attached to the text (to avoid
problems if you call it repeatedly on the same text) and sets the movement method for the
TextView to LinkMovementMethod.
Note: This method returns immediately but generates the links with the specified classifier on a background thread. The generated links are applied on the calling thread.
This method must be called on the thread that originally created this UI element. This is typically the main thread of your app.
Parameters | |
---|---|
textView |
TextView : TextView whose text is to be marked-up with linksThis value must never be |
options |
TextLinks.Options : optional parameters to specify how to generate the linksThis value may be |
Returns | |
---|---|
Future<Void> |
a future that may be used to interrupt or query the background task |
addLinksAsync
Future<Void> addLinksAsync (Spannable text, TextClassifier classifier, int mask)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by the link mask
into clickable links. If links are found, this method
removes any pre-existing TextLinks.TextLinkSpan
attached to the text to avoid
problems if you call it repeatedly on the same text.
Note: This method returns immediately but generates the links with the specified classifier on a background thread. The generated links are applied on the calling thread.
Note: If the text is currently attached to a TextView, this method should be called on the UI thread.
Parameters | |
---|---|
text |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
classifier |
TextClassifier : the TextClassifier to use to generate the linksThis value must never be |
mask |
int : mask to define which kinds of links will be generated |
Returns | |
---|---|
Future<Void> |
a future that may be used to interrupt or query the background task |
addLinksAsync
Future<Void> addLinksAsync (Spannable text, TextClassifier classifier, TextLinks.Options options)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by options
into clickable links. If links are found, this method
removes any pre-existing TextLinks.TextLinkSpan
attached to the text to avoid
problems if you call it repeatedly on the same text.
Note: This method returns immediately but generates the links with the specified classifier on a background thread. The generated links are applied on the calling thread.
Note: If the text is currently attached to a TextView, this method should be called on the UI thread.
Parameters | |
---|---|
text |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
classifier |
TextClassifier : the TextClassifier to use to generate the linksThis value must never be |
options |
TextLinks.Options : optional parameters to specify how to generate the linksThis value may be |
Returns | |
---|---|
Future<Void> |
a future that may be used to interrupt or query the background task |
addLinksAsync
Future<Void> addLinksAsync (Spannable text, TextClassifier classifier, TextLinks.Options options, Executor executor, Consumer<Integer> callback)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by options
into clickable links. If links are found, this method
removes any pre-existing TextLinks.TextLinkSpan
attached to the text to avoid
problems if you call it repeatedly on the same text.
Note: This method returns immediately but generates the links with the specified classifier on a background thread. The generated links are applied on the calling thread.
Note: If the text is currently attached to a TextView, this method should be called on the UI thread.
Parameters | |
---|---|
text |
Spannable : Spannable whose text is to be marked-up with linksThis value must never be |
classifier |
TextClassifier : the TextClassifier to use to generate the linksThis value must never be |
options |
TextLinks.Options : optional parameters to specify how to generate the linksThis value may be |
executor |
Executor : Executor that runs the background taskThis value may be |
callback |
Consumer : Callback that receives the final status of the background task executionThis value may be |
Returns | |
---|---|
Future<Void> |
a future that may be used to interrupt or query the background task |
addLinksAsync
Future<Void> addLinksAsync (TextView textView, TextLinks.Options options, Executor executor, Consumer<Integer> callback)
Scans the text of the provided TextView and turns all occurrences of the entity types
specified by options
into clickable links. If links are found, this method
removes any pre-existing TextLinks.TextLinkSpan
attached to the text (to avoid
problems if you call it repeatedly on the same text) and sets the movement method for the
TextView to LinkMovementMethod.
Note: This method returns immediately but generates the links with the specified classifier on a background thread. The generated links are applied on the calling thread.
This method must be called on the thread that originally created this UI element. This is typically the main thread of your app.
Parameters | |
---|---|
textView |
TextView : TextView whose text is to be marked-up with linksThis value must never be |
options |
TextLinks.Options : optional parameters to specify how to generate the linksThis value may be |
executor |
Executor : Executor that runs the background taskThis value may be |
callback |
Consumer : Callback that receives the final status of the background task executionThis value may be |
Returns | |
---|---|
Future<Void> |
a future that may be used to interrupt or query the background task |
Interfaces
Classes