How to show Hindi(non-english) webpages using web view in android application


How to show Hindi(non-english) webpages using web view in android application



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...





Have u refer it stackoverflow.com/a/8203564/1915697
– Yogesh Tatwal
Aug 8 '13 at 13:16





@Yogesh, Thanks for your reply. I have already refer this. But I am not clear, how to replace system/fonts with downloaded hindi fonts? And this approach will work on all devices? Please reply me in detail.
– Manoj Agarwal
Aug 9 '13 at 6:26






You should overwrite /system/fonts/DroidSansFallback.ttf using adb or another tool. Make sure to have a backup of the file first.
– Yogesh Tatwal
Aug 9 '13 at 6:35





please refer link if you are not able to push file using adb pocketmagic.net/2009/05/…
– Yogesh Tatwal
Aug 9 '13 at 6:40





For this, we have to root the device? If yes, then I do not want to root the device.
– Manoj Agarwal
Aug 9 '13 at 6:45




2 Answers
2



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");





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 9 '13 at 8:42



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");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails