0

I've been looking about for a proper explanation about javascript's relationship with the android WebView. Can anyone tell me: if I an HTML page online and all the required javascript files to make what I need work on that HTML page, can I port it to android.

For example could I move the javascript files onto the android app local folder ( if so: which folder? ) and call a function from in the app. Then could I get the result of the javascript function from one of those files?

If anyone has any tutorials or resources related to this could you point me in the right direction?

1 Answer 1

1

When you have a WebView you can set a Webchormeclient or a Webviewclient, the webchromeclient will be similar to your chrome desktop browser. The other option is to use a Webviewclient this webview client can be configurated as you wish, you can add an "interface" to call Javascript functions from your java code, or java functions from your Javascript code. The interface is a class and can be something like this: final class JavaScriptInterface {

        public void myalert(String message){
            new AlertDialog.Builder(wview.getContext()).setMessage(message).setNeutralButton("Ok",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            }).show();

        }

The webview has a method to "attach" this class to the Javascript:

wview.addJavascriptInterface(new JavaScriptInterface(), "interface);

Once you have add the JavascriptInterface, you can call the method myalert("hi") from the Javascript code, to do so, you will have to do something like:

window.interface.myalert("hi") //This is called in the Javascript

if you want to call a Javascript from the Java code you can do something like this:

wview.loadUrl("javascript:changeValues('"+name+"','"+id+"')");//this will call a Javascript function called changeValues, and will pass to variables (name, id)

To sum up, the webchromeclient is easier to use but the webviewclient allows you more configuration. Before, maybe you want to read about phonegap which is a framework to devlop Webapps x-platform. Hope you that this fast explanation helps you a little.

PS: With the second option you can store all that you need in the sdcard.

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

5 Comments

Cheers for the reply but where would the javascript files have to be placed? And say if I had a function called 'myAlert' in a javascript file called alerts.js would I need to supply the file name or does android just read over them all?
You can store the Javascript files in the sdcard (you will be able to modify the javascript file), in the asset_folder (you will be able to use it but not to modify it), or store it in the server as a normal page, the script will only load if you have internet connection.
If I'm not wrong when the browser loads the page, it also loads all the javascript, so you wont have to refer to the file, but im not sure...
Many thanks for the fast reply mate - one final question - do you have any links that can explain this a bit more? :D

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.