Sliding Home

We are going to build a simple calorie-tracking application called Kilo that allows the user to add and delete food entries for a given date. All told, there will be five panels: Home, Settings, Dates, Date, and New Entry. We'll start off with two panels and work our way up as we go.

We will be assigning CSS classes to some of the HTML elements (e.g., toolbar, edgetoedge, arrow, button, back). In every case, these classes correspond to predefined CSS class selectors that exist in the default jQTouch theme. Bear in mind that you can create and use your own classes by modifying existing jQTouch themes or creating your own from scratch; we're just using the defaults in the examples here.

We're going to start from scratch here, so you can put aside the files you created in the preceding chapters. To begin, let's create a file named index.html and add the HTML given in Example 4-1 for the Home and About panels.

Example 4-1. HTML for the Home and About panels in index.html

<title>Kilo</title> </head> <body>

<div class="toolbar">

<ul class="edgetoedge">

<li class="arrow"><a href="#about">About</a></li> </ul> </div>

<div class="toolbar"> <h1>About</h1>

<a class="button back" href="#">Back</a> </div> <div>

<p>Kilo gives you easy access to your food diary.</p> </div> </div> </body> </html>

The HTML here basically amounts to a head with a title and a body with two children, both divs:

O This div (as well as the about div that appears a few lines down) will become a panel in the application by virtue of the fact that they are direct descendants of the body.

© Inside each panel div, there is a div with a class of toolbar. This toolbar class is specifically predefined in the jQTouch themes to style an element like a traditional mobile phone toolbar.

© This unordered list tag has the class edgetoedge. The edgetoedge class tells jQTouch to stretch the list all the way from left to right in the viewable area.

O On this line there is an li that contains a link with its href pointing at the About panel. Including the arrow class on the li is optional; doing so will add a chevron to the right side of the item in the list.

© The toolbar elements each contain a single h1 element that will become the panel title. On this line, there are links with the classes button and back, which tell jQTouch to make the button look and act like a Back button.

* > The href on the Back button is set to #. Normally, this would tell the browser to return to the top of the current document. But when using ' ^ »A* jQTouch, it navigates back to the previous panel instead. In more advanced scenarios, you might want to use a specific anchor, such as #home, which instructs the Back button to navigate to a particular panel regardless of what the previous panel was.

With the basic HTML in place, it's time to add jQTouch to the party. Once you've downloaded jQTouch and unzipped it into the same directory as the HTML document, just add a few lines of code to the head of your page (Example 4-2).

® - For this and other examples in this book, you will need to download

^v jQTouch at, unzip it, and move the jqtouch and " v i-gt* themes directories into the same directory as your HTML document. You will also need to go into the jqtouch directory and rename the jQuery JavaScript file (such as jquery.1.3.2.min.js) to jquery.js.

Example 4-2. Adding these lines to the head of your document will activate jQTouch

<link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css"> <link type="text/css" rel="stylesheet" media="screen" href="themes/jqt/theme.css"> <script type="text/javascript" src="jqtouch/jquery.js"></script> <script type="text/javascript" src="jqtouch/jqtouch.js"></script> <script type="text/javascript">©

O This line includes the jqtouch.css file. This file defines some hardcore structural design rules that are very specific to handling animations, orientation, and other Android-specific minutiae. This file is required and there should be no reason for you to edit it.

© This line specifies the CSS for the selected theme, in this case, the "jqt" theme, which comes with jQTouch. The classes that we've been using in the HTML correspond to CSS selectors in this document. jQTouch comes with two themes available by default. You can also make your own by duplicating a default theme and making changes to it or writing a new one from scratch.

© jQTouch requires jQuery, so it is included here. jQTouch comes with its own copy of jQuery (which you need to rename to jquery.js, as described earlier), but you can link to another copy if you prefer.

O This is where we include jQTouch itself. Notice that you have to include jQTouch after jQuery or ain't nothin' gonna work.

© This brings us to the script block where we initialize the jQTouch object and send in a property value: icon.

jQTouch exposes several properties that allow you to customize the behavior and appearance of your app. You'll see several throughout the course of this book, and they are all optional. However, you'll pretty much always be using at least a few of them.

In this case, icon tells jQTouch where to look for the custom home screen icon.

The difference between the application before jQTouch (Figure 4-1) and after (Figure 4-2) is dramatic, but the truly astonishing change is that you've just added gorgeous left/right sliding to your app with 10 lines of code. jQTouch is awesome, and we're just getting started.


0 >3

Kilo About

Figure 4-1. Kilo before jQTouch...

0 0

Post a comment