Build mobile web applications with Sencha Touch

An HTML5 mobile JavaScript framework for WebKit browsers

Sencha Touch is a mobile web application framework that was built using HTML5, CSS3, and the JavaScript language. In this article, learn to apply your current web development skills to mobile web development. Download and set up Sencha Touch, and explore the basics using a sample application. Learn everything you need to know to start working with the Sencha Touch framework.

Brice Mason, Senior Web Developer, Equip For Quality

Brice MasonBrice Mason is a husband and father from upstate New York. He is also a developer at EQUIP For Quality, where he works on a team developing the next generation of health care analytics software for the long-term care industry. He maintains a personal site at http://www.bricemason.com.



22 February 2011

Also available in Chinese Japanese Portuguese

Overview

In the software development world, two important trends are increasingly important: mobile application development and standards-based HTML5 web development. The learning curve for either type of development can be steep. Developing a native mobile application often requires knowledge of specific platforms and skills, such as Objective-C for iPhone and Java™ for Android (and these are only two platforms). HTML5 development has gained traction lately because it is standards-based. While vendors are working rapidly to incorporate and comply with these early specifications, HTML5 is still rather immature.

The recent release of Sencha Touch 1.0 fuses the cutting-edge worlds of mobile application development with HTML5 web development to form a simple, accessible framework for building mobile web applications. In this article, learn everything you'll need to know to start working with the Sencha Touch framework.


Sencha Touch

Learning to build mobile applications—especially from the perspective of a web developer—can be troublesome. A variety of platforms to choose from and technologies to learn are available. HTML5 support, though gaining momentum quickly, is still not quite ready to be used for complex web applications, particularly line-of-business applications.

Frequently used abbreviations

Ajax: Asynchronous JavaScript + XML
API: application programming interface
CSS: Cascading Style Sheets
HTML: Hypertext Markup Language
JSON: JavaScript Object Notation
SDK: software development kit
UI: user interface

Sencha Touch combines the rich platforms of HTML5 and mobile web application development into a happy medium. The framework is developer-friendly and similar to using the Ext JS JavaScript framework. If you have moderate to advanced JavaScript experience, then Sencha Touch is approachable. If you already have skills as a JavaScript and CSS developer, Sencha Touch can immediately turn you into a mobile application developer, too.

Sencha is a company with a core commercial product offering, but it also supports open source software. Sencha Touch 1.0 is free for both personal and commercial use.

Platform support

Sencha Touch is currently supported on WebKit browsers, including the popular Apple iOS and Google Android platforms. Some might contend that this support isn't enough—that all platforms must be supported for Sencha Touch to be taken seriously. When adopting a framework in the mobile web development realm, it's wise to look for two things: platform richness and feature richness. A framework that abstracts the most from HTML5/CSS3 in a form that’s developer-friendly, and with a wide reach, is desirable. In this aspect, Sencha is astute in supporting the two most popular platforms and using their resources to provide rich, easy-to-use features for developers.

What you need to know

To start using Sencha Touch, all you need is working knowledge of the JavaScript language and CSS. As mentioned, the framework abstracts many of the features and styles you'd normally be assembling from scratch.

If you want to explore further, and perhaps build your own custom components or modify styles for your own brand, you need more in-depth skills in HTML5 and CSS3. For more information, see the Resources section.

Getting started

To get started with Sencha Touch:

  1. Download the framework from Sencha. (See Resources.)
  2. Extract the contents to the root of your development website.

    Consider renaming the extracted folder to a generic name, such as sencha-touch, so that you can use this same folder name for future framework versions without updating other files that reference it.

  3. Using a WebKit browser, such as Google Chrome, open the examples page at http://localhost/sencha-touch/examples/. Though your environment might look slightly different, you should see a screen similar to Figure 1.
Figure 1. Sencha Touch Examples
Screenshot of the Sencha Touch Examples page

Explore some of the framework features by building a Sencha Touch application.

  1. Create a new HTML file named index.html in the root of your site with the source code from Listing 1.
    Listing 1. Sample HTML5 document
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Touch Test</title>
    </head>
    
    <body>
    </body>
    </html>

    Once you create that single file, you are officially an HTML5 developer. The <!DOCTYPE HTML> doctype at the top is the key; it ensures the document is interpreted as HTML5.

  2. Modify the code from Listing 1 as shown in Listing 2.
    Listing 2. Installing Sencha Touch scripts and styles
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Touch Test</title>
    
    <!-- sencha touch css -->
    <link rel="stylesheet" type="text/css"
               href="sencha-touch/resources/css/sencha-touch-debug.css" />
    
    <!-- sencha touch javascript -->
    <script type="text/javascript" src="sencha-touch/sencha-touch-debug.js"></script>
    
    </head>
    
    <body>
    </body>
    </html>

    The above code adds the CSS and JavaScript files for the Sencha Touch framework. You hooked in the debug versions of these files, which is recommended for development purposes since they yield better error messages. To deploy, simply swap out the debug versions for the minified versions. You can also add your own custom CSS file and a JavaScript file for building your application.

  3. To keep the example simple, include some embedded JavaScript code. Start by modifying your code to resemble Listing 3.
    Listing 3. The simplest Sencha Touch application
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Touch Test</title>
    
    <!-- sencha touch css -->
    <link rel="stylesheet" type="text/css"
               href="sencha-touch/resources/css/sencha-touch-debug.css" />
    
    <!-- sencha touch javascript -->
    <script type="text/javascript" src="sencha-touch/sencha-touch-debug.js"></script>
    
    <!-- application script -->
    <script type="text/javascript">
        Ext.setup( {
            onReady: function() {
                // create the root panel
                new Ext.Panel({
                    fullscreen: true,
                    html: "Sencha Touch is ready!"
                });
            }
        });
    </script>
    
    </head>
    
    <body>
    </body>
    </html>
  4. Run the page in Google Chrome, and you should see something similar to Figure 2.
    Figure 2. The simplest Sencha Touch application
    Output of executed code from Listing 3, which displays Sencha Touch is ready!

Ext.setup

Several other useful configuration options for the Ext.setup function are available; see the API documents, found in the docs folder of your Sencha Touch installation, for details.

The code in Listing 3 introduces a block of JavaScript code you'll reuse throughout this article to explore framework features. If you've used Ext JS, this code might look familiar. The Ext.setup function is the starting point for your Sencha Touch applications. It accepts an object with a number of configuration options. The example uses the onReady option, which takes a function to evaluate when the document is ready. The onReady function creates the root panel of the application. The root panel takes up all available space and contains a simple string by using the fullscreen and html options, respectively.

Running applications in an Android emulator

So far you've used the Google Chrome browser to work through development. It's a good approach, especially when trying to deal with JavaScript and CSS bugs. Use a device emulator to start application testing. This section shows how to install an Android emulator.

  1. Download the most recent Android SDK for your operating system platform and extract it to disk.

    The SDK includes all the utilities required for getting an emulator up and running, but a few more steps are necessary.

  2. Android emulators require that you create an Android Virtual Device (AVD), which is basically configuration options that model an actual Android-powered device. To create an AVD, run the android utility in the tools folder of the Android SDK.

    The Android SDK and AVD Manager should open and look similar to Figure 3.

    Figure 3. Android SDK and AVD Manager
    Screenshot of Android SDK and AVD Manager, which is used to install add-on packages and manage virtual devices

At this point, if you try to create a new virtual device using the New… button, notice an important field labeled Target, which is disabled. Before you can create a new virtual device, download an add-on to the SDK.

  1. Select Available Packages from the left menu.
  2. Expand the site labeled https://dl-ssl.google.com/android/repository/repository.xml to reveal the list of available packages to install.
  3. Select the most recent SDK platform, as shown in Figure 4, and then click Install Selected.
    Figure 4. Available packages for Android SDK
    List of available packages for the Android SDK that can be installed via the Android SDK and AVD Manager
  4. Accept the installation by clicking the Install button in the next window.
  5. When the download and installation finish, click Close.

You just installed a potential target for any AVDs you want to create. This target is an Android device running version 2.2. The next step is to create the AVD.

  1. Select Virtual Devices from the left menu in the AVD Manager, and click New….
  2. Enter sencha-avd for the Name and select the Android 2.2 target you just installed as the Target. Leave all other defaults.
  3. Click Create AVD.

    Click OK at the confirmation window, then close the AVD Manager.

Everything you need to run an emulator is now in your local development environment. To run the emulator: open a terminal window, change directories to the root of your Android SDK installation, and enter the command in Listing 4.

Listing 4. Invoking the Android emulator
tools/emulator -avd sencha-avd

You can use the web browser in the Android emulator to browse to your test application at http://localhost/, but you'll get a big 404. This is because referencing localhost, or 127.0.0.1, in the emulator is actually within the context of the emulator itself. To reference your local development environment, use the address 10.0.2.2, which yields the screen shown in Figure 5.

Figure 5. Example running in the Android emulator
Screenshot of example defined in Listing 3 running in the Android emulator that displays Sencha Touch is ready!

A tour of Sencha Touch UI components

Now that you have the basics, this section provides a tour through some of the Sencha Touch UI components.

Buttons

You can create a number of button styles by using just a few configuration options. The code in Listing 5 creates a vertically stacked collection of all available buttons. The example application is expanded to include an array of items to be added to the root panel—in this case, a single panel with a vertically stacked collection of buttons.

The buttons are styled based on the ui configuration option. Supported button types are normal, back, round, action, and forward.

Listing 5. Buttons
<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.Panel({
        fullscreen: true,
        items: [
          // add a panel to the root panel
          // this will contain a vertical layout of buttons
          { 
            xtype: "panel",
            layout: "vbox",
            items: [
              {
                xtype: "button",
                ui: "normal",
                text: "Normal"  
              },
              {
                xtype: "button",
                ui: "back",
                text: "Back"  
              },
              {
                xtype: "button",
                ui: "round",
                text: "Round"  
              },
              {
                xtype: "button",
                ui: "action",
                text: "Action"  
              },
              {
                xtype: "button",
                ui: "forward",
                text: "Forward"  
              }
            ]  
          }
        ]
      });
    }
  });
</script>

Figure 6 shows the result.

Figure 6. Available button styles in Sencha Touch
Screenshot of executed code from listing 5, which displays a vertical stack of all available button styles

Forms

The forms suite contains all the usual suspects and more. It is apparent that HTML5-specific functions are being handled and incorporated. Support is available for field types (such as e-mail, web addresses, and date pickers) and attributes (such as placeholder text) in HTML5; Sencha Touch just makes it easier to use. Listing 6 shows some of the features.

Listing 6. Sampling of form controls
<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.Panel({
        fullscreen: true,
        items: [
          // add a panel to the root panel
          { 
            xtype: "form",
            items: [
              {
                xtype: "textfield",
                name: "name",
                label: "Name",
                placeHolder: "your name here"  
              },
              {
                xtype: "emailfield",
                name: "email",
                label: "Email",
                placeHolder: "you@example.com"  
              },
              {
                xtype: "urlfield",
                name: "url",
                label: "Url",
                placeHolder: "http://www.example.com"  
              },
              {
                xtype: "datepickerfield",
                name: "date",
                label: "Date",
                picker: { yearFrom: 2010 }  
              }
            ]  
          }
        ]
      });
    }
  });
</script>

Running the code in Listing 6 should yield a screen similar to Figure 7. The Date field is focused, revealing the date picker control docked at the bottom.

Figure 7. Sampling of available form fields
Screenshot of sampling of available form fields

Lists

When developing mobile web applications, you're working with limited real estate. Support for list-based UI components becomes important. Sencha Touch is equipped with support for various types of lists, including simple, grouped, and nested. Figure 8, which is taken from the Kitchen Sink demo in the framework download, demonstrates a grouped list. The vertical stack of letters at the right of the list exposes a useful method for jumping to specific portions of the list.

Figure 8. A grouped list from the Kitchen Sink demo
Screenshot of grouped list found in the Kitchen Sink demo

Icons and toolbars

Sencha Touch ships with an impressive repository of icons built in and ready to use. All you need to do is specify a string representing a CSS class for the icon you want. Listing 7 shows how to build two toolbars: one docked to the top of the root panel, and one docked to the bottom. Each toolbar is configured with a small sample of the icons available out of the box.

Many more icons are available. Check out the API docs and examples for more information.

Listing 7. Icons in toolbars
<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.Panel({
        fullscreen: true,
        dockedItems: [
          {
            xtype: "toolbar",
            dock: "top",
            items: [
              {
                iconMask: true,
                ui: "plain",
                iconCls: "add"
              },
              {
                iconMask: true,
                ui: "plain",
                iconCls: "delete"
              },
              {
                iconMask: true,
                ui: "plain",
                iconCls: "star"
              },
              {
                iconMask: true,
                ui: "plain",
                iconCls: "home"
              }
            ]
          },
          {
            xtype: "toolbar",
            dock: "bottom",
            items: [
              {
                iconMask: true,
                iconCls: "download"
              },
              {
                iconMask: true,
                iconCls: "favorites"
              },
              {
                iconMask: true,
                iconCls: "search"
              },
              {
                iconMask: true,
                iconCls: "user"
              }
            ]  
          }
        ]
      });
    }
  });
</script>

After running the code from Listing 7, you should see a screen similar to Figure 9.

Figure 9. Sampling of icons and styles
Screenshot of a sampling of icons and styles available out of the box in the framework

Carousels and tabs

Carousels and tabs are easy to implement using coding patterns you've already explored. Listing 8 shows a tabbed interface and a carousel.

Listing 8. Constructing carousels and tabs
<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.TabPanel({
        fullscreen: true,
        items: [
          {
            title: "Tab 1",
            html: "First tab"  
          },
          {
            title: "Tab 2",
            html: "Second tab"  
          },
          {
            title: "Tab 3",
            html: "Third tab"  
          }
        ]
      });
    }
  });
</script>

<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.Carousel({
        fullscreen: true,
        items: [
          {
            html: "First item"  
          },
          {
            html: "Second item"  
          },
          {
            html: "Third item"  
          }
        ]
      });
    }
  });
</script>

Carousels and tabs are similar in code and function. The carousel moves between cards by either performing a sliding gesture from side to side, or by clicking one of the circular icons docked at the bottom. By default, both controls transition between cards using a slide animation.

Figure 10 shows the tab interface.

Figure 10. Sample tab interface
Screenshot of the tab interface defined in Listing 8

Figure 11 shows the carousel interface.

Figure 11. Sample carousel interface
Screenshot of the sample carousel interface defined in Listing 8

Overlays

You can use several overlay controls. Your options include standard alert, confirm, and prompt controls, as well as plain modal controls.

Maps

In mobile web development, one of the more popular components is maps. Sencha Touch makes it very simple to include a map in your application using the Ext.Map component. Listing 9 shows how to include a map in the sample application. Remember to include the JavaScript file for the Google Maps API for this example to work.

Listing 9. Using maps
<!DOCTYPE HTML>
<html>
<head>
<title>Touch Test</title>

<!-- sencha touch css -->
<link rel="stylesheet" type="text/css"
         href="sencha-touch/resources/css/sencha-touch-debug.css" />

<!-- Google Maps API -->
<script type="text/javascript"
              src="http://maps.google.com/maps/api/js?sensor=true"></script>

<!-- sencha touch javascript -->
<script type="text/javascript" src="sencha-touch/sencha-touch-debug.js"></script>

<!-- application script -->
<script type="text/javascript">
  Ext.setup( {
    onReady: function() {
      // create the root panel
      new Ext.Panel({
        fullscreen: true,
        items: [
          {
            xtype: "map"  
          }
        ]
      });
    }
  });
</script>

</head>

<body>
</body>
</html>

The resulting map window is shown in Figure 12.

Figure 12. Example map control
Screenshot of example map window using code from listing 9

Events and data access

Sencha Touch has support for several key events you might expect in a mobile application, such as touch start/end, scroll start/end, tap, double tap, swipe, and pinch. Data access will look familiar if you've done Ext JS work in the past.

The Sencha Touch framework supports JSON with padding (JSONP), Yahoo! query language (YQL), and Ajax requests. Combined with the Sencha Touch data package, they provide a flexible mechanism for binding data to your UI components.

Style and design

Creating your own theme can be a rather difficult undertaking. The Sencha Touch framework has key features that make it much easier to modify the default styles and design.

The framework uses Syntactically Awesome Stylesheets (Sass), which is an extension of CSS3 that, among other things, lets you use variables and selector inheritance to add more power to theme development. Changing a single variable can impact the entire theme—and it's that easy.

Creating a new theme is outside the scope of this article. Resources has several links to help you get started.

Conclusion

This article provided an introduction to Sencha Touch, which is a mobile web application framework built using HTML5, CSS3, and JavaScript. You learned how to create a simple Sencha Touch application, and how to test it with a device emulator. You also explored some of the UI components.

This article is merely an introduction to the Sencha Touch framework. Now that you know the basics, consider diving deeper into the concepts using the Resources below.

Resources

Learn

  • Learn all about Sencha: read API docs, check out the blog, explore the forums, download demos, and more.
  • Read "Dive into HTML5," by Mark Pilgrim, to get a jumpstart on HTML5 development.
  • Learn more about Android. The Android Dev Guide has reference information and step-by-step instructions for common tasks.
  • The developerWorks Web development zone specializes in articles covering various web-based solutions.

Get products and technologies

  • Download Sencha Touch.
  • Download the latest version of the Google Chrome browser.
  • Get the latest Android SDK.
  • Download and learn more about Sass.
  • Evaluate IBM products in the way that suits you best: Download a product trial, try a product online, use a product in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement Service Oriented Architecture efficiently.

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Web development on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development, Open source, Industries, Mobile development
ArticleID=628442
ArticleTitle=Build mobile web applications with Sencha Touch
publish-date=02222011