-1

I get the Html String from the server. I applied this serverString to TextView using HtmlCompat. The inside of the body tag works correctly, but the inside of the style tag does not work, and the code is exposed to TextView as it is.

val serverString = """
<html>
    <head>
        <style>
            body {
                font-family: 'Noto Sans KR', sans-serif;
                font-size: 14pt;
                color: #333;
            }
            p {
                margin-bottom: 16px;
            }
            .underline {
                text-decoration: underline;
            }
            .red {
                color: #FF0000;
            }
        </style>
    </head>
    <body>
        <p>This is a sample text with <b>bold</b>, <i>italic</i>, and <span class="underline">underline</span> text.</p>
        <p>This is another paragraph with <span class="red">red</span> text.</p>
        <p>This is a long paragraph with line breaks:<br/>
            First line<br/>
            Second line<br/>
            Third line
        </p>
    </body>
</html>
binding.textView.text = HtmlCompat.fromHtml(serverString, HtmlCompat.FROM_HTML_MODE_LEGACY)

Below is the result image.

enter image description here

How can I get the code to be applied correctly applied?

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
haruple
  • 1
  • 1
  • First thing remove 2 `"` from start and end there should be only one `"..."` and then refer this. https://stackoverflow.com/a/2116191/12833551 – Pratik Fagadiya Apr 11 '23 at 03:42

1 Answers1

0

I might be wrong with this but what I understood, the problem here is that HtmlCompat doesn't support tag. so it can't compile css styling.

You can try to go with two approach here -

  1. Either change your html and apply styling with html tags inside body. This way your styling will work.

  2. Or you can try to create a custom tag handler for tag that can compile styling and then apply the same styling to respective tag.

As of now, I don't have any concrete solution for creating Tag handler so I'll suggest you to go with changing your html layout..