Creating Custom Link Strings

To define your own linkify strings you create a new RegEx pattern to match the text you want to display as hyperlinks.

As with the native types, you linkify the target View by calling Linkify.addLinks, but this time pass in the new RegEx pattern. You can also pass in a prefix that will be prepended to the target URI when a link is clicked.

Listing 5-14 shows a View being linkified to support earthquake data provided by an Android Content Provider (that you will create in Chapter 7). Rather than include the entire schema, the linkify pattern matches any text that starts with ''quake'' and is followed by a number. The content schema is then prepended to the URI before the Intent is fired.

LISTING 5-14: Creating custom link strings in Linkify Available for downloadon int flags = Pattern.CASE_INSENSITIVE;

Wrox.com Pattern p = Pattern.compile("\\bquake[0-9]*\\b", flags); Linkify.addLinks(myTextView, p,

"content://com.paad.earthquake/earthquakes/")

Linkify also supports TransformFilter and MatchFilter interfaces. These offer additional control over the target URI structure and the definition of matching strings, and are used as in the following skeleton code:

Linkify.addLinks(myTextView, pattern, prefixWith, new MyMatchFilter(), new MyTransformFilter());

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

  • Veera
    How do i create my own custom linkification in android programming?
    1 year ago

Post a comment