44

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.

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 1
    Solution: Use the other other method on WebView --> mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "UTF-8", "about:blank"); – Ian Vink Jul 22 '10 at 20:48

3 Answers3

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");
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Thanks, any idea why this works, is it a bug? For me I can display UTF correctly using `loadData` pre-Honeycomb but it doesn't work on Honeycomb. I've made the change as above for Honeycomb and it is now displaying correctly. – PJL Aug 12 '11 at 11:11
  • 2
    Hi PJL it was reported as an issue "Issue 3552 - android - WebView.loadData() doesn't show Japanese ..." code.google.com/p/android/issues/detail?id=3552, but now its just a broken link =0 – Jorgesys Aug 12 '11 at 16:15
  • 1
    Not only Japanese, it seems that this problem appears with Arabic characters too. – TMMDev Jun 28 '14 at 12:35
  • 3
    loadDataWithBaseURL() is working for me. WebSettings does not. – Wooff Aug 14 '14 at 08:05
  • 2
    loadDataWithBaseURL() is working for me too :) great ans.thanks :) – Nevaeh Jan 14 '15 at 12:13
  • 1
    hey can u plz specify what is the 2nd parameter on loadDatawithBaseURL – Dipen Feb 19 '15 at 12:26
  • the second parameter, data is a string to load into the webview. http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) – Jorgesys Feb 19 '15 at 15:31
  • Any chance you could explain why the first method is working? instead of loadData? – Mathijs Segers May 09 '15 at 09:18
  • 3
    loadDataWithBaseURL() worked for me too. WebSettings does not make any difference. – Bharat Dodeja May 19 '15 at 13:46
  • Unfortunately this does not work for android 4.4. any more :-(. This duplicate question [android-webview-with-garbled-utf-8-characters](http://stackoverflow.com/questions/4933069/android-webview-with-garbled-utf-8-characters) has a working solution for more modern androids – k3b May 28 '15 at 09:11
  • 1
    Thanks for it! mWebView.loadData(topHtml, "text/html; charset=utf-8",null); work for me for arabic. – jiahao Sep 02 '15 at 14:05
  • 1
    Thank you! it works for me, italian accented characters – moondroid Dec 11 '15 at 10:12
  • @MathijsSegers If you check my answer you'll see this is an issue with Android itself. Furthermore, it will exhibit different behavior on different manufacturers version of Android. – Cameron Lowell Palmer Apr 13 '16 at 08:29
10

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.

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
0

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.

Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46