I'm getting an html from a service that I need to print in a textview. In this case the html has two links the first one works bt the second one doesn't.
XML:
<TextView
android:id="@+id/_doctor_comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:textSize="14dp"
app:textType="regular"/>
Print code in textview:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = Html.fromHtml(HtmlCompat.fromHtml(data.consultationData.doctorAnswer, HtmlCompat.FROM_HTML_MODE_LEGACY).toString())
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text = Html.fromHtml(Html.fromHtml(data.consultationData.doctorAnswer).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
This is what I get from the service:
Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>
esto es otra prueba solo con el texto https://www.google.com
And here you can see what it's print in the textview:
And as you can see the htts://www.google.com will not behave as a link and it isn't highlighted and neither clickable. If anyone has a clue of why this happens please give me any advice.
So there are two things.
<a href
which can be detected by HtmlCompat.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:
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.