Android WebView UTF-8 not showing

Viewed 27278

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.

3 Answers

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.

Related