I have a TextView
that gets it\'s data set by calling this:
tv.setText(Html.fromHtml(myText));
The string myText
cont
100% works solution (kotlin).
Create class for store HtmlLink before Linkify
class HtmlLink(val urlSpan: URLSpan, val spanStart: Int, val spanEnd: Int)
Create spanned html (both formats for test)
val spanned = Html.fromHtml("https://google.com" +
"
Google")
Store html
val htmlLinks = ArrayList()
spanned.getSpans(0, spanned.length, URLSpan::class.java).forEach { urlSpan ->
htmlLinks.add(HtmlLink(urlSpan,
spanned.getSpanStart(urlSpan),
spanned.getSpanEnd(urlSpan)))
}
Create spannable builder and Linkify it
val builder = SpannableString(spanned)
Linkify.addLinks(builder, Linkify.WEB_URLS)
Restore spans.
htmlLinks.forEach { htmlLink ->
builder.setSpan(URLSpan(htmlLink.urlSpan.url), htmlLink.spanStart, htmlLink.spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
Set finally text
scrollContent.text = builder