Arithmetic Operations Using Webview In Android
Arithmetic Operations Using Webview In Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/webView1"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="CALCULATE" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
WebView webView;
Button button;
String compute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView1);
final MyJavaScriptInterface myJavaScriptInterface
= new MyJavaScriptInterface(this);
webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebChromeClient(new JsPopupWebViewChrome());
webView.loadUrl("file:///android_asset/www/hello.html");
button = (Button)findViewById(R.id.calculate);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder myDialog
= new AlertDialog.Builder(v.getContext());
myDialog.setTitle("Result");
myDialog.setPositiveButton("ok", null);
myDialog.show();
}
});
}
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void showToast(String toast){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
public void openAndroidDialog(){
AlertDialog.Builder myDialog
= new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("Alert!");
myDialog.setMessage("DONE BY HEMANTH");
myDialog.setPositiveButton("OK", null);
myDialog.show();
}
public void compute(){
AlertDialog.Builder myDialog
= new AlertDialog.Builder(MainActivity.this);
myDialog.show();
}
}
}
Step 4 : Open src -> package -> JsPopupWebViewChrome.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class JsPopupWebViewChrome extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(view.getContext())
.setTitle("Result")
.setMessage(message)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
b.show();
// Indicate that we're handling this manually
return true;
}}
Step 5 : Open Hello.html and add following code :
<!DOCTYPE HTML>
<HEAD>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<TITLE>Android</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
function compute(fm)
{
alert( fm.result.value = eval(fm.expr.value) );
}
function showAndroidToast(toast)
{
AndroidFunction.showToast(toast);
}
function openAndroidDialog()
{
AndroidFunction.openAndroidDialog();
}
</SCRIPT>
<H1>Android </H1>
<FORM>
Enter some expression:
<BR>
<INPUT TYPE="text" NAME="expr" SIZE=15>
<INPUT TYPE="button" VALUE="Calculate"onClick="compute(this.form)" >
<BR>
Result:
<BR>
<INPUT TYPE="text" NAME="result" SIZE=15>
<BR>
<BR>
<BR>
<BR>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Hemanth')" />
<input type="button" value="Open Dialog" onClick="openAndroidDialog()" />
</FORM>
</BODY>
</HTML>
Step 6 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 6 : Our output will be like this :