UTF-8 does not encode html in webview android

I use the following code to encode an html file that is located in my resource folder. I went through various links here, but was not successful. Here is my code snippet.

 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 works for other characters, it cannot code as it prints the same in a web view. Please suggest me what to do.

Any help would be appreciated.

+5
source share
3 answers

You need to install the standard text encoding of WebSettings for utf-8. And after the power of the html header, it encodes 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");

This works for me on Android 2.2.1, 4.0.4 and 4.1.2.

+10
source

Could you try:

myWebVeiw.loadDataWithBaseURL("file:///android_asset/"+totaldays+".html", null, "text/html", "utf-8",null);
+1
source

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

0
source

All Articles