How to Make Links in Fromhtml Clickable? (Android)

How can I make links in fromHTML clickable? (Android)

As I assumed, the solution was trivial:

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

The second line somehow activates the link behavior, although I'm not quite sure how. The same question is addressed over at Google Code.

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

android: html in textview with link clickable

Try this

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

Remember : don't use android:autoLink="web" attribute with it. because it causes LinkMovementMethod doesn't work.

Update for SDK 24+
The function Html.fromHtml deprecated on Android N (SDK v24), so turn to use this method:

    String html = "<a href=\"http://www.google.com\">Google</a>";
Spanned result = HtmlCompat.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
txtTest.setText(result);
txtTest.setMovementMethod(LinkMovementMethod.getInstance());

Here are the list of flags:

FROM_HTML_MODE_COMPACT = 63;
FROM_HTML_MODE_LEGACY = 0;
FROM_HTML_OPTION_USE_CSS_COLORS = 256;
FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;

Update 2
with android.text.util.Linkify, it's more easier now to make a clickable TextView:

TextView textView =...
Linkify.addLinks(textView, Linkify.WEB_URLS);

How to make phone number,links and url all clickable using Html.fromHtml() in TextView

This problem occure because of removing spans from your Spanned text. Linkify will remove all html spans before applaying it's own.

You have to store your html spans and add it back after linkify.

 private Spannable linkifyHTML(CharSequence text) {
Spannable s = new SpannableString(text);

URLSpan[] old = s.getSpans(0, s.length(), URLSpan.class);
LinkSpec oldLinks[] = new LinkSpec[old.length];

for (int i = 0; i < old.length; i++) {
oldLinks[i] = new LinkSpec(old[i], s.getSpanStart(old[i]), s.getSpanEnd(old[i]));
}

Linkify.addLinks(s, Linkify.ALL);
for (LinkSpec span : oldLinks) {
s.setSpan(span.span, span.start, span.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}

Please see this answer for more info: How to make all 3 types of links in textview\

Android TextView with Clickable Links: how to capture clicks?

Based upon another answer, here's a function setTextViewHTML() which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
// Do something with span.getURL() to handle the link click...
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String html)
{
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}

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



Related Topics



Leave a reply



Submit