How to Make Links in a Textview Clickable

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:// or https://
  • 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

Android Kotlin - making link in TextView clickable

Alright this is the full solution:

    val text = getString(R.string.by_continuing_you_agree_terms_and_policy)
val ss = SpannableString(text)

val clickTerms: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
Log.d(tagg, "terms clicked")
binding.createAccountTermsText.clearFocus()
}
}

val clickPrivacy: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
Log.d(tagg, "privacy clicked")
binding.createAccountTermsText.clearFocus()
}
}

val hintColorOne = ForegroundColorSpan(Color.parseColor("#FF0000"))
val hintColorTwo = ForegroundColorSpan(Color.parseColor("#FF0000"))

ss.setSpan(hintColorOne, 29, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
ss.setSpan(hintColorTwo, 50, 64, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

ss.setSpan(clickTerms, 29, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
ss.setSpan(clickPrivacy, 50, 64, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

binding.createAccountTermsText.text = ss
binding.createAccountTermsText.movementMethod = LinkMovementMethod.getInstance()

the ints are standing for first/last characters, which means the color and click area is between them.

Note 1: using hintColorOne and hintColorTwo instead of like hintColorOne for both spans is necessary, because using only one for both spans would make only the second span work. It's just plain happiness to develop android apps with all those surprises!

Note 2: Using clickableSpan will disable the defined color and make use of the in the XML defined android:textColorLink="#FF0000" instead

android make links in a TextView clickable

What you are trying to do will not work because a Google Maps InfoWindow is not a live view.

In the documentation for Custom Info Windows it states:

The info window that is drawn is not a live view. The view is rendered
as an image (using View.draw(Canvas)) at the time it is returned. This
means that any subsequent changes to the view will not be reflected by
the info window on the map. To update the info window later (for
example, after an image has loaded), call showInfoWindow().
Furthermore, the info window will not respect any of the interactivity
typical for a normal view such as touch or gesture events. However you
can listen to a generic click event on the whole info window as
described in the section below.

and

As a result, any listeners you set on the view are disregarded and you
cannot distinguish between click events on various parts of the view.

So the clicks on the individual tv_link TextView within the overall InfoWindow layout will not be processed.

You need to use the OnInfoWindowClickListener to listen for clicks on an InfoWindow

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()

I want text view as a clickable link

All tested and working 100%

below is a complete example

Solution: android:autoLink="web"

Sample Xml:

<TextView
android:id="@+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="@string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="@string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />

String in string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>


Related Topics



Leave a reply



Submit