Android active link of url in TextView

2019-01-08 09:43发布

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?

9条回答
神经病院院长
2楼-- · 2019-01-08 10:30

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

查看更多
Lonely孤独者°
3楼-- · 2019-01-08 10:30

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>  
查看更多
男人必须洒脱
4楼-- · 2019-01-08 10:39

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

查看更多
登录 后发表回答