Home » Android » Directly put html code in a WebView (Android)

Directly put html code in a WebView (Android)

Posted by: admin April 23, 2020 Leave a comment

Questions:

While using WebView, we usually put a URL for it:

WebView.loadUrl(myURL);

but it is possible to put HTML code directly?? So that it will be in a logic that:

WebView.loadContent ( <html><head><script></script></head><body>....</body></html> );

Thanks.

How to&Answers:

Check out this:
http://developer.android.com/reference/android/webkit/WebView.html

 // OR, you can also load from an HTML string:
 String summary = "<html><body>You scored <b>192</b> points.</body></html>";
 webview.loadData(summary, "text/html", null);

Answer:

 String yourhtmlpage = "<html><body>You scored <b>hello world</b> points.</body></html>";
 webview.loadDataWithBaseURL(null, yourhtmlpage, "text/html", "UTF-8", null);

Answer:

Try this code. It works for me.

WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);