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>&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 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>&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": "<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>&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>",
"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());