0

I have a simple page in html:

<html>
<head>
<script >
function submit() {
alert('aa');
}
</script>
</head>
<body>
<form action = "form.html" method = "POST">
Label<inpt type = "text"></br>
Label2<inpt type = "text"></br>
Label3<inpt type = "text"></br>
Label4<inpt type = "text"></br>
Label5<inpt type = "text"></br>
<button type="button" id="no" onclick="submit();">Submit</button>
</form>
</body>
</html>

Now I want to use function submit to show AlertDialog in android. This is my code:

final MyJavaScriptInterface myJavaScriptInterface
= new MyJavaScriptInterface(this);
formWebView.addJavascriptInterface(myJavaScriptInterface, "no");
formWebView.getSettings().setJavaScriptEnabled(true);
formWebView.loadUrl(url);
formWebView.getSettings().setBuiltInZoomControls(true);

and MyJavaScriptInterface:

public class MyJavaScriptInterface {
          Context mContext;

             MyJavaScriptInterface(Context c) {
                 mContext = c;
             }

             public void submitForm(){
                  AlertDialog.Builder myDialog
                  = new AlertDialog.Builder(AmmsFormsWebViewActivity.this);
                  myDialog.setTitle("DANGER2!");
                  myDialog.setMessage("You can do what you want2!");
                  myDialog.setPositiveButton("ON", null);
                  myDialog.show();
             }

         }

but doesn't work. Where I have a bug? I want to chceck if submit() method is use from webview I want to show dialog in android.

3
  • 1
    Are you getting any error ? If so, post the stack trace. Also see if javascript is enabled in your browser options . Commented Jun 13, 2013 at 13:32
  • use mcontext instead of AmmsFormsWebViewActivity.this in your MyJavaScriptInterface class file. like AlertDialog.Builder myDialog = new AlertDialog.Builder(mcontext); Commented Jun 13, 2013 at 13:33
  • I don't get any error. Everything is ok, but when I click on button I don't see Alert. Commented Jun 13, 2013 at 14:40

1 Answer 1

1

Since the "name" of your JS interface is no when you add the interface to your webview, and the method in your JS interface is submitForm(), the javascript should call no.submitForm(); for the interface to catch it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.