5

I am using the following code for encoding the html file which is in my asset folder. I have gone through various link here but was not successful. Here is my piece of code.

 WebSettings settings = myWebVeiw.getSettings();
    //settings.setJavaScriptEnabled(true);
    //myWebVeiw.getSettings().setJavaScriptEnabled(true);
    settings.setDefaultTextEncodingName("utf-8");
    //settings.setDefaultTextEncodingName("ISO-8859-1");

    myWebVeiw.loadUrl("file:///android_asset/"+totaldays+".html"); 

Although it is working for other characters but it is not able to encode †.as it prints same on the web view. Please suggest me what to do.

Any help will be appreciated.

TNR
  • 5,839
  • 3
  • 33
  • 62
vyas
  • 414
  • 3
  • 8

3 Answers3

11

You need to set the WebSettings default text encoding to utf-8. And after force the html header charset to utf-8. Exmple:

String htmlText = "<html> your page here </html>";
WebView web = (WebView) findViewById(R.id.webview); // get your webview from the layout
WebSettings settings = web.getSettings();
settings.setDefaultTextEncodingName("utf-8");
web.loadData(htmlText, "text/html; charset=utf-8", "utf-8");

That works for me on android 2.2.1, 4.0.4 and 4.1.2.

Derzu
  • 7,011
  • 3
  • 57
  • 60
1

Could you try and check:

myWebVeiw.loadDataWithBaseURL("file:///android_asset/"+totaldays+".html", null, "text/html", "utf-8",null);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Yes I have already tried with it but it shows nothing more than a blank page. Can this be the reason that the html may be not correctly decoded. – vyas Jan 23 '13 at 07:59
0

Not so familiar with webView. But the browser parse a html charset encoding following: 1.first charset .so please check the totaldays.html <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 2. according the content.

xff1874
  • 336
  • 1
  • 4
  • 9
  • 1
    Meta is not helping in my case. as it is static html i guess so I have tried by it..Waiting for the client. I guess this may be the bad html decoding form the client thats why not encoding on android side. – vyas Jan 23 '13 at 08:49