I have HTML content that has web URLs, email addresses, and phone numbers. When I use the below code, only the email addresses are becoming links and not the web URLs. What can be the issue? I had tried with Linkify.ALL too. But didn't work.
val htmlSpannable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(content)
}
val spannableBuilder = SpannableStringBuilder(htmlSpannable)
val bulletSpans = spannableBuilder.getSpans(
0,
spannableBuilder.length,
BulletSpan::class.java
)
bulletSpans.forEach {
val start = spannableBuilder.getSpanStart(it)
val end = spannableBuilder.getSpanEnd(it)
spannableBuilder.removeSpan(it)
spannableBuilder.setSpan(
ImprovedBulletSpan(bulletRadius = dip(3, context), gapWidth = dip(8, context)),
start,
end,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
}
textView.text = spannableBuilder.trimEnd()
Linkify.addLinks(textView, Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS)
textView.movementMethod = LinkMovementMethod.getInstance()
Linkify.addLinks removes existing UrlSpan, so in the given example, the links which are generated in HTML spannable get removed once Linkify is applied. However, both methods can be combined. First, converting from HTML, then saving these spans, linkify-ing to get phone numbers, etc., and re-applying HTML spans later.
Using provided example:
Removed the bullet span from example, as it's custom, so need to readd that back in.
In this scenario, it might be good to also to test further what happens if you have a link like
"<a href=\"https://www.google.com\">https://www.google.com</a>", so span would be applied again.