3

I have following Test Android App.

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/test.html");        
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sudoku, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Need to call javascript function testFun() here (see test.html)
  }
}

Code for test.html

<!doctype html>
<html>
  <head>
  <title>Test</title>
  <meta charset="UTF-8" />
  <script type="text/javascript">
    function testFun()
     {
       alert('Hi');
    }
  </script
  <body>
        <button type="button" onclick="testFun()">Test</button>
  </body>
</html>

I read about calling android functions in javascript http://developer.android.com/guide/webapps/webview.html

But could not get how to call javascript functions from android (menu item click).

1 Answer 1

7

Have you tried this.

webView.loadUrl("javascript:testFun()");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.