Monday, 12 October 2015

Call JavaScript function inside WebView from Android Activity


   Here I am going to explain Call JavaScript function inside WebView from Android Activity
                                     
                                                             
Step 1)MainActivity .java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity 
 {
WebView webView;
  EditText text;
  Button Show;
@Override
protected void onCreate(Bundle savedInstanceState) 
   {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity MyActivity = this;
   text=(EditText)findViewById(R.id.textValue);
   Show=(Button)findViewById(R.id.textButton);
webView= (WebView) findViewById(R.id.webview);
   getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

MyActivity .setTitle("Loading...");
MyActivity .setProgress(progress * 100);

if (progress == 100)
MyActivity .setTitle("Android Dhina");
}
});
webView.setWebViewClient(new WebViewClient());
webView.addJavascriptInterface(new MyJavaScriptInterface (this), "Android");

webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/web.html");

  Show.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) 
          {
            webView.loadUrl("javascript:callFromAndroidActivity
                                  (\""+text.getText().toString()+"\")"); }
});

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("DANGER!");
     myDialog.setMessage("You can do what you want!");
     myDialog.setPositiveButton("ON", null);
     myDialog.show();
    }

}
}


Step 2)activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
     android:orientation="vertical"
    android:layout_height="match_parent">
   
    <EditText
        android:id="@+id/textValue"
        android:textColor="@android:color/holo_blue_light"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/textButton"
        android:text="Show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

      <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Step 3)assets/web.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Android Dhina</title>

<h1>Android Dhina Webview</h1>
<p id="mytext">Dhina</p></head>
<body>

<script language="javascript">
function callFromAndroidActivity(msg)
 { 
        document.getElementById("mytext").innerHTML = msg; 
 }

</script>

</body>
</html>


Happy Coding :)