1

I have a TextView in layout

 <TextView
    android:id="@+id/homepage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

I would like to set a string with HTML link in this TextView, and make the link clickable (open the link on browser when clicked). I tried the following:

I defined the string in resource, the string contains a HTML link to google website:

<string name="home_page">please go to &lt;a ref="www.google.com">www.google.com!&lt;/a>.

In my Activity:

TextView homepage =  (TextView)findViewById(R.id.homepage);

String text = getString(R.string.home_page)
CharSequence styledText = Html.fromHtml(text);

homepage.setText(styledText.toString());

The result is please go to www.google.com , but the www.google.com is not a clickable link. How to make it clickable? (I mean open the link in browser when clicked)

1

4 Answers 4

3

Try this..

Don't forget to use http:// before www. otherwise you will get ActivityNotFoundException

TextView homepage =  (TextView)findViewById(R.id.homepage);
homepage.setMovementMethod(LinkMovementMethod.getInstance());
homepage.setText(Html.fromHtml("<font color='#696969'> please go to <a href=\""+"http://www.google.com"+"\">"+"http://www.google.com"+"</a></font>"));
1

below code work for me:-

<TextView
    android:id="@+id/txt_post_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:textSize="16sp" />

Autolink inside a TextView in android

http://alltechsolution.wordpress.com/2012/06/17/how-do-i-make-links-in-a-textview-clickable/

http://www.technotalkative.com/android-textview-autolink-attribute/

0

All the code to do this is described in this blog.. check it http://android-developers.blogspot.fi/2008/03/linkify-your-text.html

-1
<TextView 
        android:id="@+id/links"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:autoLink="web" 
        android:text="www.google.com" />

Try this code it's worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.