0

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);
6
  • 1
    Can you show us your json and code? Commented Nov 16, 2012 at 6:22
  • 1
    @ambrish : I have edited my question.Please see it. As I am working on a bit big project, I cant show the whole code here. Commented Nov 16, 2012 at 6:29
  • I am sure Json tag will not work properly in WebView Commented Nov 16, 2012 at 6:32
  • @Sameer: Dude there must be some way to achieve this.The same json file works well in iPhone webview. Commented Nov 16, 2012 at 6:34
  • Have you tried this tv.loadurl(all, "text/html", "UTF-8") ? Commented Nov 16, 2012 at 6:37

3 Answers 3

4

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>";

Sign up to request clarification or add additional context in comments.

6 Comments

yeah you are right.Already I have solved it but anyways I will mark your answer as accepted.
@User_2013 mind posting your answer I am stuck in this too and this answer is not working for me
@IllegalArgument: This answer is based on the context of my question.If you can explain your problem, then we can help you.
@User_2013 I am receiving response like "i am \r\n a \r\n <br/> <p> subash </p>" it can be anything with html tags and \r\n replacing \r\n like above is not an option in my case because users can be allowed to type \r\n
@IllegalArgument:WebView reads \r\n as a simple string but I want to treat as a breakline so I replaced \r\n with <br>.
|
0
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");

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.