Textview With Multiple Hyperlinks
I have for example the following String \n 3 Doors Down is a http://www.last.fm/tag/post-grunge\' class=\'bbcode_tag\' rel=\'tag\'>post-grunge band from Esca
Solution 1:
Use below code.
TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());
String content = tv.getText().toString();
List<String> links = new ArrayList<String>();
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(content);
while (m.find()) {
String urlStr = m.group();
links.add(urlStr);
}
SpannableString f = new SpannableString(content);
for (int i = 0; i < links.size(); i++) {
final String url = links.get(i);
f.setSpan(new InternalURLSpan(new OnClickListener() {
public void onClick(View v) {
Context ctx = v.getContext();
String urlToOpen = url;
if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
urlToOpen = "http://" + urlToOpen;
openURLInBrowser(urlToOpen, ctx);
}
}), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(f);
Post a Comment for "Textview With Multiple Hyperlinks"