Adding a web link to a TextView widget

I would like to know if a web link can be added to a TextView widget. In my application, I display text and an image is adjacent to this text. I would like to insert an interactive link with interactive access into the text. Is it possible?

+8
android textview hyperlink
source share
3 answers

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" /> 
+10
source share

Here's 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>")); } 
0
source share

If your web link is different from the text you display in the TextView:

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 line in the 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 resource string is required.

0
source share

All Articles