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?
Asked
Active
Viewed 7,304 times
9
-
1http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable – Vic Vuci Jan 24 '12 at 18:35
3 Answers
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

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