Reputation: 68740
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.
Upvotes: 45
Views: 27847
Reputation: 2455
So much time has elapsed and still an issue!
None of these answers worked for me. Maybe I had a slightly different situation. The text string I was loading was coming from a file in res/raw and it is being displayed in an AlertDialog. I had three unicode symbols in the file. I tried all of the methods above and everyone one worked and produced identical results on the Android screen, but the unicode symbols were displayed as their raw form, for example \u1F4F6. They were not rendered.
I was using an Android Nexus 5 OS 6.
Finally I did this (changing the \u to 0x)
WebView help = helpContent.findViewById(R.id.helpView);
help.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return true;
}
});
helpText = helpText.replace("0x1F4F6", new String(Character.toChars(0x1F4F6)));
helpText = helpText.replace("0x1F4DE", new String(Character.toChars(0x1F4DE)));
helpText = helpText.replace("0x2796", new String(Character.toChars(0x2796)));
help.loadData(helpText, "text/html; charset=utf-8", "UTF-8");
and it worked. This is not a solution but a pathetic hack and would never work in general. If anyone knows why I need to do this I would be grateful!
Note I also tried different manners of representing the unicode symbols as, for example, in w3schools and none worked.
Upvotes: 0
Reputation: 126445
Use:
mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "utf-8", null);
or using WebSettings with setDefaultTextEncoding:
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("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");
Upvotes: 120
Reputation: 22235
This seems to have been broken in some form or fashion forever. Issue 1733
// 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
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");
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.
Upvotes: 11