Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android active link of url in TextView

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like <a href="http://hello.com">hello</a>. I have set the text using the following code.

textView.setText(Html.fromHtml(sampletext)); 

And also set android:autoLink="web" in the corresponding xml of that contains the TextView. Now the link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?

like image 625
dev_android Avatar asked Aug 02 '11 10:08

dev_android


People also ask

Can you make a TextView clickable in android?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do I use Linkify?

To call linkify in android, we have to call linkify. addLinks(), in that method we have to pass textview and LinkifyMask. Linkify. WEB_URLS: It going make URL as web url, when user click on it, it going to send url to default web browsers.


2 Answers

After revisiting all solutions, a summary with some explanations:

android:autoLink="web"  

will find an URL and create a link even if android:linksClickable is not set, links are by default clickable. You don't have to keep the URL alone, even in the middle of a text it will be detected and clickable.

<TextView     android:text="My web site: www.stackoverflow.com"     android:id="@+id/TextView1"     android:layout_height="wrap_content"      android:layout_width="wrap_content"     android:autoLink="web"> </TextView> 

To set a link via the code, same principle, no need for pattern or android:autoLink in layout, the link is found automatically using Linkify:

  final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);   myClickableUrl.setText("Click my web site: www.stackoverflow.com");   Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS); 
like image 170
L. G. Avatar answered Sep 21 '22 14:09

L. G.


This works for me:

<TextView     android:text="www.hello.com"     android:id="@+id/TextView01"     android:layout_height="wrap_content"      android:layout_width="fill_parent"     android:autoLink="web"> </TextView> 
like image 41
Vinayak Bevinakatti Avatar answered Sep 18 '22 14:09

Vinayak Bevinakatti