5

I am currently designing a native android application. I am planning to use jQuery Mobile web view as my interface and do all the calculations with java back-end. (still deciding using phonegap or not)

I have some difficulties implementing a page that allows a user to fill in a form and pass the variable to the android java part.

Researching all morning, I have learned how to interact between javascript/html and java with addJavascriptInterface(). But the only thing I can find that answer my question is with JSON. That seems a little complicated. Is there a way I can pass the variable as a parameter of a java function?

(I learned that if I do not use a web view, I can simply use getText() or getSelectedItem() with default UI to do what I want to)

I apologize there is no code avaliable, since this is still in designing stage, and I am a little new to android sdk.

Thanks

3
  • What data do you need to pass into the javascript interface? How many fields on the form and what type? You could simply create a delimited string and call a function on the interface. Commented Jan 22, 2012 at 13:42
  • looks like its going to be around 5 user setting fields. By that does it mean that when the submit button is clicked simply make a javascript function save those input values into variables and call a java function? Commented Jan 22, 2012 at 13:49
  • See my example answer. Only just started playing with this stuff myself but I think you can pass multiple parameters into the JavaScriptInterface function. Commented Jan 22, 2012 at 13:55

1 Answer 1

8

OK, here's an example of interacting with the javascript interface...

Setting the javascript interface in your Activity...

JavascriptInterface javasriptInterface = new JavascriptInterface(this);
webview.addJavascriptInterface(javasriptInterface, "Android");

The inner JavascriptInterface class in your Android Activity...

public class JavascriptInterface {
    Context mContext;

    JavascriptInterface(Context c) {
        mContext = c;
    }

    public boolean doSomething(String name, String address) {
        ...
        return true;
    }
}

EDIT: Your form will have various input fields. Example...

<form name="myForm" ...>
    <input type=text name=personName>
    <input type=text name=personAddress>
    <input type="button" value="Do it" onClick="callDoSomething()" />
</form>

<script type="text/javascript">
    function callDoSomething() {
        var theName = document.myForm.personName.value;
        var theAddress = document.myForm.personAddress.value;
        var result = Android.doSomething(theName, theAddress);
    }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I have actually seen it elsewhere and I understand this part. What I don't understand thou is how should I pass the input values into the Android.showTast? (i.e. how do i get the form input values into "message" ?( thanks
See my edit at the end of my answer and also that I've added the doSomething(...) function to the JavascriptInterface class further up. As I said I'm fairly new to doing this stuff but it hopefully will give you a start.

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.