Converting the jQuery Mobile Calculator to PhoneGap

PhoneGap, now known as Apache Cordova, makes it amazingly easy to turn your jQuery Mobile web application into a mobile device application. So let me show you how to turn the JQM Calculator into an Android app. I am not going to cover how to set up the Android SDK and Eclipse, that is way too much work plus there are plenty of tutorials already covering that. Instead we will start by assuming that you have the Android SDK set up, have the JQM Calculator code available, and have downloaded the latest version of PhoneGap. By the time we are finished you will have a working Android application.

(In case you missed the post about creating a jQuery Mobile calculator, here’s the link:  jQuery Mobile Calculator )

Create an Android Project
We begin by a creating a normal Android project. Start up Eclipse and choose File > New > Android Project. Give your project a name. I chose PhoneGapCalculator for mine. Then choose your Android version. I chose Android 2.2. And finally set your package name. At this point you have a regular Android project nothing special here. 
PhoneGap-ize The Project

Now we start adding all of things necessary to PhoneGap-ize the project. Begin by adding a libs folder to the root of your project. Be careful not to add it to any existing folder. Then add a www folder to the existing assets folder. If for some reason the assets folder doesn’t exist, create it and then add the www folder to it.
If you haven’t already unzipped the PhoneGap zip file, please do so now. Find the android folder. It will be under the lib folder. You need to copy the cordova-1.7.0.jar file to your projects libs folder. Then the cordova-1.7.0.js file to the www folder. And finally we need to copy the entirety of the xml folder to the res folder. Remember to copy these files and folders and don’t move them. You will need them for other projects in the future. Your project should look similar to the image.

Fix the Build

Now we need to make the build process aware of the cordova jar file. To do that right click on the libs folder then choose: Build Path > Configure Build Path. Click on the Libraries tab, the click on Add JARs…. Then you need to find the cordova jar file and choose it. After clicking OK, you project should have a new item, Referenced Libraries. If you don’t see this item, don’t continue, something is wrong. You may need to press F5 in order to make Eclipse refresh.

Now we need to modify the generated Android source code to make it a PhoneGap app. Expand the src folder until you reach the java file, mine is named Near the top of the file below the import lines, add the following line:
import org.apache.cordova.*;

Change the line which reads:

public class PhoneGapCalculatorActivity extends Activity {

To read as follows:

public class PhoneGapCalculatorActivity extends DroidGap {

And finally replace the line:
With the following line:
Now lets copy our calculator files into the project. All of your CSS, HTML, and JavaScript files go into the www folder. So we simply copy the entire contents of the JQMCalculator project to the www folder. Please note: PhoneGap works with the project structure the way that it is, no need to flatten folders.

Once the files are copied, the app will actually run, but let’s be neat and fix an issue. The JQMCalculator project includes code to hide the URL bar on Android. In a PhoneGap app, that isn’t necessary. So we need to delete hideAddressBar.js file. Then remove all references to it in the code. In the index.html file delete the hideAddressBar.js  script tag.
Delete the script tag

Then replace the method RocknCoder.Events which looks like:

RocknCoder.Events = function () {
‘pagebeforecreate pagecreate pagebeforeload pagebeforeshow pageshow pagebeforechange pagechange pagebeforehide pagehide pageinit’,
“pageinit”, RocknCoder.hideAddressBar);
With the following:
RocknCoder.Events = function () {
‘pagebeforecreate pagecreate pagebeforeload pagebeforeshow pageshow pagebeforechange pagechange pagebeforehide pagehide pageinit’,
Delete the “.on…” code

Now if you run the application, you should get see the following:

That’s all it takes to turn your jQuery Mobile web app into an Android app using PhoneGap. 

Complete Source Code for PhoneGap Calculator


15 thoughts on “Converting the jQuery Mobile Calculator to PhoneGap

  1. When I run the application, eclipse throw 2 errors in file index.html: ” is not recognized!” and ” is not recognized!”. Could you please tell how can I solve this problems – set any value in Preferences, change elements to ordinary element div?

    Thank you


  2. Julian, thanks for the kind words. Have you already downloaded PhoneGap? I don't have a blackberry phone but it seems very straight forward to build the app for it. I have so far built the calculator for Android, iPhone, and Windows Phone 7. Only the Windows Phone has given me any trouble, but it doesn't seem to be PhoneGap's fault. There is some weirdness related to how WP7 handles touch events. I will explain in details in a later post. Please let me know how things work out with blackberry.



  3. Hi fun_dave,

    I am not sure if I understand your question. PHP is server side tech. Are you asking if a phonegap app can contact a PHP based server? If so the answer is absolutely YES it can.



  4. Thanks Troy …

    The Getting Started guide says that you must run the “create” command from the command line before creating any new project in Eclipse. Are you familiar with this, and wondering why you didn't mention it?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s