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 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 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.
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());
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());
}
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
Why is not working the link from an html in a textview?
So there are two things.
- Your other link is just added as plain text. It does not have a
<a href
which can be detected byHtmlCompat.fromHtml
. So if you want that second text is also displayed as a link. You've to modify the response.
Example:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto <a href="https://www.google.com">https://www.google.com</a>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
Output:
- If you want an automatic way to detect links from plain text which does not have anchor text then you can use
Linkify.addLinks(txtDoctorComments,Linkify.WEB_URLS)
.
Example:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto https://www.google.com"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
Linkify.addLinks(txtDoctorComments, Linkify.WEB_URLS)
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
Output:
Note: You can either have an anchor tag "<a" or a plain-text link. Unfortunately, both can't be combined directly.
Related Topics
Android: Textcolor of Disabled Button in Selector Not Showing
Supportmapfragment Does Not Support Androidx Fragment
Change Order of Views in Linear Layout Android
Android Imageview Change Tint to Simulate Button Click
Android Dialog: Removing Title Bar
How to Read All The Coming Notifications in Android
How to Create a Date and Time Picker in Android
Air 3 Native Extensions for Android - Can I/How to Include 3Rd Party Libraries
Error Building Android Library: Direct Local .Aar File Dependencies Are Not Supported
How to Integrate Samsung Gear Steps in Android Application
What Is The Easiest Way to Use Svg Images in Android
Send File to Server via Retrofit2 as Object
How to Create a Release Android Library Package (Aar) in Android Studio (Not Debug)
How to Create Custom Spinner Like Border Around The Spinner with Down Triangle on The Right Side