Book Image

Sencha Touch Cookbook, Second Edition

By : Ajit Kumar
Book Image

Sencha Touch Cookbook, Second Edition

By: Ajit Kumar

Overview of this book

<p>Sencha Touch is one of the most popular HTML5 and JavaScript frameworks for building touch-based mobile devices. Using Sencha Touch, you can create applications for touch mobile devices with ease, and once created, the same application works on multiple platforms – iOS, Android, Blackberry – without having to change anything.</p> <p>Sencha Touch Cookbook, Second Edition is a practical, hands-on guide with easy to follow recipes that provide you with clear, step-by-step instructions, which will help you take complete advantage of the power of Sencha Touch 2 and will help you to build amazing applications for the mobile world.</p> <p>Sencha Touch Cookbook, Second Edition starts by showing you how to set up your project for development, then walks through building, packaging, and running it in a browser, emulator, or a real device. With the groundwork set, the subsequent recipes of the book take you through the different UI components offered by the framework and explains how to use them, when to use them, and, if needed, how to extend them for your own application need.</p> <p>You will also learn how to model your client side data, load data from different data sources, and use them on the data-centric UI components. The later parts of the book cover the practical day-to-day problems like how to create a custom view, how to take your application offline and support automatic sync, how to utilize the Geolocation to learn more about the user, how to utilize device features such as camera, contact, orientation, and how to link your application behaviour with the device buttons such as Back, Search, and so on. At the end, the book shows you how to create native packages without using PhoneGap/Cordova using Sencha Cmd.</p> <p>Using this book, you will learn everything about using Sencha Touch classes and components to build mobile applications that can run across multiple platforms.</p>
Table of Contents (17 chapters)
Sencha Touch Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up the Android-based development environment


This recipe describes the detailed steps we need to follow to set up the environment for the Android-based development. The steps do not include setting up the production environment.

Getting ready

Install JDK and set the following environment variables correctly:

  • JAVA_HOME

  • PATH

Install Apache Ant and set the following environment variables correctly:

  • ANT_HOME

  • PATH

How to do it...

Follow these steps to set up your Android-based development environment:

  1. Download Android SDK ADT Bundle for your platform from http://developer.android.com/sdk/index.html and install it inside the softwares folder, C:\sencha-touch-cookbook\softwares\adt-bundle-windows-x86_64.

  2. The installed folder contains two folders, sdk and eclipse. sdk contains Android SDK whereas eclipse contains ADT-bundled Eclipse.

  3. Add the following folder paths to the PATH environment variable:

    • platform-tools

    • tools

  4. Download Apache Cordova from http://cordova.apache.org/ and extract it to a folder or your choice, say, C:\sencha-touch-cookbook\softwares\cordova-2.4.0. We will refer to this as CORDOVA_HOME.

  5. Go to the CORDOVA_HOME folder and extract the following in it:

    • cordova-android: This is an Android application library that allows for Cordova-based projects to be built for the Android platform

    • cordova-cli: This is the command-line tool to build, deploy, and manage Cordova-based applications

    • cordova-js: This contains a unified JavaScript layer for Cordova-based projects

  6. Launch the command prompt and go to <CORDOVA_HOME>\cordova-android\bin.

  7. Run the following command to create our project:

    create c:\sencha-touch-cookbook\projects\SenchaTouch com.senchatouch.book SenchaTouch
    

    You shall see the following messages, which shows that everything went well:

    The syntax of the command create <project folder> <default package> <project name>.

  8. This will create a SenchaTouch folder inside the projects folder.

  9. Go to the eclipse folder inside the ADT installed folder.

  10. Launch Eclipse and use C:\sencha-touch-cookbook\workspace as the workspace folder.

  11. Click on the File menu and select Import. Select Existing Android Code Into Workspace under the Android section.

  12. Click on the Next button; this will take you to the Import Projects window. Click on the Browse... button, next to Root Directory, and select the SenchaTouch project that we created in the previous step.

  13. Click on the Finish button to import the project.

  14. Expand the assets\www folder and delete the files as shown in the following screenshot:

  15. Copy the www directory and located at C:\sencha-touch-cookbook\softwares\touch-2.2.1 folder to the assets directory and rename it to touch.

  16. Create and open a new file named ch01_01.js file in the assets/www/ch01 directory. Paste the following code in it:

    Ext.application({
        name: 'MyApp',
    
        launch: function() {
        Ext.Msg.alert("INFO", "Welcome to the world of Sencha Touch!");
        }
    });
  17. Now create and open a new file named index.html in the assets\www directory. Paste the following code in it:

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Sencha Touch Cookbook - Sample</title>
    <link rel="stylesheet" href="touch/resources/css/sencha-touch.css" type="text/css">
    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="touch/sencha-touch-all-debug.js"></script>
    <script type="text/javascript" charset="utf-8" src="ch01/ch01_01.js"></script>
    </head>
    <body></body>
    </html>
  18. Deploy the project to the simulator:

    1. Right-click the project, go to Run As, and click on Android Application.

    2. Eclipse will ask you to select an appropriate AVD. If there isn't one, you'll need to create it. To create an AVD, follow these steps:

      1. In Eclipse, go to Window | Android Virtual Device Manager.

      2. Select the Android Virtual Devices tab and click on the New… button.

      3. Enter your virtual device detail. For example, the following screenshot shows the virtual device detail for the Samsung Galaxy Ace running Android 2.2:

      4. Click on the OK button.

  19. Deploy the project to the device:

    1. Make sure USB debugging is enabled on your device and plug it into your system. You may enable it by going to Settings | Applications | Development.

    2. Right-click on the project, go to Run As, and click Android Application. This will launch the Android Device Chooser window.

    3. Select the device and click on the OK button.

With these steps, now you will be able to develop and test your application.

How it works...

From steps 1 to 5, we downloaded and installed Android SDK, the ADT-bundled Eclipse plugin, and Apache Cordova, which are required for the development of the Android-based application. The SDK contains the Android platform-specific files, an Android emulator, and various other tools required for the packaging, deployment, and running of Android-based applications. The ADT plugin for Eclipse allows us to create Android-based applications and to build, test, and deploy them using Eclipse.

In steps 6 and 7, we created an Android project using Apache Cordova's command-line utility. It creates a project that is completely compatible with the ADT plugin.

From steps 9 to 13, we imported the Cordova-created project into the ADT-bundled Eclipse.

In steps 14 and 15, we removed the unwanted files and folders and copied Sencha Touch SDK to the www folder of the project. This is required to run the touch-based applications. We kept the Cordova JavaScript file, which contains the implementation of the Cordova APIs. You shall do this if you intend to use the Cordova APIs in your application (for example, to get the contacts list in your application). For this book, this is an optional step; however, interested readers may find details about the API at http://docs.phonegap.com/en/2.4.0rc1/index.html.

In step 16, we created the ch01_01.js JavaScript file that contains the entry point for our Sencha Touch application. The Ext.application class registers the launch function as the entry point to the application, which the framework calls during the application startup.

In step 17, we modified the index.html file to include the Sencha-Touch-related JavaScript (sencha-touch-all-debug.js), CSS files (sencha-touch.css) and our application-specific JavaScript files (ch01_01.js). sencha-touch-all-debug.js is very useful during development as it contains well-formatted code, which can be used to analyze application errors during development. You also need to include the Cordova JS file in case you intend to use its APIs in your application. The Cordova-related JS and CSS files need to be included first, then come Sencha-Touch-related JS and CSS files, and finally the application-specific JS and CSS files.

In step 18, we created an Android virtual device (an emulator) and deployed and tested the application on it.

Finally, in step 19, we deployed the application on a real Android 2.2-compatible device.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.