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

<html>
	<title>WEB-API Demo</title>
	<script language="JavaScript">
		function f1()
		{
			alert("hello");
		}
	</script>
	<body>
		<form>
			<input type="button" value="hello" onClick="f1()"/>
		</form>
	</body>
</html>

Assets -> test1.html

<html>
<head>
<script type="text/javascript">
function doAlert() 
{
	alert("This is an alert.");
}
function doToast()
{
	mca5a.testtoast(document.getElementById("form_text").value,50);
}
function doLog()
{
	console.log("this is test log");
}
function doSetHTMLText(update)
{
	document.getElementById("form_text") = update;
}
</script>
</head>
<body>
<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();" />
</body>
</html>

WebAPIDEmo.java

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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final WebView wv = (WebView)findViewById(R.id.wv1);
		//final TextView tv = (TextView)findViewById(R.id.textView1);
		wv.getSettings().setJavaScriptEnabled(true);
		wv.setWebChromeClient(new WebChromeClient());
		wv.addJavascriptInterface(new test(), "mca5a");
//		wv.loadUrl("file:///android_asset/test.html");
		wv.loadUrl("file:///android_asset/test1.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();
		}
	}

	@Override
	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;
	}

}

Advertisements

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