0

I am making an app in which I have to use html in my java code, Actually I am making a table whose rows and columns will get their values dynamically. My code snippet is as follows:

WebView myWebView;
public static String Monthlysavings,Month,Savings,January,$100,February,$80;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            myWebView = (WebView) findViewById(R.id.webview);
         //   myWebView.loadUrl("file:///android_asset/neww.html");

            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);


            String summary = "<html> <body>" + "<table border='1'>" + "<caption>" + Monthlysavings + "</caption> <tr> <th>" + Month + "</th><th>" + Savings + "</th></tr>" + "<tr><td>" + January + "</td><td>" + $100 + "</td></tr>" + "<tr><td>" + February + "</td><td>" + $80 + "</td></tr></table></body></html>";


            myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

            myWebView.loadUrl(summary);

But i am getting following output instead of table , i am getting following outputinstead of table view , i am getting the above

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80

2 Answers2

1

Instead of using myWebView.loadUrl(summary) use:

 myWebView.loadData(summary,"text/html", "UTF-8");

OR

 myWebView.loadDataWithBaseURL(null, summary,"text/html", "utf-8", null);

Hope this will work for you.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
0

From the WebView documentation page: http://developer.android.com/reference/android/webkit/WebView.html

Use

webview.loadData(summary, "text/html", null);

instead of trying to load the page as a URL.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • that's just copied from the example. If you look at the documentation for that method: http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String) it's the encoding of the string, which doesn't matter. – Robert Rouhani Jan 11 '12 at 06:20
  • Thanks for help Dinesh and +1 for help –  Jan 11 '12 at 06:29
  • HAve you any idea how to use javascript in android ,i found the similar problem that i am facing on below link http://stackoverflow.com/questions/8799764/integrate-javascript-in-android/8801642#8801642 –  Jan 11 '12 at 06:31
  • Javascript should work the same as any other website, include it in the data you load. Only use `addJavascriptInterface` if you want to call javascript from Java or vice versa. – Robert Rouhani Jan 11 '12 at 06:37
  • have you checked the link that i have mentioned because i am facing same problem that Aditya1510 is facing –  Jan 11 '12 at 06:49
  • 1
    If you're talking specifically about `alert()` then the solution was linked to directly in one of the comments on an answer. You have to use a WebChromeClient to get proper alert functionality. http://code.google.com/p/android/issues/detail?id=752#c12 – Robert Rouhani Jan 11 '12 at 07:03