Thursday, 2 January 2014

Android Example: Calling Javascript from Android Java coding


In Android application, WebView is the component for display web pages. As Hybrid application are popular now, there are needs of calling javascript method from Android Java coding. We can do this by implement the addJavascriptInterface method in WebView.
The following example demonstarte this:
Step 1: Create a new Android project "JavascriptDemo" in Eclipse. A html web page is created in the assests directory.


Step 2: Modify activity_javascript_demo.xml layout document. Add a button in the WebView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
  <TextView    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Hello World!!"  
        />  
    <WebView  
        android:id="@+id/webview"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />  
    <Button  
        android:id="@+id/button"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="Change the webview content"  
    />  

</LinearLayout>  
Step 3: Create a demo.html file under assest directory.
 <html>  
     <script language="javascript"><!--  
    
  function fillContent(){  
      document.getElementById("content").innerHTML =   
    "This Content is showed by Android invoked Javascript function.";  
  }  
       
 // --></script>    
   <body>  
     <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>  
     <p id="content"></p>  
     <p>EasyInfoGeek.com</p>  
     <p>Javscript Demo ---- Android and Javascript interaction</p>  
     
   </body>  
 </html>   
Step 4: Modify JavascriptDemo.java
 package com.easyinfogeek.javascriptdemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

public class JavascriptDemo extends Activity {
  private WebView mWebView;  
     private Button mButton;  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_javascript_demo);  
         setupViews();  
     }  
     
     private void setupViews() {  
         mWebView = (WebView) findViewById(R.id.webview);  
         WebSettings mWebSettings = mWebView.getSettings();  
       
         mWebSettings.setJavaScriptEnabled(true);  
       
         mWebView.addJavascriptInterface(new Object() {  
       
             public void startMap() {  
                 Intent mIntent = new Intent();  
                 ComponentName component = new ComponentName(  
                         "com.google.android.apps.maps",  
                         "com.google.android.maps.MapsActivity");  
                 mIntent.setComponent(component);  
                 startActivity(mIntent);  
             }  
         }, "demo");  
       
         mWebView.loadUrl("file:///android_asset/demo.html");  
         mButton = (Button) findViewById(R.id.button);  
         mButton.setOnClickListener(new Button.OnClickListener() {  
             public void onClick(View v) {  
                 mWebView.loadUrl("javascript:fillContent()");  
             }  
         });  
     }  

}

 

0 comments:

Post a Comment