9

I would like to know if and how it is possible to add a web link to a TextView widget. In my app, i show some text and adjacent to this text an image. I would like to insert a clickable internet link in the text. Is this possible?

Marco Masci
  • 818
  • 10
  • 22

3 Answers3

11

You just need to set the android:autolink property.

<TextView
        android:autoLink="web"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:text="http://www.google.com" />
The Real Baumann
  • 1,941
  • 1
  • 14
  • 20
0

In case your web link is different from the text you are showing in the TextView:

The TextView in your layout file:

<TextView
    android:id="@+id/textview_with_hidden_clickable_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_with_text_and_link"/>

Your string in your resource file:

<string name="string_with_text_and_link">
    <a href="http://any_web_site">The text in your TextView</a>
</string>

And in your Java class:

((TextView)findViewById(R.id.textview_with_hidden_clickable_link))
    .setMovementMethod(LinkMovementMethod.getInstance());

NOTE: http:// in the string resource is necessary.

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
0

This is how I did it 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>"));
    }
Dominic
  • 3,353
  • 36
  • 47