Professional Documents
Culture Documents
What Is Apache Cordova?: HTML CSS Javascript
What Is Apache Cordova?: HTML CSS Javascript
It allows software programmer to build Apps for mobile devices (Android, iOS, Windows,
Ubuntu OS etc) with HTML, CSS and Javascript.
Most important thing is - It is open source.
This means, with good coding skills in HTML, CSS and Javascript, one can build an App for
most of the mobile operating systems with help of Cordova, without learning different new
programming languages for various platforms.
Following are the platforms for which one can develop apps using Cordova:
Android
iOS
Windows
Blackberry
Firefox OS
Symbian
Tizen
Web OS
Ubuntu
Architecture of Cordova
Cordova provides us with APIs which can be used to access and stimulate native Mobile
OS features like Geolocation, Accelerometer, Camera, Storage, Notifications etc.
Web App is nothing but a simple webpage created using HTML, and styled using CSS along
with Javascript, jQuery etc for User Interface.
Cordova, lets us create an App, in which our Web App is initialised in a WebView (HTML
Rendering Engine).
Cordova Plugins are components that can be used to utilise the native OS features.
Advantages of Apache Cordova
Following are some of the major advantages to develop applications using Apache
Cordova:
If you go for native apps, then you will have to learn different languages for different
operating systems. For Example: Java for Android, Objective C or Swift for iOS, .NET for
Windows and so on. But with Apache Cordova, you can build app for multiple platforms
in one go, without learning new programming languages.
Development is fast, as one application developed in HTML, CSS and Javascript can be
easily transformed into apps supported by other platforms.
Best suited for early stage Startups, as product can be launched on multiple platforms at
once. And maintaining the app is super easy.
It's very quick to prototype.
As designing involves CSS, hence it becomes easier to produce great design easily,
without the trouble of native UI design for iOS, Android etc.
1. Search cmd in the Menu search bar by typing cmd or command prompt, then click on
the Command Prompt App option in search results.
2. We can also press Ctrl + R to open RUN dialog box, and type in cmd to open
Command Prompt.
3. It will open a black colored screen, this is the command prompt. Step 1 will open cmd in
normal mode.
4. To open command prompt in administrator mode, search cmd in searchbar and click
right button of mouse and choose option Run as administrator.
Following are some useful commands, used in day-to-day work:
o ipconfig → To get information about your IP address and DNS related info.
6. Press Command + Space to open Spotlight Search, and type terminal in the search
field.
7. And click on the Terminal - Utilities option to open the Terminal.
Tools for App Development
Now we will learn about the various tools which we will require for App development. We
have to download 5 different tools:
1. NodeJS
2. Cordova CLI
3. Apache Ant
4. Android SDK
5. Java JDK
NodeJS
NodeJS is a JavaScript runtime and is open source. We use NodeJS to execute backend
code written in Javascript. It is made on Chrome V8 javascript engine. It is lightweight and
very efficient.
NodeJS's package is npm. It is one of the largest ecosystem of open source libraries. We will
use npm to download Cordova, JS libraries, various plugins, packages etc.
Cordova CLI
The command line interface of cordova is used
to create, build, deploy and manage applications based on cordova. From creating project
to building the executable file, we will use command line interface in this tutorial.
Apache Ant
It's main objective is to build a Java application. It is a JavaScript library which is also run
using command line. Or you can say it is a build tool. Using Ant, we can create Ant build
files with all the details about how to compile, assemble, test and run a particular Java
applications.
Android SDK
It is a Software Development Kit(SDK) which includes all the necessary libraries required
to build Android App. So if we wish to develop Android Apps, we need this.
Java JDK
It is Java Develoment Kit (JDK) which is used to develop Java Applications. Again very
important.
As in this quick tutorial, we will be developing an Android Application using Cordova, hence,
the above mentioned tools. In the next tutorial, we will learn how to setup all these tools on
your computer/laptop.
Tools Installation
Now we start installation of tools on Windows OS, one by one.
Installing NodeJS
First of all we need to download and install NodeJS. You can download it from, here. Scroll
down and you will see two download option with title Download for Windows. Download
the most stable version of Nodejs. Stable version is prefixed with LTS
Once the download is finished, you need to install it. To install, simply double click on the
file just downloded. After installation, open cmd and type node -v. If it shows vx.y.z(x,y,z
are numbers), then you have succesfully installed it.
Next type npm -v. If it also shows x.y.z(x,y,z are numbers), then everything is installed
correctly.
If cmd is throwing error, while executing any of the above command, then check it in
the Node.JS Command Prompt.
If Nodejs cmd is also throwing error, then you have to remove NodeJS and install it once
again and follow the same process.
Installing Cordova
Type npm install -g cordova in cmd or node cmd. Once the installation is complete,
check it by typing cordova -v.
If it is showing version (x.y.z), then you have succesfully installed cordova.
Download X64 .exe jdk for 64 bit windows. Download X86 .exe jdk for 32 bit Windows
operating system.
If you don't know which version of Windows OS you have, just select Properties option
from the menu, after a right click on My PC or My Computer.
There you will see the version no. (16bit, 32bit, 64bit) of your computer/laptop, against the
label System type
After successful installation of JDK. Next step is to add it to our system's path variable.
Click on the button Environment variables, available at the bottom of the dialog box.
Now add a system variable and name it JAVA_HOME, and add another system variable
with the name as, _JAVA_OPTIONS.
To understand the values that we have to assign to the system
varibales JAVA_HOME and _JAVA_OPTIONS, please check the addresses specified
below:
Address of Java JDK will be c:\Program Files\Java\jdk_(x.y.z_k). Lets assume this to
be address 1
Address of 'bin' will be c:\Program Files\Java\jdk_(x.y.z_k)\bin. Lets assume this to
be address 2
Address of JRE 'bin' will be c:\Program Files\Java\jre_(x.y.z_k)\bin. Lets assume this to
be address 2a
Now add address 1 as the varibale value for variable name, JAVA_HOME. And in the
System Variable box, there is a variable with name, path. Add address 2 and address
2a as the variable value for path variable by separating the values with semicolon ;. For
Example: address 2;address 2a
Next add address 2 and address 2a as the variable values for the User
Variable PATH (User variable panel is just above the System Variable).
And add -Xmx512M, as the varibale value for System variable _JAVA_OPTIONS.
Save all these changes and restart cmd then type javac -version. If it shows the version,
then you have successfully installed Java.
And in path and PATH add address 3a, to the exisiing values, by using a smeicolon ;
After saving all the changes, restart cmd and type ant -v. If it displays the same version
number as we downloaded, then you have installed apache ant.
Here we have the complete directory structure of Android SDK, out of which 2 directories
are important for us, they are:
C:\Users\[User_name]\AppData\Local\Android\android-
sdk\tools and C:\Users\[User_name]\AppData\Local\Android\android-sdk\platform-
tools"
Now create a new System Variable ANDROID_HOME and add only the Android SDK
directory as the variable value.
And in path variable, add platform tools directory and tools directory of Android SDK.
Now save all the changes, restart cmd and type android.
A new screen will pop up, congrates! you have successfully installed Android SDK.
In the new window, check (tick mark) the checkboxes to download only Android SDK tools,
Android SDK platform-tools, SDK build tool in tool panel and download SDK platform etc.
From next lesson we will start with development of App.
HTML
Hyper Text Markup Language is used to create the DOM structure for any webpage. It's
main motive is to show content on the screen when opened in a broswer.
HTML basic structure:
<html>
<head>
<title> </title>
</head>
<body>
</body>
</html>
CSS
Cascading Style Sheets is used to style the HTML content to make it look good.
CSS basic structure:
Suppose we have to provide a background color to the content of body.
<style type="text/css">
body{
background-color: blue;
</style>
<style>
//CSS Code
</style>
</head>
Or we can write the CSS code in another file and link it to our HTML file.
To do this, name a file style.css and write the below sepcified CSS code in this file:
body{
background-color: blue;
p{
background-color: red;
Now to add this CSS file to our HTML code, do the following:
index.html
<head>
</head>
NOTE: While writing CSS code in style.css file, don't include <style> tag now.
JS, or Javascript
We use Javascript to dynamically operate on the content, present in the HTML webpage.
Suppose we have to inject a string, say I am learning using Studytonight to the body of
our HTML webpage, dynamically.
<script type="text/javascript">
</script>
We can either put the javascript code inside the script tag, which is place in the head tag.
<head>
<script>
//Javascript code
</script>
</head>
Or, we can write JS code separately in another file and add it to our pre-existing HTML file.
For example: Write JS code in index.js
index.js
document.getElementByTagName('body').innerHTML="I am learning using Studytonight";
</head>
</head>
<head>
</head>
<body>
</body>
</html>
NOTE: One must have knowledge of all these above concepts to use Cordova for App
development.
1. Open cmd.
2. Go to the location where you want to create your first application. Suppose you want to
create your first app on desktop. So type the following code in command prompt: cd
desktop, if you are here "C:user\system_name\". The command cd desktop will take
project_name → This will be your project name, like myFirstApp. After executing 3rd
step, a folder will be created with the specified name on the desktop. You may check
there.
4. Type cd [project_name] and press enter, to get inside the project directory.
5. Now we will specify the name of the platform for which we want to create our mobile
application, for example we can inform cordova, that we want to create android
application by using the command, cordova platform add android. It will add android
platform. You can see it in platforms folder, inside the project folder.
6. Type cordova build android. This command will generate the apk.
For first time, Step 5 or Step 6 will take time, because the gradle file will be downloaded.
<head>
</head>
<body onload="ask()">
<div id='answer'></div>
</body>
</html>
margin-top:100px;
text-align:center;
color:red;
#question:hover {
color:geen;
#answer {
margin-top:30px;
text-align:center;
color:yellow;
if(k.length > 0) {
document.getElementById('answer').innerHTML = "";
document.getElementById('answer').innerHTML = k;
else {
// If length of string is 0, then it will again ask you the same question.
ask();
Now open project_name folder, go to www folder, delete all the pre-existing files and place
all the three file (index.html, index.js and style.css). Then follow the steps 5 & 6 again.
Whenever the app is opened, it gets redirected to this page index.html. You can edit it
and you can change it to anything like firstpage.html, just by modifying the
configuration in the config.xml file.
1. Create a new project. You know how to do this, we just did this in the previous lesson.
2. Now go to Google's Developer Console → https://developers.google.com/maps
3. Scroll down and click on Google Map Javascript API
4. Click on GET A KEY.
5. Now click on Create Project and then name your project and enable API.
index.html
<html>
<head>
<style type="text/css">
#map_area {
position:fixed;
height:100%;
width:100%;
top:0;
left:0;
</style>
<script type="text/javascript">
function start() {
zoom: 17,
center: latlong
});
position: latlong,
map: map
});
</script>
</head>
<body>
<div id="map_area"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=start"><script
>
</body>
</html>
8. Place the above code in your index.html file, and put the file inside the www folder.
9. Pass the command to generate the apk file.
10. Transfer this apk to your phone and test it.
Cordova Plugin
Plugina allow applications developed with Cordova to access the device's functionality.
Plugins are written in JavaScript and native libraries. All the cordova main features are
implemented as plugins. If we don't use plugin then we can't use any of the native features.
Types of Plugins
There are many types of plugin, some are:
1. Create project.
2. Add platform → android
3. Add geolocation plugin - Type cordova plugin add cordova-plugin-geolcation and
press enter.
4. Now write the following code in your index.html file and put it in www directory in the
project folder.
5. <html>
6. <head>
7. <style type="text/css">
8. #map_area {
9. position:fixed;
10. height:100%;
11. width:100%;
12. top:0;
13. left:0;
14. }
15. </style>
18. if (navigator.geolocation) {
22. }
23. navigator.geolocation.getCurrentPosition(showPosition);
24. }
25. else {
27. }
28. }
29.
30. function showPosition(position) {
33.
35. }
36.
42. });
46. });
47. }
48. </script>
49. </head>
50. <body>
53.
54. </body>
55. </html>
After the script loads successfully, it will call geo Location function. This function
requests the device to grant the required permission. If the device grants permission, it
will pass the latitude and longitude to start function. And your device location will be
shown on the map.
Introduction to AJAX
AJAX stands for Asynchronous Javascript And XML. It is a client-side script used in
Javascript to communicate with server to exchange data or anything without refreshing the
page.
Let us suppose we want to make a dynamic cordova based application where user needs to
exchange data with server, say login credential verification, so we can use AJAX to send
the data to the server and through the same gateway server, send the result back to the
user.
Ajax basic code: We will use jquery.
$.ajax is a jQuery function to make http POST, GET etc request to the server and receive
results without the page being refreshed or reloaded.
$.ajax({
success:function(data) {
})
NOTE: Covering Ajax and jQuery is not in the scope of this tutorial. But we strongly advice
you to learn abour Ajax, as it is very useful if you want to create dynamic Apps.
2. Now write some code in Php and place it in the htdocs folder inside the XAMPP folder.
index.php
<?php
echo “hello world!”;
3. Open the browser and type in the address bar: http://localhost or http://127.0.0.1, it
should show hello world! on the screen.
This means our XAMPP server is running successfully. Now we are ready to develop a
dynamic mobile app. Client-side script (HTML, CSS, Javascript and jQuery, just like in
previous tutorials). Keep the jquery.js file with index.html (or both in same folder)
6. <head>
8. <script type=”text/javascript”>
9. $(document).ready(function(){
10. $(“#sub”).click(function(){
14. $.ajax({
18. cache:false,
19. success:function(data) {
20. alert(data);
21. }
22. })
23. }
24. else {
26. }
27. })
28. })
29. </script>
30. </head>
31. <body>
35. </body>
36. </html>
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
?>
Now place this PHP file and the HTML file in the htdocs folder. And make sure your
XAMPP server is running. Run the index.html in browser and feed input and check
output.
2. Click on SQL
3. Type the following command in the textarea and click on Go.
CREATE DATABASE mydb;
This command will create database mydb if it does not exist already.
4. Now open the mydb database, by clicking on it in the sidebar and once again click on
SQL. Then create table using the following command.
5. CREATE TABLE my_table (
6. id int AUTO_INCREMENT,
7. firstname varchar(200),
8. lastname varchar(200)
9. )
It will create a table with name, my_table in mydb database.
10. Client side code i.e. index.html file, will remain the same but the Php side code will
change.
firstname.php
<?php
/*
*/
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$result = mysqli_query($con,$sql);
if($result) {
else {
?>
Now try it or refresh phpmyadmin page and check whether the data is inserted or not.