Reputation: 1405
I'm using some webviews in my android app, but are unable to make them display in utf-8 encoding.
If use this one I won't see my scandinavian charcters:
mWebView.loadUrl("file:///android_asset/om.html")
And if try this one, I won't get anything displayed at all
mWebView.loadDataWithBaseURL("file:///android_asset/om.html", null, "text/html", "utf-8",null);
Regards
Upvotes: 78
Views: 59602
Reputation: 983
Derzu's bit is very helpful above:
webview.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8");
I had UTF-8 on Android 2.x and garbled ANSI on 4.x until I put in the
charset=utf-8
in the wv.loadUrlWhatever()
call. Excellent attention to detail, Derzu
Upvotes: 27
Reputation: 22245
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
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");
You can also use this formulation with WebSettings
WebView webView = (WebView) findViewById(R.id.DemoWebView);
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", null);
It is amazing that Android still hasn't resolved this basic issue.
Upvotes: 135
Reputation: 14746
You can try to edit the settings of your webview before you load the data:
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
Also, as provided in the comment below, be sure to add "charset=utf-8"
to the loadData call:
mWebView.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8");
Upvotes: 158
Reputation: 5890
mwebView.loadData(URLEncoder.encode(data, "utf-8").replaceAll("\\+"," "), "text/html", "utf-8");
Upvotes: 0
Reputation: 3368
You should keep 3 things in mind to show the right content always:
The examples have been provided via other answers so I don't repeat!
Upvotes: 1
Reputation: 91
WebView wv = (WebView) findViewById(R.id.rowWebview);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
wv.loadData(topHtml, "text/html; charset=utf-8",null);
A combination of the two seems to work for me. For some reason it likes null on the encoding and the charset in the mime type :/ weird. this has solved months of aggravation for me.
Upvotes: 9
Reputation: 7700
There are two ways that a HTML page delivered by a HTTP server can specify the content encoding. Usually, the server will specify the content encoding in the HTTP headers, but since this page is being loaded from a file, there is no HTTP transaction and therefore no headers. As a result, WebView assumes a default encoding of Latin-1.
However, you can specify a content encoding using the <meta>
tag. Construct your html file thus:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Title</title>
</head>
Your content following
And then load it into WebView using mWebView.loadUrl("file:///android_asset/om.html");
. It should display the non-latin characters as you expect.
Upvotes: 12
Reputation: 8477
You need to swap your first two arguments. See this thread: Android WebView UTF-8 not showing
So your code should look like this:
mWebView.loadDataWithBaseURL(null, "file:///android_asset/om.html", "text/html", "utf-8",null);
Upvotes: 2
Reputation: 3554
I'm not sure what you are doing prior to loading that page. Could this security change have anything to do with it? Are you loading page from web before?
Note for post 1.0. Due to the change in the WebKit, the access to asset files through "file:///android_asset/" for the sub resources is more restricted. If you provide null or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset files for sub resources.
Taken from here: http://developer.android.com/reference/android/webkit/WebView.html In the section on method "loadDataWithBaseURL".
Can you use "loadData" instead for a quick test? Specify "utf-8" for the encoding and pasting a scandinavian character into the data parmeter. Simple test to remove the security issue.
Upvotes: 0