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.
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.
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:
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.