Saturday, 5 December 2020

How to create Making first PhoneGap App?

In our previous section, we learned how we can create a new PhoneGap project and how we can preview it either in a desktop browser or on a mobile app. In this section, we will learn about how we can create our first PhoneGap app. These are the following steps used to create a PhoneGap App:

1) Create a new PhoneGap project

In our first step, we will create a new PhoneGap project. We will name it My First App and will give a unique ID also, i.e., com.javatpoint.My_First_App in my case.

Making first PhoneGap App
Making first PhoneGap App

When we run this on the desktop browser or on the PhoneGap Developer app, we will see a blank screen.

Making first PhoneGap App

2) Open index.html file

After creating a PhoneGap project, we will go to the directory where we saved this project. We will open the folder of our project that contains many other folders like hooks, platforms, plugins, and www. We will open the www folder that contains the index.html file. We will open this file in our preferred editor to code.

Making first PhoneGap App
Making first PhoneGap App
Making first PhoneGap App
Making first PhoneGap App

3) Start Coding

Now, we will update this file and start our coding.

Note: We are creating a dog age calculator. You can create your own code or can copy the following code into your html file.

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.          // adding title  
  7.         <title>Dog Years Calculator</title>  
  8.         <script>  
  9.          //creating window function to initialize the application  
  10.         window.onload=function()  
  11.         {  
  12.             document.getElementById('btnCalc').addEventListener('click', calcAge);  
  13.         }  
  14.         //creating calcAge function to calculate the age of dog  
  15.         function calcAge(e)  
  16.         {  
  17.     //fetching age  
  18.             var age = document.getElementById('dogAge').value;  
  19.             ageage = age *7;  
  20.             var result = "In dog years, your dog is: " + age;  
  21.           //  console.log(result);  
  22.          //alert(result);  
  23.             document.getElementById('result').innerHTML = result;  
  24.         }  
  25.         </script>  
  26.     </head>  
  27.     <body>  
  28.     <script type="text/javascript" src="cordova.js"></script>  
  29.     //add heading  
  30. <h1>Dog Years Calculator</h1>  
  31.     //add lable  
  32. <label for="dogAge">Dog Age in Human Years:</label>  
  33.     //add input field  
  34. <input id="dogAge" type="number" />  
  35.     //add button  
  36. <button id="btnCalc">Calculate</button>  
  37.             //add a field for result   
  38. <div id="result"></div>  
  39.     </body>  
  40. </html>  

We will save this file and go to the PhoneGap Developer App. We will connect Desktop Application with PhoneGap Developer App and run applications on the server.

Desktop Browser

Making first PhoneGap App

PhoneGap Developer App

Making first PhoneGap App Making first PhoneGap App Making first PhoneGap App Making first PhoneGap App

No comments:

Post a Comment