in Android, Android Code Tips, Programming

WebView not displaying UTF-8 Data

If you have a WebView and you want to display html code, you will probably use the WebView.loadData function as seen in the example here.

String htmlCode = "<html><body><b>hello man!</b> non bold text</body></html>";
mWebView.loadData(htmlCode, "text/html", "UTF-8");

Done by the book, nice try, though it does not always work.
When the html code contains UTF data, you have to base encode all the html data.
You can accomplish that by using the loadDataWithBaseURL function.
example:

String htmlCode = "<html><body><b>hello man!</b> non bold text</body></html>";
mWebView.loadDataWithBaseURL(null, htmlCode, "text/html", "UTF-8", null);

Ready :)

Share