0

I know this problem has been documented elsewhere but the solutions don't seem to work for me. Other similar questions:

Android WebView with garbled UTF-8 characters.

Android WebView UTF-8 not showing

I'm essentially trying to show the minus/plus character (∓) in an android webview. I tested several other characters 'around' the minus plus character in the UTF-8 table but some of them didn't work either

Here is the java im using:

final WebView w = (WebView) findViewById(R.id.webview1);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setDefaultTextEncodingName("utf-8");        

InputStream is;
try {
    is = getAssets().open("test5.html");

    int size = is.available();

    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();

    String str = new String(buffer);

    w.loadData(str, "text/html; charset=utf-8", "utf-8");

Here is the html test5.html

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    char: ∓ <br/>
    char: ∔ <br/>
    char: ∕ <br/>
    char: ∖ <br/>
    char: ∗ <br/>
    char: ∘ <br/>
</body>

The only characters that show up are the "∕" and "∗". I've also tried

w.loadDataWithBaseURL("file:///doesnotmatter", str, "text/html", "utf-8", "");

with no success. I'm not too familiar with the input stream thing so I don't know if there's something wrong there. Please help, its taken me awhile =\

-Teneth

Community
  • 1
  • 1
bluemoon12
  • 73
  • 1
  • 8

2 Answers2

0

When you call loadData(), pass the encoding as uppercase "UTF-8", because it is case sensitive and that's the standard representation according to the documentation.

gknicker
  • 5,509
  • 2
  • 25
  • 41
  • I tried "w.loadData(str, "text/html; charset=UTF-8", "UTF-8");" but nothing changed. I then also put caps in the "w.getSettings().setDefaultTextEncodingName("UTF-8");" but nothing changed. =\ – bluemoon12 Jan 28 '15 at 03:20
  • Then [save your HTML file with UTF-8 encoding](http://stackoverflow.com/questions/16598785/save-text-file-in-utf-8-encoding-using-cmd-exe). Perhaps even include [BOM](http://en.m.wikipedia.org/wiki/Byte_order_mark) to ensure its encoding is interpreted as UTF-8. – gknicker Jan 28 '15 at 04:04
0

use html codes for that characters in your html file:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
   char: &#8723;<br/>
   char: &#8724;<br/>
   char: &#8725;<br/>
   char: &#8726;<br/>
   char: &#8727;<br/>
   char: &#8728;<br/>
</body>
Jorgesys
  • 124,308
  • 23
  • 334
  • 268