Android active link of url in TextView

前端 未结 10 522
盖世英雄少女心
盖世英雄少女心 2020-11-30 22:24

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like hello

10条回答
  •  鱼传尺愫
    2020-11-30 22:54

    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.

    
    
    

    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);
    

提交回复
热议问题