How to make links in a TextView clickable
Buried in the API demos, I found the solution to my problem:
File Link.java:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
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"/>
That solved it. It is pretty difficult to uncover and fix.
Important: Don't forget to remove autoLink="web"
if you are calling setMovementMethod()
.
Part of TextView clickable with a link
check below point before testing
- Check your link starts with
http://
orhttps://
- Place your String in CDATA tag (like
<string name="some_text"><![CDATA[PLACE_YOUR_HTML_STRING_HERE]]></string>
) - Check you have added internet Permission in your
manifest.xml
file<uses-permission android:name="android.permission.INTERNET"/>
- then dynamically html text can be set using below code into textView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(getString(R.string.html_string), Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml(getString(R.string.html_string)));
}
Linkify.addLinks(textView, Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
How to make a textview text link clickable
try with this code, its working code in my project.
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
How to set the part of the text view is clickable
Have HyperLink Text in TextView
Use SpannableString
for this
SpannableString ss = new SpannableString("Your string value");
ClickableSpan clickableTerms = new ClickableSpan() {
@Override
public void onClick(View textView) {
// show toast here
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
ss.setSpan(clickableTerms, 4, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(ss);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setHighlightColor(Color.TRANSPARENT);
You can make multiple words clickable by this method.
Multiple Clickable links in TextView on Android
You can use Linkify (android.text.Spannable,java.util.regex.Pattern,java.lang.String)
String termsAndConditions = getResources().getString(R.string.terms_and_conditions);
String privacyPolicy = getResources().getString(R.string.privacy_policy);
legalDescription.setText(
String.format(
getResources().getString(R.string.message),
termsAndConditions,
privacyPolicy)
);
legalDescription.setMovementMethod(LinkMovementMethod.getInstance());
Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
Linkify.addLinks(legalDescription, termsAndConditionsMatcher, "terms:");
Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
Linkify.addLinks(legalDescription, privacyPolicyMatcher, "privacy:");
and then you can use the scheme to start an activity for example by adding the scheme in the AndroidManifest:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="terms" />
<data android:scheme="privacy" />
</intent-filter>
If you want to do a custom action, you can set the intent-filter to your current activity, which will have a singleTop launchmode.
This will cause onNewIntent to be fired where can make your custom actions:
@Override
protected void onNewIntent(final Intent intent) {
...
if (intent.getScheme().equals(..)) {
..
}
}
Links in TextView
Thank you for your help all.
I have managed to make this work, after I have found some examples in the android samples.
here is the code:
textView.setText(Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " +
"created in the Java source code using HTML."));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Hope this help others...
How handle 2 link click in textview use string resources?
I have used as below mentioned in my apps.
string.xml
<string name="about_fragment_privacy_policy" translatable="false">User Agreement and Privacy Policy</string>
Layout.xml
<TextView
android:id="@+id/privacyPolicy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_fragment_privacy_policy" />
Kotlin code
string = getString(R.string.about_fragment_privacy_policy)
spannableString = SpannableString(string)
val clickableUserAgreement = object : ClickableSpan() {
override fun onClick(widget: View) {
startActivity(
Intent(Intent.ACTION_VIEW).setData(
Uri.parse(
"https://example.com"
)
)
)
}
}
val clickablePrivacyPolicy = object : ClickableSpan() {
override fun onClick(widget: View) {
startActivity(
Intent(Intent.ACTION_VIEW).setData(
Uri.parse(
"https://example.com"
)
)
)
}
}
spannableString.setSpan(
clickableUserAgreement,
0,
14,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
0,
14,
0
)
spannableString.setSpan(
clickablePrivacyPolicy,
19,
string.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
19,
string.length,
0
)
privacyPolicy.text = spannableString
privacyPolicy.movementMethod = LinkMovementMethod.getInstance()
Related Topics
Android:What Is Difference Between Setflags and Addflags for Intent
Difference of Setvalue() & Postvalue() in Mutablelivedata
Android Device Chooser -- Device Not Showing Up
How to Change the Track Color of a Switchcompat
Error: Execution Failed for Task ':App:Clean'. Unable to Delete File
How Do We Distinguish Never-Asked from Stop-Asking in Android M's Runtime Permissions
Download Multiple Files with a Progress Bar in Listview Android
Missing Styles. Is the Correct Theme Chosen for This Layout
Using JSON File in Android App Resources
Position of Dialogfragment in Android
Padding Between Actionbar's Home Icon and Title
Access Pictures from Pictures App in My Android App
Android - Apply Selectableitembackground in Xml with Support V7
When Exactly Are Onsaveinstancestate() and Onrestoreinstancestate() Called