DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Auditing Tools for Kubernetes
  • Redefining DevOps: The Transformative Power of Containerization
  • Insider Threats and Software Development: What You Should Know
  • Real-Time Made Easy: An Introduction to SignalR
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android – Creating links using linkify

Android – Creating links using linkify

Avi Yehuda user avatar by
Avi Yehuda
·
Jan. 28, 11 · News
Like (0)
Save
Tweet
Share
17.56K Views

Join the DZone community and get the full member experience.

Join For Free

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.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=");
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












Links Android (robot)

Published at DZone with permission of Avi Yehuda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Auditing Tools for Kubernetes
  • Redefining DevOps: The Transformative Power of Containerization
  • Insider Threats and Software Development: What You Should Know
  • Real-Time Made Easy: An Introduction to SignalR

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: