loading local html file in local language in webview in android - android

I am developing an android information app for farmers.Informations will be given in local indian language.I am using webview for that purpose.Informations are stored in html files.The problem is that the informtions which are in local language are not visible.How to solve this?
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to WebView of the activity_main layout
mWebView = (WebView) findViewById(R.id.webview);
// Loading an html page into webview
mWebView.loadUrl("file:///android_asset/text.html");
}
Activity main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_world" />
</RelativeLayout>
html file
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="UTF-8">
<style>
/** Specify a font named "MyFont",
and specify the URL where it can be found: */
#font-face {
font-family: "MyFont";
src: url('file:///android_asset/b.ttf');
}
h3 { font-family:"MyFont"}
</style>
</head>
<body>
<h3>
ಪ್ರಶಾಂತ
</h3>
</body>

You mean to say you want to show helloworld in regional language. For that you need to create res/values- folder. The android framework will automatically pick up the right language
Details:
http://developer.android.com/guide/topics/resources/localization.html

Below are couple of stackoverflow posts which might help you.
How to show local languages font.
How to use custom font with webview.

Try this
add this statement to your onCreate().
mWebView.getSettings().setJavaScriptEnabled(true);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to WebView of the activity_main layout
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
// Loading an html page into webview
mWebView.loadUrl("file:///android_asset/text.html");
}

Related

CSS and JavaScript not working in Android WebView

My MainActivity.java file looks like below:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String myUrl = "file:///android_asset/index.html";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setSaveFormData(true);
view.setWebChromeClient(new WebChromeClient());
view.loadUrl(myUrl);
}
}
Now, activity_main.xml file contains following codes:
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
And, in the html page index.html, I have been linking CSS and JS files like below:
<link rel="stylesheet" href="file:///android_asset/css/bootstrap.min.css">
<script src="file:///android_asset/js/popper.min.js"></script>
I am seeing the html content; but, the CSS and JavaScript files are not in action at all. The page looks plain. I am new to Android, I am certainly missing something crucial here.
In the html page index.html, use the path names relative to your html page (not file:///android_asset).
So change this:
<link rel="stylesheet" href="file:///android_asset/css/bootstrap.min.css">
<script src="file:///android_asset/js/popper.min.js"></script>
To this:
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/popper.min.js"></script>
with directory structure:
+---assets
¦ +---css
¦ +---js

Arabic custom font in Android webView

I am trying to add Arabic custom fontto webView with no luck. In my case, webView renders the default font shipped with the device.
I have tried ttf and otf font types also converted font type to svg through https://everythingfonts.com with no luck
here is my code:
html file stored in the assets folder:
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="UTF-8">
<style>
/** Specify a font named "MyFont",
and specify the URL where it can be found: */
#font-face {
font-family: "MyFont";
src: url('file:///android_asset/fonts/NotoKufiArabic-Bold.ttf') format(‘truetype’); ;
}
h3 { font-family:"MyFont"}
</style>
</head>
<body>
<h3>
مرحبا</h3>
</body>
</html>
MainActivity where i call webView:
public class MainActivity extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to WebView of the activity_main layout
mWebView = (WebView) findViewById(R.id.webview);
// Loading an html page into webview
mWebView.loadUrl("file:///android_asset/demo.html");
}
...........
...........
my code is working fine in case of English text and font but nothing for Arabic.
Use <h3 dir="rtl" lang="ar"> arabic text </h3> :
dir="rtl" inform that the text is arabix
lang="ar" renders form right to left

why html5 feature not working in my android application?

I want to work with html file, I did it when I deploy it, it won't workout (The input html page need to be shown but it shows webpage not available when I deploy ) ! Error msg also not in LOG !Any help please. Where is the wrong I'm doing ?
public class TestActivity extends Activity {
/** Called when the activity is first created. */
WebView webView =null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView =(WebView)findViewById(R.id.web);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android-assets/www/index.html");
}
}
Here my javascript.
function sayhello(){
alert("hi", document.getElementById('name'),value);
}
and html file is here
<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
</head>
<body>
What is ur name ?
<input id="name" value="">
<button onclick="sayhello()">say hello</button>
</body>
</head>
</html>
and the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/web"
android:text="#string/hello"
/>
</LinearLayout>
You need to use:
file:///android-asset/www/index.html
Just get rid of the 's'
UPDATE:
You can also load pages with:
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8",null);
Where the second param is your page to load.

edit div tag in a webview

I'm trying to make an Android app with a webview in which I have an html page. This one contains a "div" tag with the contentEditable property set to true.
My problem: by launching the app, I can't edit the text.
I've been doing a lot of searches on Google but there was nothing on this topic. I was wondering if there was any permission missing (as the one to reach the net in the manifest) but I'm able to edit input and textarea tags (among others). I've refered to the WebView's API, WebViewClient, WebChromeClient but I didn't find any method which would configure a particular permission in order to do that.
Does the webview understand the contentEditable property? It's not working, even if the webview is supposed to interpret html.
My activity:
public class WebAppStackOverFlowActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webView) ;
webView.setWebChromeClient (new WebChromeClient()) ;
webView.loadUrl("file:///android_asset/www/index.html") ;
}
}
My asset/www/index.html file:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input id="name" value="this is an input tag" />
<br />
<div contenteditable="true">this is an editable div tag</div>
<br />
<textarea rows="" cols="">this is a textarea tag</textarea>
</body>
</html>
My main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
contentEditable is supported by the default browser on Android version 3.0 (Honeycomb) and up (source).
So, if you're running this on Gingerbread, that would explain why it's not working.

Display hindi html in android webview

I am making an app in which i have to show hindi text which is coming in html means i have to show whole html page whose content is in hindi.Can anyone help me in this .Any help will be appreciated.My code is as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/test.html");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
and html file is as follows:
<html>
<head>
<meta charset="ISO-8859-1">
<title>New test</title>
</head>
<body>
<body>
<p>
केवल उन व्यक्तियों के पात्र हैं जो हिन्दी के साथ 10 वीं कक्षा उत्तीर्ण की है
</p>
If you have text file copy the file in to assets folder.
Then read it from assets and using Webview you can display the content.
webview.loadUrl("file:///android_asset/hinditext.html");

Categories

Resources