Sunday, June 17, 2012

How do I make links in a TextView clickable?

When I wanted to show some TextView with click able and make it as a link to open a web pages in separate browser.
Android is highlighting the links in the TextView, but they do not respond to clicks

 Lets example - I wanted to show my website -

 <TextView
                android:id="@+id/what_is_my_website_url"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="http://android-expert.blogspot.in"
                android:textSize="12dip" />

 But this "URLs" is not clickable, hence I changed my code (check bold colored clickable)-

 <TextView
                android:id="@+id/what_is_my_website_url"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:linksClickable="true"
                android:text="http://android-expert.blogspot.in"
                android:textSize="12dip" />

Even though it wouldn't work, it will clickable but it wouldn't open any browser so -

hence I changed my code (check bold colored clickable)-


<TextView
                android:id="@+id/what_is_my_website_url"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:linksClickable="true"
                android:text="http://android-expert.blogspot.in"
                android:textSize="12dip" />

 Now, it will open web pages, but here   android:linksClickable="true" not required.

Some other solution and suggestion to user. -

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

Sometimes android:autoLink="web" would work for that situation -

&lt;a href="http://www.google.com">Google&lt;/a>
Not like below 
<a href="http://www.google.com">Google</a>
  
In general, encode all the chevrons in the string like that. BTW, the link must start with http://
Then set this option on your TextView:

 android:linksClickable="true"
 
Finally, in code, do:

((TextView)findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
  ((TextView)findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));

 
Note - 
 
This will create an HTML Parser everytime this code is executed. Be aware of that if you use this to create links 
inside a list item or at other performance critical places in your app.</br> 
During creation of a listitem this increases the execution time of my getView Methods off 50% 

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>")); 
    }
 
 
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="http://www.google.com/">Google</a>).