title: Android : callback function from java script to java
body: <p>I have created an application in which i am using webview and loading a simple static html page. I am calling a java script function from activity but i am unable to call a function from java script. I have tried few of the link , but it didn't work . <br/><a href="https://stackoverflow.com/questions/7019265/javascript-callback-function-pass-to-android">Javascript Callback function pass to Android</a><br/>
  <a href="https://stackoverflow.com/questions/6636118/i-cannot-call-an-android-function-from-javascript?rq=1">I cannot call an android function from javascript</a>
  <br/>
  Here is my code. Thank you in advance.</p>
  
  <p><br/>acitivity_main.xml</p>
  
  <pre><code>&lt;Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Call JavaScript Function"
          android:onClick="callJavaScript" /&gt;
  
  &lt;WebView
      android:id="@+id/myWebView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" /&gt;
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript"&gt;
          function displayMessage(){
              document.getElementById('test1').innerHTML = 'This is from java script.';
              Android.returnResult();
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1 id="test1"&gt;Hello World&lt;/h1&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Main activity has following two functions .</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          myWebView = (WebView) this.findViewById(R.id.myWebView);
          myWebView.getSettings().setJavaScriptEnabled(true);
          myWebView.addJavascriptInterface(new JavaScriptHandler(this), "Android");
          myWebView.loadUrl("file:///android_asset/index.html");
      }
  
      public void callJavaScript(View view) {
          Log.v(null, "Calling java Script");
          myWebView.loadUrl("javascript:displayMessage()");
      }
  </code></pre>
  
  <p>This is the JavaScriptHandler class</p>
  
  <pre><code>public class JavaScriptHandler {
      Context mContext;
  
      public JavaScriptHandler(Context context) {
          mContext = context;
      }
  
      public void returnResult() {
          Log.v(null, "result received");
      }
  }
  </code></pre>