0

I have tried multiple ways to render html in android . But none of them fulfill my requirement.

In response of a service I got below html message in string.

<table id='tblUpdateHistory' cellpadding='0' cellspacing='0'><tr align='left' valign='top'><td align='left' colspan='3' valign='top'><b>Case Update History<b></td></tr><tr align='left' valign='top'><td class='istColumn' align='left'><b> </b></td><td class='secondColumn' align='left' valign='top'><b>Old Value</b></td><td class='thirdColumn' align='left' valign='top'><b>Updated Value</b></td></tr><tr align='left'><td class='istColumn' align='left' valign='top'>Phone</td><td class='secondColumn' align='left' valign='top'>2034947144</td><td class='thirdColumn' align='left' valign='top'>203-494-7144</td></tr></table>",

I am using the given method to render html in textView.

public static void setHtml(TextView textView, String bodyData) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            textView.setText(Html.fromHtml(bodyData, Html.FROM_HTML_MODE_LEGACY));
        } else {
            textView.setText(Html.fromHtml(bodyData));
        }
    }

I have also tried three ways I handle all EscapeCharacter in my message and replace them appropriately

public String handleEscapeCharacter( String str ) {
        String[] escapeCharacters = { ">", "<", "&", """, "'" ," "};
        String[] onReadableCharacter = {">", "<", "&", "\"\"", "'" , "\\u00A0"};
        for (int i = 0; i < escapeCharacters.length; i++) {
            str = str.replace(escapeCharacters[i], onReadableCharacter[i]);
        } return str;
    }

Then i tried below way

public String convertToHtml(String htmlString) {

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("<![CDATA[");
    stringBuilder.append(htmlString);
    stringBuilder.append("]]>");
    return stringBuilder.toString();
}

thirdly I tried replace

setHtml(tvDetailText, descriptionTemp.replaceAll("(\n|\n\r|\r)", "<br />"));

But when I open Notepad++ and replace all escape character and format my message by replacing some character. then i tried it manually it works but only 1 tab is missing. where am i doing wrong. The formatted html is written below.

<table 
        id='tblUpdateHistory' 
        cellpadding='0' 
        cellspacing='0' >

            <tr align='left' 
                valign='top' >
                <td align='left' colspan='3' valign='top'><b>Case Update History<b> </td>
            </tr>

            <tr align='left' 
                valign='top' >
                <td class='istColumn' align='left'><b>&amp;nbsp;</b></td>
                <td class='secondColumn' align='left' valign='top'><b>Old Value</b></td>
                <td class='thirdColumn' align='left' valign='top'><b>Updated Value</b></td>
            </tr>

            <tr align='left'>
                <td class='istColumn' align='left' valign='top'>Phone</td>
                <td class='secondColumn' align='left' valign='top'>2034947144</td>
                <td class='thirdColumn' align='left' valign='top'>203-494-7144</td>
            </tr>
    </table>

I have parsed this json response.

{
        "CaseNote": [{
                "ID": 575772,
                "CaseID": 218320,
                "Date": "6/15/2014 3:10:49 PM",
                "Message": "Refunded: Order was never imported/charged due to web issue.  This customer did not want to pickup a different arrangement.",
                "Status": 6,
                "ReOpened": "False",
                "UserName": "",
                "Source": "EAConnect",
                "OriginalRequest": "False"
            }, {
                "ID": 575732,
                "CaseID": 218320,
                "Date": "6/15/2014 3:04:12 PM",
                "Message": "&lt;table id='tblUpdateHistory' cellpadding='0' cellspacing='0'&gt;&lt;tr align='left' valign='top'&gt;&lt;td align='left' colspan='3' valign='top'&gt;&lt;b&gt;Case Update History&lt;b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr align='left' valign='top'&gt;&lt;td class='istColumn' align='left'&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;td class='secondColumn' align='left' valign='top'&gt;&lt;b&gt;Old Value&lt;/b&gt;&lt;/td&gt;&lt;td class='thirdColumn' align='left' valign='top'&gt;&lt;b&gt;Updated Value&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr align='left'&gt;&lt;td class='istColumn' align='left' valign='top'&gt;Phone&lt;/td&gt;&lt;td class='secondColumn' align='left' valign='top'&gt;2034947144&lt;/td&gt;&lt;td class='thirdColumn' align='left' valign='top'&gt;203-494-7144&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;",
                "Status": 2,
                "ReOpened": "False",
                "UserName": "Jill Bailey",
                "Source": "CallCenter",
                "OriginalRequest": "False"
            }
        ]
        }

Second Message field in CaseNotes and at IOS end it's working fine.

How to convert this message to HTML format ??

edit 1

My HTML response is not encoding properly I have tested it on codepen. and tried to encode it using given two methods but all in vain..

String firstEncoding = TextUtils.htmlEncode(itemList.get(position).getMessage());
String secondEncoding =  Html.escapeHtml(itemList.get(position).getMessage());
ZA BroadPeak
  • 29
  • 1
  • 9
  • 1 tab is missing. what does it really mean? – Abdul Waheed Mar 26 '18 at 13:13
  • If you are the one behind the service response, I strongly recommend not to exange data in html format, use xml or json instead. So, if this is the case, fix your service first and then render properly, using native controls, on your app. – Pedro Emilio Borrego Rached Mar 26 '18 at 13:17
  • check this qustion https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview – Rohit Chauhan Mar 26 '18 at 13:24
  • @AbdulWaheed In IOS it's working totally fine. But after replacing all escape character by me using Notepad++ still one space is missing as compared to IOS. – ZA BroadPeak Mar 27 '18 at 04:42
  • @PedroEmilioBorregoRached No I am not working at service end but the same response is handled correctly at IOS end. What am i doing wrong Or does table is supported in formating in android – ZA BroadPeak Mar 27 '18 at 04:43

1 Answers1

0

Your HTML seems fine except that the first one is escaped. If you have a escaped String and you want to unescape it, you can use StringEscapeUtils.unescapeJava(String str).

Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44