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)

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • use a clickable span for this purpose. http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring – Raghunandan Apr 15 '14 at 09:01

4 Answers4

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>"));
Hariharan
  • 24,741
  • 6
  • 50
  • 54
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/

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
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

Karthik
  • 2,282
  • 2
  • 22
  • 23
-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.