Gilad Neiger
Gilad Neiger

Reputation: 643

Is it possible to make an automatic recognize for links?

My application is kind of a social app. My problem is that if someone uploads a text with a link in it, the app doesn't recognize the link and the link isn't clickable.

I thought to myself that maybe there is a simple way to make my app recognize a link and make it clickable.

Upvotes: 0

Views: 215

Answers (1)

SkyWalker
SkyWalker

Reputation: 29168

You can use this code for auto detecting in string.

String originalString = "Please go to http://www.stackoverflow.com";
String newString = originalString.replaceAll("http://.+?(com|net|org)/{0,1}", "<a href=\"$0\">$0</a>");

Resource Link:

How to detect the presence of URL in a string

UPDATE1:

Enabling support for one of Android’s default link patterns is very easy. Simply use the addLinks(TextView text, int mask) function and specify a mask that describes the desired link types.

import android.text.util.Linkify;

// Recognize phone numbers and web URLs
Linkify.addLinks(text, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);

// Recognize all of the default link text patterns 
Linkify.addLinks(text, Linkify.ALL);

Resource Link:

  1. Android Text Links Using Linkify
  2. Linkify and autoLink Need a Custom URLSpan

For more details, you can go through this tutorial: Android Linkify both web and @mentions all in the same TextView

Upvotes: 1

Related Questions