Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView Hyperlink

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> 
like image 846
Yen Pei Tay Avatar asked Aug 07 '16 10:08

Yen Pei Tay


People also ask

Is TextView clickable android?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.

How do you create a hyperlink?

Select the text or picture that you want to display as a hyperlink. Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box.

How do you send a link in a text message on android?

To include a link in any text message, just type or paste the full URL into your text. Most messaging platforms will automatically turn the URL into a link that allows contacts to click and access the linked page.


2 Answers

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.

like image 133
nymeria Avatar answered Oct 02 '22 04:10

nymeria


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)); 
like image 31
Harshal Benake Avatar answered Oct 02 '22 03:10

Harshal Benake