-1

I am developing an android application where I need to load a URL which contains the Hindi fonts in web view.

I used the following code for this:

WebView webView = (WebView) findViewById(R.id.webView);

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setDefaultTextEncodingName("utf-8");

webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(url);

This code is working fine in most of the latest devices and showing Hindi contents properly. But in Android 2.2, 2.3 or other lower versions, it is showing boxes rather than Hindi charactors.

How can I support web view to be enabled for non-english test so my application can run on all devices.

Thanks in advance...

Manoj Agarwal
  • 703
  • 3
  • 17
  • 39

2 Answers2

1

try the below link :Click here

private boolean copyFile(Context context,String fileName) {
        boolean status = false;
        try { 
            FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            InputStream in = context.getAssets().open(fileName);
            // Transfer bytes from the input file to the output file
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            // Close the streams
            out.close();
            in.close();
            status = true;
        } catch (Exception e) {
            System.out.println("Exception in copyFile:: "+e.getMessage());
            status = false;
        }
        System.out.println("copyFile Status:: "+status);
        return status;
    }

3.You have to call above function only once (you have to find some logic for this).

copyFile(getContext(), "myfont.ttf");

4.Use the below code to set value for your webview. Here I am using CSS to set font.

private String getHtmlData(Context context, String data){
    String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ context.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style></head>";
    String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;
    return htmlData;
 }

5.You can call the above function as below

webview.loadDataWithBaseURL(null, getHtmlData(activity,htmlData) , "text/html", "utf-8", "about:blank");

Community
  • 1
  • 1
Yogesh Tatwal
  • 2,722
  • 20
  • 43
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – 웃웃웃웃웃 Aug 09 '13 at 08:42
0

Me also struggled with this for one day but finally found a solution.

Just add these two lines of code it will work

 WebSettings webSettings = webView.getSettings();
 webSettings.setDefaultTextEncodingName("utf-8");
Manik
  • 153
  • 4
  • 15