0

I have used the following code to display a string in android Webview:

    webView.loadData(anEnglishString + "<br />" + aPersianString, "text/html", "utf-8");

Also Note that I tried to substitute "utf-8" with: "unicode", "utf8", "utf-16", "ansi", ... But each time I got the following output in WebView:

This message is in English
%%#:-=%@+=%-#@##%@%

By the way, I also tried to use:

    webView.loadDataWithBaseURL(null, ..., null);

as well as:

    webView.getSettings().setDefaultTextEncoding();

But I'm still getting the same wired characters for Persian message.

1
  • Is aPersianString properly constructed using some persian known encoding? or using utf-8 Commented Oct 7, 2013 at 8:24

3 Answers 3

2

I finally used the following code to solve the problem:

    WebView.LoadUrl("data:text/html;charset=UTF-8," + nonEnglishString);
Sign up to request clarification or add additional context in comments.

Comments

1

I have also come with the same problem and i fixed that problem.

You should use like this,

try {
                // get input stream for text
                InputStream is = getAssets().open("YOUR HTML.html");//index.html
                // check size
                int size = is.available();
                // create buffer for IO
                byte[] buffer = new byte[size];
                // get data to buffer
                is.read(buffer);
                // close stream
                is.close();
                webView.loadDataWithBaseURL(null, new String(buffer),
                        "text/html", "utf-8", null);
            } catch (IOException e) {
                e.printStackTrace();
            }

Comments

0

use this:

webview.loadData(html_content, "text/html; charset=utf-8", "utf-8");

I tested it, and it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.