Introduction
PhoneGap is an open source framework for quickly building cross-platform mobile apps using HTML5, JavaScript and CSS.
PhoneGap is an application container technology that allows you to create natively-installed applications for mobile devices using HTML, CSS and JavaScript. The core engine for PhoneGap is also 100% open source, under the Apache Cordova project.
The UI layer of a PhoneGap application is a web browser view that takes up 100% of the device width and 100% of the device height.
PhoneGap provides an application programming interface (API) that enables you to access native operating system functionality using JavaScript. You build your application logic using JavaScript and the PhoneGap API handles communication with the native operating system.
PhoneGap currently supports:
- iOS
- Android
- Wndows Phone 7
- Windows Phone 8
- BlackBerry
- WebOS
- Symbian
- Bada
Did you see that PhoneGap supports 7 different platforms? Interestingly Windows Phone 8 is the seventh (newest ) one to be included.
As per the
PhoneGap website, the following features are supported in PhoneGap for
Windows Phone 8.
- Accelerometer
- Camera
- Com
- Contacts
- File
- Geolocation
- Media
- Network
- Notification (Alert)
- Notification (Sound)
- Notification (Vibration)
- Storage
Setting up PhoneGap
If you are a Windows Phone Developer and wish to develop Apps using HTML5, CSS and JavaScript, then you can use PhoneGap and develop Windows Phone Apps.
Data Source:
PhoneGap Explained Visually At first you need to download the PhoneGap project from this link. I have used PhoneGap 2.8.0 in this tutorial,
Install PhoneGap.Now unzip the downloaded PhoneGap package, then open the lib folder and then open the windows-phone-8 folder. Now, you will see a Zip file named CordovaWP8_2_8_0.
Firgure 1
Copy this file and then go to the My Documents \ Visual Studio 2013 \ Templates \ ProjectTemplates folder.
And paste CordovaWP8_2_8_0 here in this folder.
Figure 2
Creating a Project
Now, open your Visual Studio IDE and click on New Project.
In the New Project panel, you will see the CordoWP8_2_8_0 template and then create a project with your desired name.
Figure 3
Now In the Solution Explorer, you will see a WWW folder. Click on this folder and then you will see, a CSS folder, an img folder, a JavaScript folder and an index.html page. You will keep your CSS, JavaScript pages and images in these CSS, JavaScript and img folders.
Now click on the index.html page and then you will see a design like this.
Figure 4
Now you can directly run this cross-platform app on your Windows Phone 8 devices. If you deploy it on a real device then you will see a screen like this, in other words, your project is now ready.
Cheers!!!
Figure 5
Now select index.html and right-click on the file then select view code.
This is the code of the index.html file and this is what we see when we run this project on an emulator or real device.
Then I did some minor modification to the code. I have deleted the image and texts and added a h1 tag and wrote, Hello Windows Phone Developers. Now see what happens.
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <meta name="format-detection" content="telephone=no" />
- <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
- <title>Hello World</title>
- </head>
- <body>
- <h1>Hello Windows Phone Developers</h1>
- <script type="text/javascript" src="cordova.js"></script>
- <script type="text/javascript" src="js/index.js"></script>
- <script type="text/javascript">
- app.initialize();
- </script>
- </body>
- </html>
Listing 1
Figure 6
Now, we can see the Hello Windows Phone Developers text in our project main screen.
jQuery Mobile Theme
- jQuery Mobile Theme for Windows Phone 8.
- Now we will add jQuery Mobile Theme to our Windows Phone 8 project.
- We can add a jQuery mobile theme in two ways.
- One, we can install JQMThemeForWindowsPhone8 Package using the Package Manager Console (PMC). Two, we can directly add necessary references to our HTML file.
- To use the Package Manager Console, at first we need to click Tools then the Library Package Manager and then Package Manager Console.
- The Package Manager Console will appear at the bottom of Visual Studio.
- Then we will write:
- Install-Package JQMThemeForWindowsPhone8 -version 0.0.1
- In the Package Manager Console and press the Enter button.
Figure 7
Then PCM will install the JQM Theme for Windows Phone 8 successfully and we will see many JavaScript files, CSS files and pages have been added to our project (look at the Solution Explorer at the right).
Now if we rename our index.html file to any other name and rename themeStartPage.html file to index.html file and run this project then we will see the stunning look of the jQuery Mobile Theme in our Windows Phone 8 project. Here, you will see multiple jQuery mobile controls, backgrounds and transitions. I have selected the Dark theme and below you can see some screen shots of the Dark Theme.
Figure 8
Way, two, directly add references of the jQuery Mobile theme in your index.html file.
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
- <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
Listing 2
Using a ListView Control
Now, I have added a list view control code in my index.html file and you can see how it looks like now in the following images. Here, I have used the jQuery Mobile light theme.
Figure 9
The full code of the Index.html file is given below.
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <meta name="format-detection" content="telephone=no" />
- <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
- <title>Functions</title>
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
- <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
- <script type="text/javascript" charset="utf-8">
-
- function checkLanguage() {
- navigator.globalization.getPreferredLanguage(
- function (language) { alert('language: ' + language.value + '\n'); },
- function () { alert('Error getting language\n'); }
- );
- }
- </script>
- </head>
- <body>
-
- <div data-role="page" id="pageone">
- <div data-role="header">
- <h1>List</h1>
- </div>
- <div data-role="main" class="ui-content">
- <ul data-role="listview" data-autodividers="true" data-inset="true">
- <li><a href="#pagetwo" data-transition="flip">Adele</a></li>
- <li><a href="#">Agnes</a></li>
- <li><a href="#">Albert</a></li>
- <li><a href="#">Billy</a></li>
- <li><a href="#">Bob</a></li>
- <li><a href="#">Calvin</a></li>
- <li><a href="#">Cameron</a></li>
- <li><a href="#">Chloe</a></li>
- <li><a href="#">Christina</a></li>
- <li><a href="#">Diana</a></li>
- <li><a href="#">Gabriel</a></li>
- <li><a href="#">Glen</a></li>
- <li><a href="#">Ralph</a></li>
- <li><a href="#">Valarie</a></li>
- </ul>
- <p>The data-autodividers="true" attribute creates dividers where it is appropriate with uppercased first letters of the item's text.</p>
- </div>
- <div data-role="footer">
- <h1>Footer Text</h1>
- </div>
- </div>
- <div data-role="page" id="pagetwo">
- <div data-role="header">
- <h1>Welcome To My Homepage</h1>
- </div>
-
- <div data-role="main" class="ui-content">
- <p>Click on the link to go back. <b>Note</b>: fade is default.</p>
- <a href="#pageone">Go back to Page One</a>
- </div>
- <div data-role="footer">
- <h1>Footer Text</h1>
- </div>
- </div>
- <script type="text/javascript" src="cordova.js"></script>
- <script type="text/javascript" src="js/index.js"></script>
- <script type="text/javascript">
- app.initialize();
- </script>
- </body>
- </html>
Listing 3
Summary
I hope you've understoond how to get started with PhoneGap and create hybrid apps. I hope to see you soon. Happy coding.
Source of this jQuery Mobile List View code:
jQuery Mobile List Views.