0

I'm trying to attach a link to a TextView. But I can't seem to get it to work.

Here's the TextView

<TextView
       android:id="@+id/terms"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:clickable="true"
       android:linksClickable="true"
       android:text="@string/terms" />

And here's the setLink() method

private void setLinks(){
        String termsURI = "";
        TextView termsTextView = (TextView) findViewById(R.id.terms);
        String termsLink = "<a href='www.google.com'>terms test</a>";
        termsTextView.setMovementMethod(LinkMovementMethod.getInstance());
        termsTextView.setText(Html.fromHtml(termsLink));
    }

I've tried

How do I make links in a TextView clickable?

I want text view as a clickable link

Android TextView Hyperlink

All 3 are highly reviewed and did not work for me. Which leads me to believe I'm doing something wrong.

Is there something wrong here? or possibly some tips?

One thing to add, I need to be able to set the link in a method, since our live and debug environment differ. (different domains)

Trevor Wood
  • 2,347
  • 5
  • 31
  • 56
  • if possible please check my answer https://stackoverflow.com/a/45727769/5381331 here – Linh Sep 06 '17 at 02:53
  • try this in textview xml : android:autoLink="web" – Bapusaheb Shinde Sep 06 '17 at 03:12
  • For future, if somebody is using **Kotlin** and wants to have full control over the clickable text with callback - I wrote an article about it to have an extension function for `TextView` - https://link.medium.com/TLq6s8ltc3 – Hossain Khan Jan 12 '20 at 23:14

1 Answers1

1

Maybe you missed "http://" in the URL string.

Please try this one:

String termsLink = "<a href='http://www.google.com'>terms test</a>";
John Le
  • 1,116
  • 9
  • 12