I have a String that I need to render in Webview, but facing issues. The String is as follows.
History
Riots and political conflicts of the 1960s
--------Line Feed1----------------
The LAPD Metropolitan Division's "D" Platoon is one of the world's most prominent SWAT units and was the second SWAT team established in the United States, after that of the Philadelphia Police Department in 1964.[2]
According to the Historical Dictionary of Law Enforcement, the term "SWAT" was used as an acronym for the "Special Weapons and Tactics" established as a 100-man specialized unit in 1964 by the Philadelphia Police Department in response to an alarming increase in bank robberies.
--------Line Feed2----------------
The purpose of this unit was to react quickly and decisively to bank robberies while they were in progress, by utilizing a large number of specially trained officers who had at their disposal a great amount of firepower. The tactic worked and was later soon to resolve other types of incidents involving heavily armed criminals.[2][3] Los Angeles Police Department (LAPD) Inspector Daryl Gates has said that he first envisioned "SWAT" as an acronym for "Special Weapons Attack Team" in 1967, but later accepted "Special Weapons and Tactics" on the advice of his deputy chief, Edward M. Davis.[4]
<p> </p>
<p><span><img src="https://upload.wikimedia.org/wikipedia/commons/1/19/Flag_of_Andorra.svg" border="0" alt="" /></span></p>
<p> </p>
Approach 1: When I pass above String to webview using (str is the String mentioned above)
webview.loadDataWithBaseURL(null,str,"text/html","utf-8",null)
The result is as indicated in Figure A below. The problem here is that the Line feed characters(\n) and whitespaces are removed by the webview.
Approach 2: When I pass above String to webview using
webview.loadDataWithBaseURL(null,Html.toHtml(SpannableString(str)),"text/html","utf-8",null)
The result is as indicated in Figure B below. The problem here is that the Line feed characters(\n) and whitespaces are shown properly but the HTML is also shown as plain text.
Approach 3: When I pass above String to webview using
webview.loadDataWithBaseURL(null,str.replace("\n",<br/>"),"text/html","utf-8",null)
The result is as indicated in Figure C below. The problem here is that the Line feed characters(\n) and HTML are rendered properly, but the whitespaces at the start of paragraphs are not shown.
My requirement is to achieve it as shown in Figure D below
What would be the optimum solution to achieve the desired result. I have created a sample application here for reference.