Reputation: 438
The following is the text that I have to set in text view. I wanted to open webview on click of hyperlink. Rest of the text should not be clickable.
String value = "Check on this link: <a href="http://www.google.com">Go to Google</a>";
binding.text.setText(value);
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:autoLink="web"
android:textColorLink="@color/g_turquoise_blue" />
Thanks in advance.
Upvotes: 0
Views: 607
Reputation: 21
This will help you if i got you correct
yourTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
UPDATE: Try this
/**
* Returns a list with all links contained in the input
*/
public static List<String> extractUrls(String text)
{
List<String> containedUrls = new ArrayList<String>();
String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
Matcher urlMatcher = pattern.matcher(text);
while (urlMatcher.find())
{
containedUrls.add(text.substring(urlMatcher.start(0),
urlMatcher.end(0)));
}
return containedUrls;
}
Example:
List<String> extractedUrls = extractUrls("Welcome to https://stackoverflow.com/ and here is another link http://www.google.com/ \n which is a great search engine");
for (String url : extractedUrls)
{
System.out.println(url);
}
Prints:
https://stackoverflow.com/
http://www.google.com/
source: Detect and extract url from a string?
Upvotes: 0