29

I'm implementing a TextView with a string containing two hyperlinks as below but the links are not opening a new browser window:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#ffffff"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:textSize="14sp"
        android:clickable="true"
        android:linksClickable="true"
        android:textColorLink="@color/colorPrimary"
        android:autoLink="web"
        android:text="@string/agree_terms_privacy"/>

In string.xml

<string name="agree_terms_privacy">By continuing, you agree to our <a href="http://link1/terms">Terms of Use</a> and read the <a href="http://link1/privacy">Privacy Policy</a></string>
Yen Pei Tay
  • 498
  • 1
  • 6
  • 12

9 Answers9

40

Here is the solution that worked for me, after looking through multiple Stack Overflow posts. I've tailored it to your implementation:

1. Remove autolink in favor of using LinkMovementMethod, and set linksClickable to true

<TextView
    android:id="@+id/termsOfUse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="#ffffff"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:textSize="14sp"
    android:clickable="true"
    android:linksClickable="true"
    android:textColorLink="@color/colorPrimary"
    android:text="@string/agree_terms_privacy"/>

If you use the android:autoLink="web" property then you'll have to override it with textView.setAutoLinkMask(0); before calling setText() on your TextView. You can also set the link to be clickable in your activity instead like in Harshal's answer if you prefer, but I left it since you already had it in the layout. I also added an id to your TextView called termsOfUse which we'll use later.

2. Replace < with &lt; in strings.xml and remove double quotes around the url

This is because when you retrieve the string resource, it won't parse the embedded html correctly, and for some reason it doesn't escape the double quotes. So instead of:

<string name="agree_terms_privacy">By continuing, you agree to our <a href="http://link1/terms">Terms of Use</a> and read the <a href="http://link1/privacy">Privacy Policy</a></string>

you'll want to do:

<string name="agree_terms_privacy">By continuing, you agree to our &lt;a href=http://link1/terms>Terms of Use&lt;/a> and read the &lt;a href=http://link1/privacy>Privacy Policy&lt;/a></string>

3. Parsing the string resource and binding it to our TextView

Spanned policy = Html.fromHtml(getString(R.string.agree_terms_privacy));
TextView termsOfUse = (TextView)findViewById(R.id.termsOfUse);
termsOfUse.setText(policy);
termsOfUse.setMovementMethod(LinkMovementMethod.getInstance());

Note: Html.fromHtml has been deprecated in API 24 (see this post for more information on how to handle this if needed). We use this method to get the expected HTML formatting from our string.

nymeria
  • 397
  • 3
  • 6
15

Have a look on below code snippet, hope it helps,

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
8

This works for me

Add this in your textview:

android:autoLink="all"
android:clickable="true"

https://www.youtube.com/watch?v=UnJxyfyDyHU

I hope this help you.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Rebe
  • 89
  • 1
  • 2
2

I would recommend you to have two TextViews since ou want two different actions:

TextView yourTermsOfUseTextView = (TextView) findViewById(R.id.your_id);
yourTermsOfUseTextView.setOnclickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(your_download_link));
                    startActivity(myIntent);
                }
            });

Repeat to the privacy policy.

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42
1

Just add below code in your Textview

android:autoLink="email"   
Soumen Das
  • 1,292
  • 17
  • 12
1

You can use like bellow..

  1. textview xml

    <TextView
     android:id="@+id/tv_welcome_message"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"            
     android:textSize="16sp"/>
    
  2. In activity

     TextView   tv_welcome_message=(TextView) findViewById(R.id.tv_welcome_message); 
     tv_welcome_message.setMovementMethod(LinkMovementMethod.getInstance());
     String text = " <a href='http://www.google.com'> Google </a>";
     tv_welcome_message.setText(Html.fromHtml(text));
    
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

MainActivity.java

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

I removed most of the attributes on my TextView to match what was in the demo

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txtCredits"/>

Don't forget to remove autoLink="web" if you are calling setMovementMethod()

Chethana Arunodh
  • 335
  • 3
  • 15
0

Just use Linkify

textView.text = text
Linkify.addLinks(textView, Linkify.WEB_URLS)
Dmitry Kopytov
  • 341
  • 3
  • 6
0

This worked for me Add this in your textview

android:linksClickable="true"
android:autoLink="all"

AutoLink Values: all, email, map, none, phone, web Controls whether links such as urls and email addresses are automatically found and converted to clickable links. The default value is "none", disabling this feature.

Abhishek Yadav
  • 261
  • 3
  • 9