简体   繁体   中英

android set a link on a textView

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)

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

Please try this one:

String termsLink = "<a href='http://www.google.com'>terms test</a>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM