Friday, June 8, 2012

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"

where @string/txtCredits is a string resource that contains Link text.

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.

Source: Tips4all


  1. 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.

    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:layout_below="@+id/imgCredits" android:layout_centerInParent="true"

    That solved it. Pretty difficult to uncover and fix.

  2. 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.

  3. The above solutions didn't work for me, but the following did (and it seems a bit cleaner).
    First, in the string resource, define your tag opening chevrons using the HTML entity encoding, i.e.:

    &lt;a href="">Google&lt;/a>

    and NOT:

    <a href="">Google</a>

    In general, encode all the chevrons in the string like that. BTW, the link must start with http://

    Then (as suggested here) set this option on your TextView:


    Finally, in code, do:

    ((TextView) findViewById(;
    ((TextView) findViewById(;

    That's it, no regexes or other manual hacks required.

  4. The reason you're having the problem is that it only tries to match "naked" addresses. things like "" or "".

    Running your text through Html.fromHtml() should do the trick. You have to do it programatically, but it works.

  5. Only what do you need to add this in text view in xml



  6. This is how I solved clickable and Visible links in a TextView (by code)

    private void setAsLink(TextView view, String url){
    Pattern pattern = Pattern.compile(url);
    Linkify.addLinks(view, pattern, "http://");
    view.setText(Html.fromHtml("<a href='http://"+url+"'>http://"+url+"</a>"));

  7. Be sure to not use setAutoLinkMask(Linkify.ALL) when using setMovementMethod(LinkMovementMethod.getInstance()) and Html.fromHTML() on properly formatted HTML links (for example, <a href="">Google</a>).