Search code examples
javaandroidlistviewfirebase-realtime-databasehtml-parsing

How do i parse HTML text with tags that i retrieve from firebaseDB in a TextView? No WebView


So I am trying to figure out how do i parse this HTML text with tags to a Textview in a List. Any good suggestions,is there a good library or maybe i should manually write code somehow to parse it.


Solution

  • There are two options :

    First is to use Html.fromHtml(htmlString)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       textView.setText(Html.fromHtml(htmlString,Html.FROM_HTML_MODE_LEGACY));
    } else  {
       textView.setText(Html.fromHtml(htmlString));
    }
    

    The tags allowed in this approach are : <a href="..."> <b>, <big>, <blockquote>, <br>, <cite>, <dfn> <div align="...">, <em>, <font size="..." color="..." face="..."> <h1>, <h2>, <h3>, <h4>, <h5>, <h6> <i>, <img src="...">, <p>, <small> <strike>, <strong>, <sub>, <sup>, <tt>, <u>

    Source

    Another option is to use CDATA

    Define a string resource like

    <string name="stringName"><![CDATA[<html>%1$s</html>]]></string>
    

    Then call it like textView.setText(res.getString(R.string.stringName, htmlString));