66

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?

dev_android
  • 8,698
  • 22
  • 91
  • 148

10 Answers10

170

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);
L. G.
  • 9,642
  • 7
  • 56
  • 78
  • 1
    Another thing to put here is that if you need more than web links, you can use the `all` attribute, which supports all types of links. – pamobo0609 May 17 '18 at 21:17
36

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>
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
  • 1
    I used this...but it is giving with blue color and underlined text, but I found the its just a dead link. No click action is happening. That is my problem. – dev_android Aug 02 '11 at 14:02
  • i know this is old, you need to set clickable true and define onclick function – user173488 Dec 20 '14 at 11:58
14

To Save any one time the true solution is

    <TextView
    android:text="www.hello.com"
    android:id="@+id/TextView01"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:autoLink="web"
    android:linksClickable="true">
</TextView>

and i use it and it work perfect

M.Ali El-Sayed
  • 1,608
  • 20
  • 23
9

There are 2 cases:

  • the text looks like "click on http://www.hello.com"

then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="click on http://www.hello.com"/>
  • the text looks like click on <a href="http://hello.com">hello</a>

then you have to do it by code and tell the text is html, and specify a Link movement method for the click:

    String text = "click on <a href=\"http://hello.com\">hello</a>";
    TextView textView = view.findViewById(R.id.textView);
    textView.setText(Html.fromHtml(text));
    textView.setMovementMethod(LinkMovementMethod.getInstance());
Arnaud SmartFun
  • 1,573
  • 16
  • 21
4

Check out this approach:

String text = "Visit stackoverflow.com";
TextView label = new TextView(this);
label.setText(text);
Pattern pattern = Pattern.compile("stackoverflow.com");
Linkify.addLinks(label, pattern, "http://");
Pang
  • 9,564
  • 146
  • 81
  • 122
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
3

i have give some idea which i have found it

TextView tv = ( TextView ) findViewById( R.id.link );  
    WebView wv = ( WebView ) findViewById( R.id.webView );  
    URLSpan[] urlSpans = tv.getUrls();  
    for ( URLSpan urlSpan : urlSpans )  
    {  
        wv.loadUrl( urlSpan.getURL() );  
    }  

string.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <string name="app_name">Hello, Android</string>  
  <string name="link">'<a href="http://www.google.com" rel="nofollow">Google</a>'</string>  
</resources> 

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >  

  <TextView  
          android:id="@+id/link"  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content"  
          android:autoLink="all"  
          android:linksClickable="true"  
          android:text="@string/link" />  

  <WebView  
          android:id="@+id/webView"  
          android:layout_width="fill_parent"  
          android:layout_height="wrap_content"  
          android:layout_weight="1.0" />  

</LinearLayout>  
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • for the link string tag value in string.xml, ensure you escape the following characters, else the project might fail to build. & change to & < change to < > change to > – hyena Jan 13 '16 at 07:08
2

In your XML, you need to add android:linksClickable="true" in the TextView.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
0

If you are displaying in textview the string from strings.xml, strings containing the web link should not have word "a href=". If these words are deleted from the strings.xml file then the link will work.

0

Add these lines of code to your textView in xml File it will work perfectly fine..

android:autoLink="web" android:textColorLink="@android:color/holo_orange_dark" android:linksClickable="true"

or if want a your own link in textview add these lines of code in your java file

  final SpannableString s = new SpannableString("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng");
  Linkify.addLinks(s, Linkify.ALL);

set this 's' String in your TextView by function

Textview.setText(s);

and don't forget to add this line

 ((TextView)findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

id will be your textview id

Enjoy ...

Tarun Grover
  • 131
  • 1
  • 4
0

Answer is right, BUT not complete, because I had in my xml some properties from other answers like autoLink and linksClickable and programatic way did not work. Also when I passes string with html from string resource also it did not work, so beware, you have to clean your xml and pass string directly exactly as in that answer.

    String text = "click on <a href=\"http://hello.com\">hello</a>";
    TextView textView = view.findViewById(R.id.textView);
    textView.setText(Html.fromHtml(text));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

I did not try it without without LinkMovementMethod but now I am ok as it finally work. Other answers did not work for me or was for visible url text, not clickable text as link.

Renetik
  • 5,887
  • 1
  • 47
  • 66