Avi is a senior developer with a passion for Java technologies… http://www.aviyehuda.com/ Avi is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

Android – Creating links using linkify

  • submit to reddit

android   Linkify is a class that lets you create links from a TextView or a Spannable.
You can create links not just to web pages, but also to locations on the map, emails and even phone numbers.

*Note: the examples bellow may not always work in the emulator.

Web address:

TextView myWebSite = new TextView(this);
myWebSite .setText("http://http://www.dzone.com/");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);

Phone number:

TextView myPhone = (TextView) findViewById(R.id.my_web_site);
myPhone .setText(“5552323233”);
Linkify.addLinks(myPhone , Linkify.PHONE_NUMBERS);

Map address:

TextView myLocation = new TextView(this);
myLocation.setText("436 Mayfield Ave, Stanford, CA");
Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);

Email address:

TextView myEmail= (TextView) findViewById(R.id.my_web_site);
Linkify.addLinks(myEmail , Linkify.PHONE_NUMBERS);

Auto detect:
Use Linkify.ALL to automatically detect the link type.

1Linkify.addLinks(myTextView , Linkify.ALL);

More than one option:
You can choose more than a single option for the link type.

1Linkify.addLinks(myTextView, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);

Using pattern:
You can use a regular expression for detecting text parts and transform only them to links instead of the whole text.

 TextView myCustomLink = new TextView(this);
Pattern pattern = Pattern.compile("[a-zA-Z]+&");
myCustomLink.setText("press Linkify& or on Android& to search it on google");
Linkify.addLinks(myCustomLink,pattern, "http://www.google.ie/search?q=");

MatchFilter is used for more complicated filters.

MatchFilter myMatchFilter = new MatchFilter() {
public boolean acceptMatch(CharSequence cs, int start, int end) {
return start > 48;

TextView myCustomLink2 = new TextView(this);
Pattern pattern2 = Pattern.compile("[a-zA-Z]+");
myCustomLink2.setText("press one of these words to search it on google: Android Linkify dzone");
Linkify.addLinks(myCustomLink2,pattern2, "http://www.google.ie/search?q=", myMatchFilter, null);

So fat the text we have filtered stayed the same. But sometimes you need the text to be different than the text which is appended to the link.

  TransformFilter myTransformFilter = new TransformFilter() {
public String transformUrl(Matcher match, String url) {
return url.substring(1); //remove the $ sign

TextView myCustomLink3 = new TextView(this);
Pattern pattern3 = Pattern.compile("\\$[a-zA-Z]+");
myCustomLink3.setText("press $Linkify or on $Android to search it on google");
Linkify.addLinks(myCustomLink3,pattern3, "http://www.google.ie/search?q=", null, myTransformFilter);

 Download code example

Published at DZone with permission of Avi Yehuda, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Thomas Kern replied on Thu, 2012/09/06 - 10:53am

I use

TextView myWebSite = (TextView) findViewById(R.id.my_web_site);
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);

and in .xml

but don´t work in my aplication
the emulator said:

The application has stopped unexpectedly. please try again

What I am doing wrong?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.