How to make links in a TextView clickable?

后端 未结 30 3341

I have the following TextView defined:



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

    Just wasted so much time to figure out you have to use getText(R.string.whatever) instead of getString(R.string.whatever)...

    Anyways, here is how I got mine working. With multiple hyperlinks in the same text view too.

        TextView termsTextView = (TextView) getActivity().findViewById(R.id.termsTextView);
        termsTextView.append("By registering your account, you agree to our ");
        termsTextView.append(getText(R.string.terms_of_service));
        termsTextView.append(", ");
        termsTextView.append(getText(R.string.fees));
        termsTextView.append(", and the ");
        termsTextView.append(getText(R.string.stripe_connected_account_agreement));
    
        termsTextView.setMovementMethod(LinkMovementMethod.getInstance());
    
    
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/termsTextView"/>
    

    string example

        <string name="stripe_connected_account_agreement"><a href="https://stripe.com/connect/account-terms">Stripe Connected Account Agreement</a></string>
    
    0 讨论(0)
  • 2020-11-21 23:59

    If you want to add HTML-like link, all you need to do is:

    • add a resource HTML-like string:

       <string name="link"><a href="https://www.google.pl/">Google</a></string>
      
    • add your view to the layout with NO link-specific configuration at all:

       <TextView
          android:id="@+id/link"
          android:text="@string/link" />`
      
    • add appropriate MovementMethod programmatically to your TextView:

       mLink = (TextView) findViewById(R.id.link);
       if (mLink != null) {
         mLink.setMovementMethod(LinkMovementMethod.getInstance());
       }
      

    That's it! And yes, having options like "autoLink" and "linksClickable" working on explicit links only (not wrapped into html tags) is very misleading to me too...

    0 讨论(0)
  • 2020-11-22 00:00

    i used this simply

    Linkify.addLinks(TextView, Linkify.ALL);
    

    makes the links clickable given here

    0 讨论(0)
  • 2020-11-22 00:00

    [Tested in Pre-lollipop as well as in Lollipop and above]

    You can get your HTML string from the backend or from your resources files. If you put your text as an resource string, make sure to add the CDATA tag:

    <string name="your_text">![CDATA[...<a href="your_link">Link Title</a>  ...]]</string>
    

    Then in code you need to get the string and assign it as HTML and set a link movement method:

    String yourText = getString(R.string.your_text);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       textView.setText(Html.fromHtml(yourText, Html.FROM_HTML_MODE_COMPACT));
    } else {
       textView.setText(Html.fromHtml(yourText));
    }
    
    try {
       subtext.setMovementMethod(LinkMovementMethod.getInstance());
    } catch (Exception e) {
       //This code seems to crash in some Samsung devices.
       //You can handle this edge case base on your needs.
    }
    
    0 讨论(0)
  • 2020-11-22 00:01

    If using XML based TextView, for your requirement you need to do just two things:

    1. Identify your link in the string, such as "this is my WebPage." You can add it in xml or in the code.

    2. In the xml that has the TextView, add these:


    android:linksClickable="true"
    
    android:autoLink="web"
    
    0 讨论(0)
  • 2020-11-22 00:05

    The easiest thing that worked for me is to use Linkify

    TextView txt_Message = (TextView) view.findViewById(R.id.txt_message);
    txt_Message.setText("This is link https://www.google.co.in/");
    Linkify.addLinks(txt_Message, Linkify.WEB_URLS);
    

    and it will automatically detect the web urls from the text in the textview.

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