How to make links in a TextView clickable How to make links in a TextView clickable android android

How to make links in a TextView clickable


Buried in the API demos, I found the solution to my problem:

File Link.java:

    // text2 has links specified by putting <a> tags in the string    // resource.  By default these links will appear but not    // respond to user input.  To make them active, you need to    // call setMovementMethod() on the TextView object.    TextView t2 = (TextView) findViewById(R.id.text2);    t2.setMovementMethod(LinkMovementMethod.getInstance());

I removed most of the attributes on my TextView to match what was in the demo.

<TextView    android:id="@+id/text2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/txtCredits"/>

That solved it. It is pretty difficult to uncover and fix.

Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().


I'm using only android:autoLink="web" and it works fine. A click on the link opens the browser and shows the correct page.

One thing I could guess is that some other view is above the link. Something that is transparent fills the whole parent but don't displays anything above the link. In this case the click goes to this view instead of the link.


After spending some time with this, I have found that:

  • android:autoLink="web" works if you have full links in your HTML. The following will be highlighted in blue and clickable:
  • Some text <a href="http://www.google.com">http://www.google.com</a>
  • Some text http://www.google.com
  • view.setMovementMethod(LinkMovementMethod.getInstance()); will work with the following (will be highlighted and clickable):
  • Some text <a href="http://www.google.com">http://www.google.com</a>
  • Some text http://www.google.com
  • Some text <a href="http://www.google.com">Go to Google</a>

Note that the third option has a hyperlink, but the description of the link (the part between the tags) itself is not a link. android:autoLink="web" does NOT work with such links.

  • android:autoLink="web" if set in XML will override view.setMovementMethod(LinkMovementMethod.getInstance()); (i.e.; links of the third kind will be highlighted, but not clickable).

The moral of the story is use view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and make sure you don't have android:autoLink="web" in your XML layout if you want all links to be clickable.