How to make all kind of links clickable in Android textview from Rich text/html string

Viewed 108

I have some rich texts returned from api which may have phone-numbers, raw text-links, hyperlinks email. I want to make everything clickable using Android textview.

for an example :

<p>Hello <i>Mr.Foo</i>.<br>Thank you for visiting us.<br>
<br>For more details, click this link:&nbsp;<a href="https://example.com/" target="_blank">My Web Link</a>
<br> You can also visit another website: www.anotherweb.com <br><br> 
<b>For any enquiries:<br>
<br>Mr.Boo</b>
<br>Contact: (65) 1020 2401 / +6512345678<br>
Email: booking@example.com<br><br>
</p>

As you can see there are

  • 1 hyperlink with <a href=""> tag,
  • 1 raw text web link ( www.anotherweb.com )
  • 1 email address without <a href="mailto:.. tag)
  • 2 phone numbers

I tried to use following (along with Html.fromHtml() )

  1. Textview.setMovementMethod(LinkMovementMethod.getInstance()) :

    Hyperlinks become clickable but email, phone number or raw text links unable to click.

  2. Used autoLink="all" in Textview :

    Everything else worked except hyperlinks( the one with <a href=""> become not clickable)

  3. Used Linkify.addLinks(textview, Linkify.ALL) :

    Again unable to click on hyperlinks but others worked fine.

I tried also combination of these but couldn't make everything clickable at once. Is there any other way exist?

0 Answers
Related