Question

Android WebView UTF-8 not showing

I have a webview and am trying to load simple UTF-8 text into it.

mWebView.loadData("將賦予他們的傳教工作標示為", "text/html", "UTF-8");

But the WebView displays ANSI/ASCII garbage.

Obviously an encoding issue, but what am I missing in telling the webview to display the Unicode text?

This is a HelloWorld app.

 45  27756  45
1 Jan 1970

Solution

 120

Use:

mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "utf-8", null);

or using WebSettings with setDefaultTextEncoding:

WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");

For recent versions of Android, API 16 to 22 it was tested and work properly using loadData() method, requires the mimeType to include: "charset=utf-8".

WebView mWebView = (WebView) findViewById(R.id.myWebView);
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");                   
mWebView.loadData(myCharacters, "text/html; charset=utf-8",null);

or

  mWebView.loadData(myCharacters, "text/html; charset=utf-8","UTF-8");
2010-07-22

Solution

 11

This problem goes back to at least Gingerbread

This seems to have been broken in some form or fashion forever. Issue 1733

Use loadDataWithBaseURL instead of loadData

// Pretend this is an html document with those three characters
String scandinavianCharacters = "øæå";

// Won't render correctly
webView.loadData(scandinavianCharacters, "text/html", "UTF-8");

// Will render correctly
webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null);

Now the part that is truly annoying is that on the Samsung Galaxy S II (4.0.3) loadData() works just fine, but testing on the Galaxy Nexus (4.0.2) the multi-byte characters are garbled unless you use loadDataWithBaseURL(). WebView Documentation

Recent versions of Android

Some are reporting a change in the behavior of the loadData calls requiring the mimeType to include charset=utf-8.

webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", "UTF-8");

Discussion

The first time I saw this my boss brought me his phone, an early Nexus, while I was developing at the time on a Samsung Galaxy II and it showed up in our economic news feed on his phone which had a lot of non-ASCII characters. So, not only is this a long standing issue within Android, but it also isn't consistent between device makers. This is a matter where you have to program defensively.

2016-03-06