In my android application Activity
there is a TextView
with multiple clickable text for. I have used Linkify
for making them clickable. But the problem is that they don't call the web URL on click. How do I solve this?
Below is my code.
String termsAndConditions = "Terms and Conditions";
String privacyPolicy = "Privacy Policy";
txtLinkfy.setText(
String.format(
"By tapping to continue, you are indicating that " +
"you have read the Privacy Policy and agree " +
"to the Terms and Conditions.",
termsAndConditions,
privacyPolicy)
);
txtLinkfy.setMovementMethod(LinkMovementMethod.getInstance());
Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
Linkify.addLinks(txtLinkfy, termsAndConditionsMatcher,"http://myexample.in/termsofservice");
Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
Linkify.addLinks(txtLinkfy, privacyPolicyMatcher, "http://myexample.in/privacypolicy");
And in AndroidManifest.xml :
<activity android:name=".view.ActivityContinue"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="Terms and Conditions" />
<data android:scheme="Privacy Policy" />
</intent-filter>
</activity>
Finally I solved my problem.Use below code.
Linkify.TransformFilter transformFilter = new Linkify.TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return "";
} };
Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
Linkify.addLinks(txtLinkfy,termsAndConditionsMatcher,"http://myexample.in/termsofservice",null,transformFilter);
Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
Linkify.addLinks(txtLinkfy,privacyPolicyMatcher,"http://myexample.in/privacypolicy",null,transformFilter);
The behavior of API "Linkify.addLinks" is such that, it appends the string that you want to linkify to the end of the url. For ex..if you want to linkify text "Terms and Conditions" with "http://myexample.in/termsofservice". The final URL is "http://myexample.in/termsofserviceTerms and Conditions" which is not what I needed.So i used the api transformFilter return a null string. So my final url is "http://myexample.in/termsofservice".