Create Clickable Link in Text View in Android

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

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

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

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.

Android Textview with clickable email and link

I think you need to set the android:linksClickable="true" attribute.

If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found.

Set the sms_deposit_information in strings.xml in the form of

<string name="sms_deposit_information"><![CDATA[<p>abc@xyz.com</p>]]></string>

Then do the below programmatically

TextView textView = findViewById(R.id.description);
textView.setText(Html.fromHtml(getString(R.string.sms_deposit_information)));
textView.setMovementMethod(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>

how to make clickable url link in TextView on android without using java

Sample Image

<TextView
android:textSize="18sp"
android:autoLink="web"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="http://www.example.com"
tools:ignore="HardcodedText" />

It's No required declear layout id and no code from java side. it's works from xml

Use Autolink

    • For website android:autoLink="web"
    • For call android:autoLink="phone"
    • For email android:autoLink="email"
    • For map android:autoLink="web"
    • For all android:autoLink="all"

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



Related Topics



Leave a reply



Submit