Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript in Android

Is it possible to use JavaScript in Android?? if so, how? Please provide some examples.

Thanks.

like image 589
Piyush Avatar asked Nov 18 '10 04:11

Piyush


People also ask

Can you use JavaScript on Android?

Javascript works on android by default, but if you have turned it off then you can follow these instructions to enable javascript on android phones. This also applies to javascript enabled browsers on javascript-enabled devices in general.

Why JavaScript is used in Android?

The JavaScript can alter and change the CSS and HTML as well as validate, manipulate and calculate data. Meteor is the platform to build mobile and web apps by using a JavaScript codebase. Creating new mobile apps with JavaScript is quite easy and it may suitable for all platforms such as Android and IOS.

Do I need JavaScript on my Android?

Without JavaScript, you can't do things like log in to a site or fill out a "contact us" form. For example, you can't access Gmail's Web browser client when JavaScript is disabled. By enabling JavaScript, it's possible that pages with JavaScript programming errors can cause Browser or Chrome to crash.

Where is JavaScript located on Android?

Look to the right of the address bar and click the icon with 3 stacked dots. Select Settings from the drop-down menu. Select Site settings under the advanced heading. Select the JavaScript menu item.


3 Answers

I am way late to the party here, but I had this exact need. iOS 7 now includes JavaScriptCore natively and it is really easy to use (despite limited documentation). The problem is that I didn't want to use it unless I could also use something similar on Android. So I created the AndroidJSCore project. It allows you to use your JavaScript code natively in Android without requiring a bulky WebView and injection. You can also seamlessly make asynchronous calls between Java and Javascript.

Update 27 Mar 17: AndroidJSCore has been deprecated in favor of LiquidCore. LiquidCore is based on V8 rather than JavascriptCore, but works essentially same. See the documentation on using LiquidCore as a raw Javascript engine.

From the documentation:

... to get started, you need to create a JavaScript JSContext. The execution of JS code occurs within this context, and separate contexts are isolated virtual machines which do not interact with each other.

JSContext context = new JSContext();

This context is itself a JavaScript object. And as such, you can get and set its properties. Since this is the global JavaScript object, these properties will be in the top-level context for all subsequent code in the environment.

context.property("a", 5);
JSValue aValue = context.property("a");
double a = aValue.toNumber();
DecimalFormat df = new DecimalFormat(".#");
System.out.println(df.format(a)); // 5.0

You can also run JavaScript code in the context:

context.evaluateScript("a = 10");
JSValue newAValue = context.property("a");
System.out.println(df.format(newAValue.toNumber())); // 10.0
String script =
    "function factorial(x) { var f = 1; for(; x > 1; x--) f *= x; return f; }\n" +
    "var fact_a = factorial(a);\n";
context.evaluateScript(script);
JSValue fact_a = context.property("fact_a");
System.out.println(df.format(fact_a.toNumber())); // 3628800.0

You can also write functions in Java, but expose them to JavaScript:

JSFunction factorial = new JSFunction(context,"factorial") {
    public Integer factorial(Integer x) {
        int factorial = 1;
        for (; x > 1; x--) {
            factorial *= x;
        }
        return factorial;
    }
};

This creates a JavaScript function that will call the Java method factorial when called from JavaScript. It can then be passed to the JavaScript VM:

context.property("factorial", factorial);
context.evaluateScript("var f = factorial(10);")
JSValue f = context.property("f");
System.out.println(df.format(f.toNumber())); // 3628800.0
like image 139
Eric Lange Avatar answered Oct 20 '22 15:10

Eric Lange


Do you mean something like making a native app using Javascript? I know there are tools like Titanium Mobile that let you make native apps using web tools/languages.

You could also make Web Apps. There are loads of resources and tutorials out there for that. Just search "Android Web App tutorial" or something similar.

like image 43
Joel Avatar answered Oct 20 '22 15:10

Joel


Yes you can, just create a wrap up code that points to html page and includes your javascript and css.

There are different libraries that can help you with this:

  • http://www.phonegap.com/
  • http://www.sencha.com/products/touch/
  • http://jquerymobile.com/
like image 1
Alex Djioev Avatar answered Oct 20 '22 16:10

Alex Djioev