Web – API Demo

Following Code Demonstrates How to Display HTML page stored in Asset folder to display in webview
test.html file contains HTML + JavaScript code
test1.html is an example where Android Source code is communicating with Javascript code Stored in Assets folder
and viceversa.

Assets – test.html

	<title>WEB-API Demo</title>
	<script language="JavaScript">
		function f1()
			<input type="button" value="hello" onClick="f1()"/>

Assets -> test1.html

<script type="text/javascript">
function doAlert() 
	alert("This is an alert.");
function doToast()
function doLog()
	console.log("this is test log");
function doSetHTMLText(update)
	document.getElementById("form_text") = update;
<h2>This is a test.</h2>
<input type="text" value="data" id="form_text"/>
<input type="button" value="Alert" onclick="doAlert();" />
<input type="button" value="Toast" onclick="doToast();" />
<input type="button" value="Log" onclick="doLog();" />


package com.example.webapidemo5a;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		final WebView wv = (WebView)findViewById(R.id.wv1);
		//final TextView tv = (TextView)findViewById(R.id.textView1);
		wv.setWebChromeClient(new WebChromeClient());
		wv.addJavascriptInterface(new test(), "mca5a");
//		wv.loadUrl("file:///android_asset/test.html");
	public void setHTMLText(View view) 
		WebView wv = (WebView) findViewById(R.id.wv1);
		wv.loadUrl("javascript:doSetHTMLText('Java->JS call');");
	public class test
		public void testtoast(String msg,int duration)
			Toast.makeText(getApplicationContext(), msg, duration).show();

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s