My application loads comments from an API which often contain links with the same markup as here on Stack Overflow. (If there is a name for this markup style to help me Google it, please let me know in comments)
//this is the markup I am referring to
[Here's a picture](https://www.web.com/sub/path/to/picture/?st=JHTYA46&am-p;sh=487Bac48)
I tried converting them to links with
private static final String REGEX_LINK_MARKUP = "\\[(.*?)\\]\\((.*?)\\)";
private static final String REGEX_LINK_REPLACEMENT = "<a href=\"$2\">$1</a>";
commentText.setText(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT)));
and using
android:autoLink="all"
But of course that showed the HTML with the href part clickable so I am currently converting them to links with
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//the constants are the same patterns from above
commentText.setText(Html.fromHtml(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT), Html.FROM_HTML_MODE_LEGACY));
} else {
commentText.setText(Html.fromHtml(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT)));
}
I now see the correctly coloured span, but it isn't clickable. The field already has
android:linksClickable="true"
Whether I leave the following to none
or all
makes no difference (the link is unclickable)
android:autoLink="none"
- What is the correct way to make this type of markup clickable in a TextView?
- Is there a way to make TextView links without using HTML?
- Is there a more efficient regex than my very basic pattern?