73

This seems like a trivial problem, but it has me kind of stumped. I want to load an HTML string using Html.fromHtml(), and have any links in the string to be clickable and open in the browser.

Basic example:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));

With this snippet, the text is formatted as if it were a link (blue, underlined), but it's not clickable. I tried Linkify, but it only seems to work with links that are not HTML-based.

Any suggestions?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Gunnar Lium
  • 6,023
  • 9
  • 35
  • 33

5 Answers5

165

As I assumed, the solution was trivial:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

The second line somehow activates the link behavior, although I'm not quite sure how. The same question is addressed over at Google Code.

Gunnar Lium
  • 6,023
  • 9
  • 35
  • 33
  • 1
    Note I tried this with referencing a String.xml resource. this WONT work. :-) – Blundell Apr 08 '11 at 15:19
  • 4
    Use CDATA for strings.xml to avoid links in there to be treated at subtags. – Artem Russakovskii Jun 06 '11 at 21:43
  • 26
    Just want to point out, if this isnt working for you, make sure you dont have the android:autoLink="web" tag in your textview xml, i removed mine and the links started working fine. EDIT: and it does work on 4.0+ – Glenn.nz Aug 27 '12 at 03:30
  • Work on 4.1. I'm just pass straight html and I am not pulling strings from res. – MinceMan Jun 05 '13 at 19:03
  • You cannot use the binding: "binding.txt.setMovementMethod(...)", please cast into TextView to work: "TextView txt = binding.txt; txt.setMovementMethod(...)" – Ben Jul 06 '22 at 08:52
27

As mentioned in other answers, a way forward is to use:

xtView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

However, this won't work if you have ANY android:autoLink value set, not just 'web' as other comments seem to suggest. So that means you can use this solution to linkify URLs at the expense of having phone, email and maps being disabled/unlinked.

Charlie Tabone
  • 332
  • 4
  • 6
  • This is the the correct answer for situations where you're using Html.fromHtml(). It expands upon the selected answer. – wblaschko Jan 11 '15 at 04:54
  • Better answer than the chosen one, this addresses an issue that exists in case user has `autoLink` set. – Shadow Feb 08 '19 at 14:41
9

The javadoc of the LinkMovementMethod says that it

Supports clicking on links with DPad Center or Enter.

So it makes sense that works that way.

And confirmed, with 4.2.2 works like charm with just the

textView.setMovementMethod(LinkMovementMethod.getInstance());
woliveirajr
  • 9,433
  • 1
  • 39
  • 49
Caye
  • 329
  • 4
  • 5
3

It should be this way:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setLinksClickable(true);

in XML should be

<TextView
    android:id="@+id/txtview"
    android:autoLink="web"
    android:linksClickable="true"
    />
Umakant Patil
  • 2,227
  • 6
  • 32
  • 58
0
String data="MyTest";

textView.setText(data);
textView.setText(Html.fromHtml(data));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
coffeemakr
  • 330
  • 2
  • 12
M.Ganji
  • 818
  • 8
  • 13