0

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>&#160;</p>
<p><span><img src="https://upload.wikimedia.org/wikipedia/commons/1/19/Flag_of_Andorra.svg" border="0" alt="" /></span></p>
<p>&#160;</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.

Figure A

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.

Figure B

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.

Figure C

My requirement is to achieve it as shown in Figure D below

Figure D

What would be the optimum solution to achieve the desired result. I have created a sample application here for reference.

zenith
  • 55
  • 3
  • 12

1 Answers1

0

Have you the hand on the HTML string ? If yes, you should move your text content in a <p> tag and add a padding left instead of whitespaces.

Like explains here : https://stackoverflow.com/a/1571662/6809926

Zenocode
  • 656
  • 7
  • 19
  • I cannot edit the String... it can be anything and is set by end user. – zenith May 31 '19 at 10:09
  • Ok so you could use regex to replace whitespaces by ` ` [(source)](https://www.w3schools.com/html/html_entities.asp#midcontentadcontainer) to make sure the whitespaces will be interpreted by web view. – Zenocode Jun 02 '19 at 12:02