Android active link of url in TextView

前端 未结 10 491
盖世英雄少女心
盖世英雄少女心 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:52

    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 ...

    0 讨论(0)
  • 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.

    <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);
    
    0 讨论(0)
  • 2020-11-30 22:57

    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://");
    
    0 讨论(0)
  • 2020-11-30 23:10

    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.

    0 讨论(0)
提交回复
热议问题