3

I want to make hyperlink "TERMS" and "PRIVACY POLICY" its not working in my case:-

 mTermsPPText = (TextView) view.findViewById(R.id.bottom_text);
        String value = "<html>By joining you agree to our <font color=\"#3C599F\" ><a href=\"http://www.google.com/\">TERMS</a></font> & <font color=\"#3C599F\" ><a href=\"http://www.google.com/\">PRIVACY POLICY</a></font></html>";
        mTermsPPText.setText(value);
Ankush Badlas
  • 111
  • 1
  • 9
  • possible duplicate of [How do I make links in a TextView clickable?](http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – Haresh Chhelana Jun 15 '15 at 07:18
  • see this link http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app – N J Jun 15 '15 at 07:18
  • Possible duplicate of : http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable – Arun Kumar Jun 15 '15 at 07:18

3 Answers3

4

Please use spannable String for your String like as below:

String value = "<html>By joining you agree to our <font color=\"#3C599F\" ><a href=\"http://www.google.com/\">TERMS</a></font> & <font color=\"#3C599F\" ><a href=\"http://www.google.com/\">PRIVACY POLICY</a></font></html>";

Spannable s = (Spannable) Html.fromHtml(value);
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpan(span.getURL());
            s.setSpan(span, start, end, 0);
        } 

        mTermsPPText.setText(s);

Use movement method for performing operation on click

    mTermsPPText.setMovementMethod(LinkMovementMethod.getInstance());

Hope it will helpfull for you.

1

Try doing this

mTermsPPText.setText(Html.fromHtml(value));
mTermsPPText.setMovementMethod(LinkMovementMethod.getInstance());
Bidhan
  • 10,607
  • 3
  • 39
  • 50
0
//use set movement method before setting html page in textview

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

// than set text in html
t2.setText(Html.fromHtml(linkText));

// important thing is that you should use anchor tag in html to make link clickable

// dont use autolink in textview
<TextView
    android:id="@+id/t2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62