0

I need to render bellow html in textView but don't show any thing:

<link rel="stylesheet" type="text/css" href="https://test.com/theme/mou_classic/css/vazir.css">
<style>
    @font-face {
        font-family: Vazir;
        font-style: normal;
        font-weight: bold;
        src: url('https://test.com/theme/mou_classic/fonts/Vazir/ttf/Vazir-Bold.ttf') format('truetype');
    }

    body {
        font-family: Vazir, Tahoma;
        font-size: 14px;
        font-weight: normal;
        line-height: 2em;
    }

    h5 {
        color: maroon;
        font-size: 16px;
    }

    h6 {
        color: gray;
        font-size: 11px;
    }
</style>
<div style="border: solid 1px silver;width:80%;padding: 25px;display: table;margin: auto;direction: rtl">
    <h1 style="text-align: center">
RRTEDFGDF
    </h1>

    <div style="display: table;margin: auto">
        <img src="https://test.com/images/news/137.jpg" style="border-radius: 5px;margin: 10px auto">
    </div>

    <h6>
WERWER
    </h6>

    <div>
QWEWERRR
        <h5>WW </h5>

AAAA
    </div>

    <table border="1" style="border-collapse: collapse;margin: auto;width: 50%">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>11</td>
            <td>12</td>
            <td>13</td>
        </tr>
    </table>

</div> 

I render like bellow:

binding.txtHtml.text = Html.fromHtml(resources.getString(R.string.html))
jo jo
  • 1,758
  • 3
  • 20
  • 34
  • You are loading custom styling. I think you need to use `WebView` instead .. – ADM May 01 '23 at 07:10
  • Does this answer your question? [How to display HTML in TextView?](https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview) – Halil Ozel May 01 '23 at 13:51
  • @HalilOzel that question says to do exactly this, so... no. The actual issue is that `Html.fromHtml` does not support all this stuff, especially the CSS. – Ryan M May 04 '23 at 09:22

1 Answers1

2

You cannot render Html in TextView as TextViews are for displaying text and it cannot parse HTML tags.

There are alternate implementations of TextViews that can deal with HTML Tags

html-textview

Repo Link: https://github.com/sakacyber/html-textview

examples:

add dependency

implementation "com.github.sakacyber:html-textview:1.0.15"

xml

<com.saka.android.htmltextview.HtmlTextView
    android:id="@+id/htmlTextView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp" />

applying

binding.htmlTextView.setText(html, lifecycleScope)
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • What is the advantage of this over a `WebView`? It doesn't seem to support CSS, either (see [here](https://github.com/sakacyber/html-textview/issues/2#issuecomment-1326670431) or [the code that only reads the HTML](https://github.com/sakacyber/html-textview/blob/master/html-textview/src/main/java/com/saka/android/htmltextview/utility/EManager.kt), so it won't even handle the HTML from the question. – Ryan M May 04 '23 at 09:27