Search code examples
androidhtmlkotlintextview

Why is not working the link from an html in a textview?


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 &lt;a href=&quot;https://www.google.com&quot;&gt;link aqui&lt;/a&gt;

esto es otra prueba solo con el texto https://www.google.com

And here you can see what it's print in the textview:

enter image description here

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.


Solution

  • So there are two things.

    1. Your other link is just added as plain text. It does not have a <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 &lt;a href=&quot;https://www.google.com&quot;&gt;link aqui&lt;/a&gt;\n" +
                    "\n" +
                    "esto es otra prueba solo con el texto &lt;a href=&quot;https://www.google.com&quot;&gt;https://www.google.com&lt;/a&gt;"
    
            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:

    Device link 1

    1. 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 &lt;a href=&quot;https://www.google.com&quot;&gt;link aqui&lt;/a&gt;\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:

    device link 2

    Note: You can either have an anchor tag "<a" or a plain-text link. Unfortunately, both can't be combined directly.