Reputation: 643
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
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>");
How to detect the presence of URL in a string
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);
For more details, you can go through this tutorial: Android Linkify both web and @mentions all in the same TextView
Upvotes: 1