- Hello, World!
- PhoneGap Initialization
- Leveraging PhoneGap APIs
- Enhancing the User Interface of a PhoneGap Application
- Testing and Debugging PhoneGap Applications
- Dealing with Cross-Platform Development Issues
Enhancing the User Interface of a PhoneGap Application
As you can see from the application examples highlighted so far, the PhoneGap framework doesn’t do anything to enhance the UI of a PhoneGap application. The framework provides access to device-specific features and applications and leaves it up to developers to theme their applications however they see fit. Web developers should use the capabilities provided by HTML, CSS, and even Java-Script to enhance the UI of their PhoneGap applications as needed; we’re not going to cover mobile web UI design here.
As Android and iOS-based smartphones became more popular, web developers found themselves needing to be able to build web applications that mimic the look and feel of native applications on these mobile platforms. To accommodate this need, many open source and commercial JavaScript mobile frameworks were created to simplify this task such as jQuery Mobile (www.jquerymobile.com), Dojo Mobile (www.dojotoolkit.org/features/mobile), and Sencha Touch (www.sencha.com/products/touch).
Although not directly related to PhoneGap development, the use of these frameworks is very common for PhoneGap applications, so it’s useful to highlight them here. In this section, we’ll discuss how to enhance the UI of a PhoneGap application using jQuery Mobile (jQM), an offshoot of the popular jQuery project. The jQuery and jQM libraries work together to provide some pretty useful UI elements and theming for any mobile web application.
In the following HelloWorld4 application, we’ll take the HelloWorld3 application and apply an enhanced UI using the jQuery Mobile framework.
HelloWorld4 Application
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" id="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" /> <link rel="stylesheet" href="jquery.mobile-1.0b3.css" /> <script type="text/javascript" charset="utf-8" src="jquery-1.6.4.js"></script> <script type="text/javascript" charset="utf-8" src="jquery.mobile-1.0b3.js"></script> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> function onBodyLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { //Get the appInfo DOM element var element = document.getElementById('appInfo'); //replace it with specific information about the device //running the application element.innerHTML = 'PhoneGap (version ' + device.phonegap + ')<br />' + device.platform + ' ' + device.name + ' (version ' + device.version + ' ) .; ' } </script> </head> <body onload="onBodyLoad()"> <div data-role="page"> <div data-role="header" data-position="fixed"> <h1>HelloWorld4</h1> </div> <div data-role="content"> <p>This is a PhoneGap application that makes calls to the PhoneGap APIs and uses the jQuery Mobile framework.</p> <p id="appInfo">Waiting for PhoneGap Initialization to complete</p> </div> <div data-role="footer" data-position="fixed"> <h1>PhoneGap Essentials</h1> </div> </div> </body> </html>
Figure 2-4 shows the application running on the Android simulator.
Figure 2-4 HelloWorld4 application running on an Android emulator
Notice that the device.platform call is working correctly on the Android emulator; in Figure 2-4, it lists “google_sdk” as the platform for the emulator.
Notice how jQM has a problem rendering the “PhoneGap Essentials” text in the footer. Just so you can see how this looks on a different mobile platform, Figure 2-5 shows the exact same web content running within a PhoneGap application on the BlackBerry Torch simulator. This isn’t an issue with PhoneGap but instead is an issue related to available screen width and how jQM renders content leaving space on the left and right for buttons (which aren’t used in this example).
Figure 2-5 HelloWorld4 application running on a BlackBerry simulator
In this version of the application, some additional resources have been added to the page’s header:
<link rel="stylesheet" href="jquery.mobile-1.0b3.css" /> <script type="text/javascript" charset="utf-8" src="jquery-1.6.4.js"></script> <script type="text/javascript" charset="utf-8" src="jquery.mobile-1.0b3.js"></script>
The first line points to a CSS file provided by the jQM framework. It contains the style information used to render the iPhone-ish UI shown in the figure. Next come references to the jQuery and jQuery Mobile JavaScript libraries that are used to provide the customized UI plus add additional capabilities to the application. The files referenced in the example application are the full versions of the CSS and JavaScript files. These files are used during testing of the application and should be replaced with the min versions of the files, as shown in the following code snippet, before rolling the application into production.
<link rel="stylesheet" href="jquery.mobile-1.0b3.min.css" /> <script type="text/javascript" charset="utf-8" src="jquery-1.6.4.min.js"></script> <script type="text/javascript" charset="utf-8" src="jquery.mobile-1.0b3.min.js"></script>
The min versions are compressed so comments, white space, line breaks, and so on, are removed from the files. This allows the files to take up less space within the packaged application, helping reduce the overall file size for the application, and allows these resources to load more quickly when the user launches the application.
The body of the HTML page has been updated to include several HTML <div> tags wrapped around the content for the application. These <div>s include a data-role attribute that is used by jQM to define specific areas of the content page that are then styled appropriately depending on which role is assigned.
In Figure 2-5, the content in the section of the page given the header data-role is styled with a gradient background and forced to the top of the page by the data-position=”fixed” attribute. Similarly, the content in the section of the page given the footer data-role is styled with a gradient background and forced to the bottom of the page by the data-position=”fixed” attribute. The page content defined within the data-role=”content” <div> will be rendered between the header and footer, with the middle section scrollable as needed to display all of the content within the section.
These examples only lightly cover the capabilities of jQM; there’s so much more you can do with this framework to enhance the user experience within your PhoneGap applications. Refer to the jQM online documentation or several of the new books on jQM for additional information about the capabilities provided by the framework.