Is it possible to use JavaScript in Android?? if so, how? Please provide some examples.
Thanks.
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.
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.
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.
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.
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
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With