Richard Richard - 1 year ago 169
Android Question

How do I make links in a TextView clickable?

I have the following TextView defined:

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:autoLink="web" android:id="@+id/infoTxtCredits"

is a string resource that contains
<a href="some site">Link text</a>

Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me what I'm doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?

Looks like it has to do with the way I define my string resource.
This does not work:

<string name="txtCredits"><a href="">Google</a></string>

But this does:

<string name="txtCredits"></string>

Which is a bummer because I would much rather show a text link than show the full URL.

Answer Source

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

    // 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(;

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


That solved it. Pretty difficult to uncover and fix.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download