Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android – Creating links using linkify

Posted on 27/01/2011
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); 
mainLayout.addView(myLocation);



Email address:

TextView myEmail= (TextView) findViewById(R.id.my_web_site);
myEmail.setText(“aviyehuda@gmail.com”);
Linkify.addLinks(myEmail  , Linkify.EMAIL_ADDRESSES);





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

Linkify.addLinks(myTextView , Linkify.ALL);

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

Linkify.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=");
mainLayout.addView(myCustomLink);



MatchFilter:
MatchFilter is used for more complicated filters.

MatchFilter myMatchFilter = new MatchFilter() {
	@Override
	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);
mainLayout.addView(myCustomLink2);



TransformFilter:
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() {
	@Override
	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);
mainLayout.addView(myCustomLink3);



Download code example

10 thoughts on “Android – Creating links using linkify”

  1. egofall says:
    30/01/2011 at 04:43

    man this is cool.

    Reply
  2. beginner says:
    29/04/2011 at 13:47

    I use

    TextView myWebSite = (TextView) findViewById(R.id.my_web_site);
    myWebSite.setText(“http://http://www.google.es/”);
    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?

    Reply
    1. admin says:
      05/05/2011 at 07:43

      What is written in the logger?

      Reply
  3. Carlos says:
    05/05/2013 at 18:56

    Thank you very much! 😀

    Reply
  4. Sayeed says:
    06/06/2013 at 08:56

    This is the most lucid explanation among all other sites . Thanks a ton.

    Reply
  5. Pingback: Click-able address android - Android Forums
  6. Pingback: Call Phone Number from App - Android Forums
  7. Albert Nguyen says:
    08/01/2014 at 08:27

    It ‘s very useful

    Reply
  8. VIjay says:
    09/12/2014 at 01:30

    Thanks for writting this up.

    Reply
  9. Pratidnya says:
    16/03/2016 at 06:56

    TextView myLocation = new TextView(this);
    myLocation.setText(“436 Mayfield Ave, Stanford, CA”);
    Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
    mainLayout.addView(myLocation);

    Tried the code. All options work except for MAP_Addresses. It doesnot show with a clickable text. Is any googlemap api key activation is required?

    Reply

Leave a Reply Cancel reply

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


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme