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条回答
▲ chillily
2楼-- · 2019-01-08 10:13

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);
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-08 10:13

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>
查看更多
仙女界的扛把子
4楼-- · 2019-01-08 10:13

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.

查看更多
SAY GOODBYE
5楼-- · 2019-01-08 10:15

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://");
查看更多
太酷不给撩
6楼-- · 2019-01-08 10:25

try this https://saket.me/better-url-handler-textview-android/

It is very good solution and easy

查看更多
叛逆
7楼-- · 2019-01-08 10:27

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());
查看更多
登录 后发表回答