Reputation: 818
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?
Upvotes: 9
Views: 7404
Reputation: 1
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.
Upvotes: 0
Reputation: 3473
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>"));
}
Upvotes: 0
Reputation: 1961
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" />
Upvotes: 11