user1025050
user1025050

Reputation:

Implementing html in android

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

Upvotes: 0

Views: 124

Answers (2)

Robert Rouhani
Robert Rouhani

Reputation: 14688

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.

Upvotes: 0

Dinesh Sharma
Dinesh Sharma

Reputation: 11571

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.

Upvotes: 1

Related Questions