Make a Hyperlink Textview in Android

Make a hyperlink textview in android

Try this, and let me know what happen..

Using java code:

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

From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

Or in layout xml file, inside your TextView widget attributes

android:autoLink="web"
android:linksClickable="true"

Android: textview hyperlink

You could have two separate TextViews and you could align them accordingly in your layout if needed:

    Text1.setText(
Html.fromHtml(
"<a href=\"http://www.google.com\">google</a> "));
Text1.setMovementMethod(LinkMovementMethod.getInstance());

Text2.setText(
Html.fromHtml(
"<a href=\"http://www.stackoverflow.com\">stackoverflow</a> "));
Text2.setMovementMethod(LinkMovementMethod.getInstance());

Then if you want to strip the "link underline". Create a class:

public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}

Then add this method in your main Activity class where you have the TextViews

private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}

And then just call this after you initialised the TextViews (in your onCreate):

stripUnderlines(Text1);
stripUnderlines(Text2);

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

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.

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

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"

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