Spring Breaker
Spring Breaker

Reputation: 8251

How to show html texts in Android WebView in proper format?

I have a string which contains html texts and images parsed from a json file.I need to show it in webview.The string consists of 3-4 paragraph but while displaying in webview it shows like one paragraph i.e the android webview skips the breakline tags.But my requirement is to show as it is in the website with proper alignment.Please help me.

my Json file link is this

and my webview code is

//newsContent contains the json string after parsing.

  String all="<html><body>"+newsContent+"</body></html>";

                 tv.getSettings().setJavaScriptEnabled(true); 
                 tv.getSettings().setPluginsEnabled(true);
                 tv.getSettings().setSupportZoom(false);
                 tv.getSettings().setAllowFileAccess(true);
                 WebSettings webSettings = tv.getSettings();
                 tv.getSettings().setAllowFileAccess(true);
                 webSettings.setTextSize(WebSettings.TextSize.NORMAL);

                 tv.loadDataWithBaseURL(null,all, "text/html", "UTF-8", null);

Upvotes: 0

Views: 4682

Answers (3)

Zakaria Bin Abdur Rouf
Zakaria Bin Abdur Rouf

Reputation: 1995

Below works. There should be double slash \r\n

String all=""+newsContent.replace("\\r\\n", "
")+"";

Upvotes: 0

saigopi.me
saigopi.me

Reputation: 14938

String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ac quam risus, at tempus justo. Sed dictum rutrum massa eu volutpat. Quisque vitae hendrerit sem.";
WebView webView = (WebView) findViewById(R.id.WebView);
webView.loadData(String.format(text, data), "text/html", "utf-8");

Upvotes: 0

PH7
PH7

Reputation: 3926

The problem lies with "\r\n" in your json data. Look like \r\n work only if they are wrapped in tag. Easy solution would be replace \r\n with
tag.

String all="<html><body>"+newsContent.replace("\r\n", "<br/>")+"</body></html>";

Upvotes: 4

Related Questions