How to make links in a TextView clickable?

后端 未结 30 3274

I have the following TextView defined:



        
相关标签:
30条回答
  • 2020-11-21 23:38

    The accepted answer is correct, BUT it will mean that phone numbers, maps, email addresses, and regular links e.g. http://google.com without href tags will NO LONGER be clickable since you can't have autolink in the xml.

    The only complete solution to have EVERYTHING clickable that I have found is the following:

    Spanned text = Html.fromHtml(myString);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, Linkify.ALL);
    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    textView.setText(buffer);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    

    And the TextView should NOT have android:autolink. There's no need for android:linksClickable="true" either; it's true by default.

    0 讨论(0)
  • 2020-11-21 23:39

    The following should work for anyone who is looking for a combination of text and hyperlink within an Android app.

    In string.xml:

    <string name="applink">Looking for Digital Visiting card? 
    <a href="https://play.google.com/store/apps/details?id=com.themarkwebs.govcard">Get it here</a>
    </string>
    

    Now you can utilise this string in any given View like this:

    <TextView
        android:id="@+id/getapp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="@color/main_color_grey_600"
        android:textSize="15sp"
        android:text="@string/applink"/>
    

    Now, in your Activity or Fragment, do the following:

    TextView getapp =(TextView) findViewById(R.id.getapp);
    getapp.setMovementMethod(LinkMovementMethod.getInstance());
    

    By now, you don't require to set android:autoLink="web" or android:linksClickable="true" using this approach.

    I hope you will find this helpful.

    0 讨论(0)
  • 2020-11-21 23:39

    Manage Linkify text Color Also

    tv_customer_care_no.setLinkTextColor(getResources().getColor(R.color.blue));
      tv_customer_care_no.setText("For us to reach out to you, please fill the details below or contact our customer care at  18004190899 or visit our website http://www.dupont.co.in/corporate-links/contact-dupont.html ");
       Linkify.addLinks(tv_customer_care_no, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
       Linkify.addLinks(tv_customer_care_no, Linkify.ALL );
    
    0 讨论(0)
  • 2020-11-21 23:39

    Autolink phone does not worked for me. The following worked like a charm,

    TextView tv = (TextView) findViewById(R.id.emergencynos);
    String html2="<br><br>Fire - <b><a href=tel:997>997</a> </b></br></br>";        
    tv.append(Html.fromHtml(html2));
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    
    0 讨论(0)
  • 2020-11-21 23:40

    I'm using only android:autoLink="web" and it works fine. A click on the link opens the browser and shows the correct page.

    One thing I could guess is that some other view is above the link. Something that is transparent fills the whole parent but don't displays anything above the link. In this case the click goes to this view instead of the link.

    0 讨论(0)
  • 2020-11-21 23:43

    I hope this will help you;

    String value = "<html>Visit my blog <a href=\"http://www.maxartists.com\">mysite</a> View <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback</html>";
        TextView text = (TextView) findViewById(R.id.text);
    
    
        text.setText(Html.fromHtml(value));
        text.setMovementMethod(LinkMovementMethod.getInstance());
    
    0 讨论(0)
提交回复
热议问题