-2

Hellos Mates,

When I am trying to change the string to html and render the same on the EditText Control it is not working fine.

Span does not recognize as an HTML

<span style="font-weight: bold;">Bold</span>
<div><span style="font-style: italic;">ITALIC</span></div>
<div><span style="text-decoration-line: underline;">UNDERLINE</span></div>
<div><span style="text-decoration-line: underline;"><br></span></div><div><ul><li>hieeeeeeeeeeeeee<br></li></ul></div>

How to render the given HTML in EditText. If I talk about the below HTML it gets rendered

<p dir="ltr">Testing<br>
<b>Bold</b><br>
<i>Italic</i><br>
<u>Underline</u></p>

because of span it is not getting rendered.

Please help with same. any suggestion

Raghav Chopra
  • 527
  • 1
  • 10
  • 26
  • 1
    Option #1: Write code to translate the HTML that you have received into HTML that `Html.fromHtml()` can handle. Option #2: Write code to parse the HTML that you have received and generated your own `Spanned` object with your own spans. – CommonsWare Jul 14 '17 at 11:23
  • You can follow this : https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview – Paddy02 Jul 14 '17 at 11:35

1 Answers1

2

Since EditText is extended from TextView by default it supports following tags:

<a> 
<annotation> 
<b> 
<big> 
<font> 
<i> 
<li> 
<marquee> 
<small> 
<strike> 
<sub> 
<sup> 
<tt> 
<u>

So your <span> just cannot be rendered if passed as simple String

But you can render it using Html.FromHtml()

 String text = "<span style=\"font-weight: bold;\">Bold</span>";
 if (Build.VERSION.SDK_INT >= 24) {
     // for 24 api and more
     editText.setText(Html.fromHtml(text , Html.FROM_HTML_MODE_LEGACY)) 
 } else {
     // or for older api
     editText.setText(Html.fromHtml(text )) 
 }

Html.fromHtml supports following tags

<br>
<p>
<ul>
<li>
<div>
<span>
<strong>
<b>
<em>
<cite>
<dfn>
<i>
<big>
<small>
<font>
<blockquote>
<tt>
<a>
<u>
<del>
<s>
<strike>
<sup>
<sub>
<img>
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56