Introduction
This article explains how to create your own hybrid app. We will create a simple basic calculator using Cordova (Phone Gap) and jQuery Mobile.
Hybrid Apps
Hybrid Apps are like native apps that run on the device and are written with web technologies (HTML5, CSS and JavaScript). Hybrid apps run inside a native container and leverage the device's browser engine (but not the browser) to render the HTML and process the JavaScript locally. A web-to-native abstraction layer enables access to device capabilities that are not accessible in Mobile Web applications, such as the accelerometer, camera and local storage.
Cordova
Previously named PhoneGap, this is an open source platform for building native mobile applications using HTML, CSS and JavaScript.
jQuery Mobile
jQuery Mobile is a cross-platform mobile framework designed to simplify and enhance the development of mobile web applications by integrating HTML5, CSS3, jQuery and the jQuery UI into one framework that is not only robust, but maintainable and organized.
Procedure to install Cordova in your system:
- To create any application in Cordova first of all you must install Node.Js. Install Node.Js from here: NodeJs.
- After installing Node.Js you must install the git client. Install the git client from git client.
- After installing the git client open "Node.Js Command Prompt", and install Cordova using the following command:
Node: The code above is used to install Cordova in the Windows operating system, if you want to install Cordova in Mac OS X or Linux then use the following link:
Install Cordova
Step-by-step Create a Hybrid App using Cordova:
- To create a Hybrid App using Cordova open a Node.Js Command Prompt.
- Go to the specific drive and directory where you want to create the Hybrid App.
- Now use the following command to create a new Cordova Project:
"cordova create BasicCalculator com.sourabh.BasicCalculator BasicCalculator "
Here
- The first argument BasicCalculator specifies a directory to be generated for our project.
- The second argument com.sourabh.BasicCalculator provides your project with a reverse domain-style identifier. This argument is optional, but only if you also omit the third argument.
Why do we provide the second identifier like this?
Because Packages are typically named using the following convention:
[org/com].[company].[product].[component]
- The third argument BasicCalculator provides the application's display title.
- After running the above command a project folder will be created.
- Now go to the project directory using the following command:
cd BasicCalculator
- Before you can build the project, you need to specify a set of target platforms. Your ability to run these commands depend on whether your machine supports each SDK and whether you have already installed each SDK.
To add a different platform use the following command:
For Windows Phone 8:
For Windows:
- cordova platform add windows
For Amazon-FireOS:
- cordova platform add amazon-fireos
For BB10:
- cordova platform add blackberry10
For Android:
- cordova platform add android
For FireFoxOS:
- cordova platform add firefoxos
- Now open the project directory and go to the www folder and open index.html (../BasicCalculator/www/index.html) and also open ../BasicCalculator/www/css/index.css in any editor.
- Now modify index.css
- body {
- -webkit-touch-callout: none;
- -webkit-text-size-adjust: none;
- -webkit-user-select: none;
- }
- Now in index.html add jQuery Mobile Plugin:
- jquery.mobile-1.4.0.min.css
- jquery-1.11.0.min.js
- jquery.mobile-1.4.0.min.js
- Note: Download jQuery Mobile from here : jQuery Mobile Plugin
- jQuery Mobile provides a navigation framework that allows multiple screens to be embedded in a single HTML web page.
And a single web page containing the following 3 things:
- Header
- Main Page (Content)
- Footer
- Let's create the first page for the Main Menu:
- <div data-role="page" id="Home">
- <div data-role="header" data-position="fixed">
- <h1>Main Menu</h1>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <table width="100%">
- <tr>
- <td width="50%" style="text-align:center">
- <a href="#Calculator" data-transition="slide" style="text-decoration:none;color:#000000">
- <img src="img/Calculator.png" width="70px" height="70px"/><br/>
- Calculator
- </a>
- </td>
- <td width="50%" style="text-align:center">
- <a href="#About" data-transition="slide" style="text-decoration:none;color:#000000">
- <img src="img/About.png" width="70px" height="70px"/><br/>
- About
- </a>
- </td>
- </tr>
- </table>
- </div>
- </div>
- The second page Basic Calculator:
- <div data-role="page" id="Calculator">
- <div data-role="header" data-position="fixed">
- <h1>Calculator</h1>
- <a href="#Home" data-transition="slide" data-direction="reverse" data-icon="arrow-l" class="ui-btn-left">Back</a>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <label for="tb1">1st Number</label><input type="number" id="tb1" placeholder="Enter 1st Number"/>
- <label for="tb2">2nd Number</label><input type="number" id="tb2" placeholder="Enter 2nd Number"/>
- <label id="Answer" for="Answer"></label>
- <button id="Add" onclick="Add()"> ADD </button>
- <button id="SUB" onclick="Sub()"> SUB </button>
- <button id="MULTIPLICATION" onclick="Multi()"> MULTIPLICATION </button>
- <button id="DIVISION" onclick="Div()"> DIVISION </button>
- </div>
- </div>
- The third page About:
- <div data-role="page" id="About">
- <div data-role="header" data-position="fixed">
- <h1>About Calculator</h1>
- <a href="#Home" data-transition="slide" data-direction="reverse" data-icon="arrow-l" class="ui-btn-left">Back</a>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <h3>This calculator is designed by Sourabh Somani.<br/><br/>
- <address>
- [email protected]<br/>
- </address>
- </h3>
- </div>
- </div>
- Let's write JavaScript code for the Addition, Subtraction, Multiplication and Division:
- <script type="text/javascript">
- function Add(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Addition: "+(Number(number1)+Number(number2));
- }
- function Sub(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Subtraction: "+(Number(number1)-Number(number2));
- }
- function Multi(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Multiplication:"+(Number(number1)*Number(number2));
- }
- function Div(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Division:"+(Number(number1)/Number(number2));
- }
- </script>
- The complete code is as the following:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="format-detection" content="telephone=no" />
- <meta name="msapplication-tap-highlight" content="no" />
- <meta name="viewport" content="user-scalable=yes, initial-scale=1, maximum-scale=2, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
- <link rel="stylesheet" type="text/css" href="css/index.css" />
-
- <link rel="stylesheet" href="lib/jquery-mobile/css/jquery.mobile-1.4.0.min.css" />
- <script type="text/javascript" src="lib/jquery-mobile/js/jquery-1.11.0.min.js"></script>
- <script type="text/javascript" src="lib/jquery-mobile/js/jquery.mobile-1.4.0.min.js"></script>
- <script type="text/javascript" src="js/index.js"></script>
- <script type="text/javascript">
- function Add(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Addition: "+(Number(number1)+Number(number2));
- }
- function Sub(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Subtraction: "+(Number(number1)-Number(number2));
- }
- function Multi(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Multiplication:"+(Number(number1)*Number(number2));
- }
- function Div(){
- var number1=document.getElementById("tb1").value;
- var number2=document.getElementById("tb2").value;
- document.getElementById("Answer").innerHTML="Division:"+(Number(number1)/Number(number2));
- }
- </script>
- <title>Basic Calculator By Sourabh Somani</title>
- </head>
- <body>
- <div data-role="page" id="Home">
- <div data-role="header" data-position="fixed">
- <h1>Main Menu</h1>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <table width="100%">
- <tr>
- <td width="50%" style="text-align:center">
- <a href="#Calculator" data-transition="slide" style="text-decoration:none;color:#000000">
- <img src="img/Calculator.png" width="70px" height="70px"/><br/>
- Calculator
- </a>
- </td>
- <td width="50%" style="text-align:center">
- <a href="#About" data-transition="slide" style="text-decoration:none;color:#000000">
- <img src="img/About.png" width="70px" height="70px"/><br/>
- About
- </a>
- </td>
- </tr>
- </table>
- </div>
- </div>
-
- <div data-role="page" id="About">
- <div data-role="header" data-position="fixed">
- <h1>About Calculator</h1>
- <a href="#Home" data-transition="slide" data-direction="reverse" data-icon="arrow-l" class="ui-btn-left">Back</a>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <h3>This calculator is designed by Sourabh Somani.<br/><br/>
- <address>
- [email protected]<br/>
- </address>
- </h3>
- </div>
- </div>
-
- <div data-role="page" id="Calculator">
- <div data-role="header" data-position="fixed">
- <h1>Calculator</h1>
- <a href="#Home" data-transition="slide" data-direction="reverse" data-icon="arrow-l" class="ui-btn-left">Back</a>
- </div>
-
- <div role="main" class="ui-content" id="home-content">
- <label for="tb1">1st Number</label><input type="number" id="tb1" placeholder="Enter 1st Number"/>
- <label for="tb2">2nd Number</label><input type="number" id="tb2" placeholder="Enter 2nd Number"/>
- <label id="Answer" for="Answer"></label>
- <button id="Add" onclick="Add()"> ADD </button>
- <button id="SUB" onclick="Sub()"> SUB </button>
- <button id="MULTIPLICATION" onclick="Multi()"> MULTIPLICATION </button>
- <button id="DIVISION" onclick="Div()"> DIVISION </button>
- </div>
- </div>
-
- <script type="text/javascript" src="cordova.js"></script>
- </body>
- </html>
- Build Application
I am building this application for an Android phone only. To build the application use the following command and run this command in the Node.Js command prompt:
- C:\BasicCalculator>cordova build android
- To run this in an emulator use the following command:
- C:\BasicCalculator>cordova emulate android
Output
- Main Menu Page
- Calculator Page
- About Page