Sencha Touch Cookbook - Ajit Kumar - E-Book

Sencha Touch Cookbook E-Book

Ajit Kumar

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Sencha touch is a versatile HTML5-based framework for developing mobile web apps that look and feel native on touch screen devices, and with it you can write your code once and deploy it to both iOS and Android saving you both time and money. The Sencha touch cookbook has a comprehensive selection of recipes covering everything from installation right through to HTML5 geo location. The Sencha Touch Cookbook really is your one stop resource for cross platform HTML5 application development. It covers the basics such as setting up an iOS and Android development environment right through to much more complex development issues such as touch gestures, animation, rich media and geo location. Every recipe is practically focused. Maximum action. Minimum theory.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 326

Veröffentlichungsjahr: 2011

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Sencha Touch Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code for this book
Errata
Piracy
Questions
1. Gear up for the Journey
Introduction
Setting up the Android-based development environment
Getting ready
How to do it...
How it works...
Setting up the iOS-based development environment
Getting ready
How to do it...
How it works...
See also
Setting up the Blackberry-based development environment
Getting ready
How to do it...
How it works...
See also
Setting up the browser-based development environment
Getting ready
How to do it...
How it works...
See also
Setting up the production environment
Getting ready
How to do it...
How it works...
See also
Detecting the device
How to do it...
How it works...
See also
Finding information about features that are supported in the current environment
How to do it...
How it works...
See also
Initializing your application
How to do it...
How it works...
See also
Tweaking your application to configure itself using profiles
How to do it...
How it works...
There's more...
Do not apply the profile at the time of application launch
Do not apply the profile on the components, by default
Ignoring the profile change
Deferred application of profile
See also
Responding to the orientation change
How to do it...
How it works...
There's more...
Stopping the orientation change
See also
2. Catering to your Form Related Needs
Introduction
Getting your form ready with FormPanel
Getting ready
How to do it...
How it works...
There's more...
Standard submit
Do not submit on field action
Post-submission handling
See also
Working with search
Getting ready
How to do it...
How it works...
There's more...
Using a placeholder
See also
Putting custom validation in the e-mail field
Getting ready
How to do it...
How it works...
See also
Working with dates using DatePicker
Getting ready
How to do it...
How it works...
There's more...
Setting the default date to the current date
Setting the default date to a particular date
Changing the slot order
Setting the picker date range
See also
Making a field hidden
Getting ready
How to do it...
How it works...
There's more...
Showing/Hiding a field at runtime
See also
Working with the select field
Getting ready
How to do it...
How it works...
There's more...
Using the custom display value
See also
Changing the value using Slider
Getting ready
How to do it...
How it works...
See also
Spinning the number wheel using Spinner
Getting ready
How to do it...
How it works...
There's more...
Recycling the values
See also
Toggling between your two choices
Getting ready
How to do it...
How it works...
See also
Checkbox and Checkbox group
Getting ready
How to do it...
How it works...
See also
Text and TextArea
Getting ready
How to do it...
How it works...
There's more...
Limiting the number of input characters
See also
Grouping fields with FieldSet
Getting ready
How to do it...
How it works...
There's more...
Adding instructions
See also
Validating your form
Getting ready
How to do it...
How it works...
See also
3. Containers and Layouts
Introduction
Keeping the container lightweight
Getting ready
How to do it...
How it works...
There's more...
Using layout
See also
Working with Panel
Getting ready
How to do it...
How it works...
There's more...
Docking items
Using layouts
See also
Adding items to a container at runtime
Getting ready
How to do it...
How it works...
There's more...
Inserting at a specific position
Removing an item
Hiding/Showing
See also
Building wizards using CardLayout
Getting ready
How to do it...
How it works...
There's more...
Changing the default active item
See also
Panel docking using DockLayout
Getting ready
How to do it...
How it works...
See also
Fitting into the container using FitLayout
Getting ready
How to do it...
How it works...
See also
Arranging your items horizontally using HBoxLayout
Getting ready
How to do it...
How it works...
There's more...
Component vertical alignment
Lay out items in reverse order
See also
Arranging your items vertically using VBoxLayout
Getting ready
How to do it...
How it works...
See also
Mixing layouts
Getting ready
How to do it...
How it works...
See also
4. Building Custom Views
Introduction
Basic HTML templating using Template
Getting ready
How to do it...
How it works...
There's more...
Compiling the template
Formatting values
See also
Using XTemplate for advanced templating
Getting ready
How to do it...
How it works...
There's more...
Compiling the template
Formatting values
See also
Conditional view rendering using XTemplate
Getting ready
How to do it...
How it works...
See also
Designing a custom view using DataView
Getting ready
How to do it...
How it works...
There's more...
Orientation change
See also
Showing the filtered data
Getting ready
How to do it...
How it works...
See also
Responding to the user action
Getting ready
How to do it...
How it works...
See also
5. Dealing with Data and Data Sources
Introduction
Creating models
Getting ready
How to do it...
How it works...
There's more...
Adding methods to a model
Extending a model
See also
Loading the form using a data model
Getting ready
How to do it...
How it works...
There's more...
Saving form data using the associated model
See also
Working with Store
Getting ready
How to do it...
How it works...
There's more...
Adding records to a store at runtime
See also
Converting incoming JSON data into models using JsonReader
Getting ready
How to do it...
How it works...
There's more...
Fetching a record from a nested data
Working with response metadata
See also
Converting incoming XML data into models using XmlReader
Getting ready
How to do it...
How it works...
See also
Validations in models
Getting ready
How to do it...
How it works...
There's more...
Inclusion
Exclusion
Format
Changing the default message
See also
Defining the custom validation
Getting ready
How to do it...
How it works...
See also
Relating models using association
Getting ready
How to do it...
How it works...
There's more...
Many-to-one association
See also
Persisting session-specific data using SessionStorageProxy
Getting ready
How to do it...
How it works...
There's more...
Working through the store
See also
Persisting data using LocalStorageProxy
Getting ready
How to do it...
How it works...
See also
Accessing in-memory data using MemoryProxy
Getting ready
How to do it...
How it works...
See also
Loading data through AJAX using AjaxProxy
Getting ready
How to do it...
How it works...
See also
Sorting of the data
Getting ready
How to do it...
How it works...
There's more...
Sending the sorting information to the server
Customizing the sort information being sent to the server
See also
Data grouping
Getting ready
How to do it...
How it works...
See also
Filtering data
Getting ready
How to do it...
How it works...
See also
Using a cross-domain URL in your application
Getting ready
How to do it...
How it works...
See also
6. Adding the Components
Introduction
Working with Button
Getting ready
How to do it...
How it works...
There's more...
Using badge
Using icon
See also
Creating a sheet of buttons with ActionSheet
Getting ready
How to do it...
How it works...
There's more...
Change the position and animation
See also
Carousel
Getting ready
How to do it...
How it works...
There's more...
Changing the direction
See also
Managing a list of data using List
Getting ready
How to do it...
How it works...
There's more...
Sorting entries
See also
Grouping items in a List
Getting ready
How to do it...
How it works...
See also
Navigating through a list of data using indexBar
Getting ready
How to do it...
How it works...
See also
Working with a list of nested data using NestedList
Getting ready
How to do it...
How it works...
There's more...
Using a property other than text
Showing the Back button
No toolbar, please!
See also
Picking your choice using Picker
Getting ready
How to do it...
How it works...
There's more...
Changing the position and animation
Aligning the slot text
See also
Switching between multiple views using SegmentedButton
Getting ready
How to do it...
How it works...
There's more...
Multiple pressed buttons
See also
Working with Tab panels
Getting ready
How to do it...
How it works...
There's more...
Positioning the tab bar at the bottom
Card switch animation
See also
Quicker access to application features using Toolbar
Getting ready
How to do it...
How it works...
There's more...
Adding non-button components
See also
Creating a new component
Getting ready
How to do it...
How it works...
See also
Extending an existing component capability
Getting ready
How to do it...
How it works...
See also
Overriding a component behavior
Getting ready
How to do it...
How it works...
See also
Adding behavior to an existing component using plugins
Getting ready
How to do it...
How it works...
See also
7. Adding Audio/Visual Appeal
Introduction
Animate me!
Getting ready
How to do it...
How it works...
There's more...
Working with different animation durations
Direction of animation
Reversing the animation
Postponing animation
See also
Ding-dong! You have a message!
Getting ready
How to do it...
How it works...
See also
Working with videos
Getting ready
How to do it...
How it works...
See also
Adding the chart support to your app
Getting ready
How to do it...
How it works...
See also
Working with an area chart
Getting ready
How to do it...
How it works...
There's more...
Showing legend
Changing the legend text
See also
Generating a bar chart
Getting ready
How to do it...
How it works...
There's more...
Changing the spacing between the bars
See also
Creating a column chart
Getting ready
How to do it...
How it works...
See also
Showing a group of bars and columns
Getting ready
How to do it…
See also
Switching between stacked and grouped orientation
Getting ready
How to do it...
How it works...
There's more...
Changing gesture
Resetting the chart state
See also
Highlighting and displaying an item detail
Getting ready
How to do it...
How it works...
See also
Creating a gauge chart
Getting ready
How to do it...
How it works...
There's more...
Showing a needle
The donut effect
See also
Creating a line chart
Getting ready
How to do it...
How it works...
There's more...
Filling the area
Smooth curves
See also
Creating a pie chart
Getting ready
How to do it...
How it works...
There's more...
The donut effect
Pie length derived from the data
See also
Rotating the pies
Getting ready
How to do it...
How it works...
See also
Grouping the pies
Getting ready
How to do it...
How it works...
See also
Highlighting a pie
Getting ready
How it works...
See also
Using a radar chart
Getting ready
How to do it...
How it works...
There's more...
Using a different marker
See also
Using a scatter chart
Getting ready
How to do it...
How it works...
See also
8. Taking your Application Offline
Introduction
Detecting offline mode
Getting ready
How to do it...
How it works...
There's more...
Using aggressive timeout
See also
Storing your data offline
Getting ready
How to do it...
How it works...
See also
Storing your images offline
Getting ready
How to do it...
How it works...
See also
Application caching
Getting ready
How to do it...
How it works...
See also
9. Engaging Users by Responding to Events
Introduction
Handling Touch Events
Getting ready
How to do it...
Handling Scroll Events
Getting ready
How to do it...
Handling Tap Events
Getting ready
How to do it...
Handling Double Tap Events
Getting ready
How to do it...
Handling TapHold Events
Getting ready
How to do it...
Handling Swipe Events
Getting ready
How to do it...
Handling Pinch Events
Getting ready
How to do it...
Handling Drag Events
Getting ready
How to do it...
10. Increased Relevance Using Geolocation
Introduction
Finding out your location
Getting ready
How to do it...
How it works...
See also
Auto-update of your location
Getting ready
How to do it...
How it works...
See also
Tracking direction and speed
Getting ready
How to do it...
How it works...
See also
Hooking up Google Maps with your application
Getting ready
How to do it...
How it works...
See also
Working with Google Maps options
Getting ready
How to do it...
How it works...
See also
Mapping Geolocation on Google Maps
Getting ready
How to do it...
How it works...
See also
Index

Sencha Touch Cookbook

Sencha Touch Cookbook

Copyright © 2011 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2011

Production Reference: 1081211

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-84951-544-3

www.packtpub.com

Cover Image by Rakesh Shejwal ( <[email protected] >)

Credits

Author

Ajit Kumar

Reviewers

Kristian Kristensen

Matthew Makai

Acquisition Editor

Usha Iyer

Development Editor

Meeta Rajani

Technical Editor

Azharuddin Sheikh

Project Coordinator

Kushal Bhardwaj

Proofreaders

Bernadette Watkins

Neha Shetty

Indexer

Hemangini Bari

Graphics

Conidon Miranda

Production Coordinator

Alwin Roy

Cover Work

Alwin Roy

About the Author

Ajit Kumar started his IT career with Honeywell, Bangalore, in embedded systems and moved on to Enterprise Business Applications (such as ERP) in his 11 years of journey. From day one, he has been a staunch supporter and promoter of open source and strongly believes that open source is the way for a liberal, diversified, and democratic setup, like India.

He dreams and continuously endeavors that the architecture, frameworks, and tools must facilitate the software development—at the speed of thought.

Ajit holds B.E. in Computer Science and Engineering from Bihar Institute of Technology and has co-founded Walking Tree, which is based out of Hyderabad, India. This is the place where he plays a role of CTO and works to fulfill his vision.

I would like to thank my wife, Priti, my sons, Pratyush and Piyush, who were very patient and supportive; my business partners, Alok and Pradeep, who, relentlessly, talk about the book; friends who always encouraged me; the reviewers and all the people behind the Sencha Touch project and other open source projects.

About the Reviewers

Kristian Kristensen is an independent software development consultant. Through his company, Whiz IT, he takes on the role of a teacher, coach, facilitator, and anything in between to help software shops improve their processes and skills. He is particularly interested in languages and the way they shape our thoughts and problem-solving abilities.

Kristian worked as a consultant for Microsoft before embarking on the journey of freelance consulting. He holds a Master's degree in software engineering from Aalborg University and currently lives in Brooklyn, NY with his wife.

Matthew Makai is a software development consultant with Excella Consulting in Arlington, Virginia. He works on application development with Python and Java using the Django, JQuery Mobile, Sencha Touch, and PhoneGap frameworks. He is interested in enhancing personal and business decisions with mobile applications and data visualization. Matthew earned his Computer Science B.S. at James Madison University, his Computer Science M.S. at Virginia Tech, and his Management of Information Technology M.S. at the University of Virginia.

Matthew writes about consulting and solutions to technical problems on his blog at http://mmakai.com/.

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. 

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

Preface

Sencha Touch is a HTML5-compliant framework for development of touch-based applications. An application written using this framework can be run—without any change—on iOS, Android, and Blackberry-based touch devices. The framework comes along with numerous built-in UI components similar to the ones that we see on different mobile platforms, nicely designed with efficient data package to work with varied client-side or server-side data sources. In addition to this, the framework also offers APIs to work with DOM and it brings in the extensibility to every aspect of it, which is a bingo combination that every enterprise looks for and expects from a framework.

What this book covers

Chapter 1, Gear up for the Journey. This chapter is all about setting up the right development environment for iOS, Android, and Blackberry. It covers the detailed step to set up the environment using which we can build and test our application, either on a browser or on an emulator or a real device. The latter half of the chapter covers the Sencha Touch APIs that can be used to detect what device our application is running, what platform the application is running, what features are offered by a platform, how to handle orientation change, and build profiles for different platforms or devices and let our application configure itself using those profiles.

Chapter 2, Catering to your Form Related Needs. This chapter covers every aspect of a form, including the different form fields offered by Sencha Touch, configuring each one of them for the user in a form, and configuring ways by which a typical form validation can be done. Fields such as Search, E-mail, DatePicker, Select, Slider, Checkbox, TextArea, FieldSet, and so on are covered in this chapter along with their detailed usage.

Chapter 3, Containers and Layouts. Containers contain one or more child items, and layouts help us position our content properly on the screen, and this chapter is all about them. It covers what are the different types of containers and layouts available with the framework and their behavior, which is a key point in developing an application where we would have nested containers, each one of them having their own layout to position their children on the screen.

Chapter 4, Building Custom Views. If the components offered by Sencha Touch is not sufficient to achieve the layout and look-n-feel that we are looking for, then custom views is the way to go. In this chapter, we cover Template, XTemplate, and DataView classes of Sencha Touch to see how to build custom views and how to handle events on it to build interactive custom views.

Chapter 5, Dealing with Data and Data Sources. This chapter is all about working with different data sources, storing data on the client side and using it in the most effective way. It shows how to work with the local data source, as well as remote data source. The chapter also covers how to associate data with different components, including form, and validating, filtering, grouping, and sorting data.

Chapter 6, Adding the Components. This chapter covers various other components of Sencha Touch such as Button, ActionSheet, List, IndexBar, NestedList, Picker, Toolbar, and so on. It demonstrates how to configure each of these components.

Chapter 7, Adding Audio/Visual Appeal. This chapter covers how to work with media—audio and video—and different graphs and charts to present our data, graphically. As charts don't come with Sencha Touch, this chapter explains the steps to configure the charts framework with Sencha Touch and then use it to build interactive charts.

Chapter 8, Taking your Application Offline. In this chapter, we will see how to detect the offline mode on a device or a platform and how to model our application to make it work even in the offline mode.

Chapter 9, Engaging Users by Responding to Events. In browsers, we have mouse events whereas a touch device raises events specific to touch actions such as tab, Double tap, drag, swipe, and so on. In this chapter, we will look at the list of touch events the framework offers and the ways to handle them to respond to those events.

Chapter 10, Increased Relevance Using Geolocation. This chapter shows how to fetch the GeoLocation information on a device/platform and work with it. We will cover various aspects such as how to find out the direction and speed at which the device is moving, and how to integrate the Geolocation information with Google map to show interesting and relevant information to the user.

What you need for this book

In order to follow this book, you would need the Sencha Touch framework and good knowledge of JavaScript and some knowledge of HTML and CSS. The following list shows the software needed:

Sun JDK Version 1.5 or aboveEclipse 3.3 or abovePhoneGap 1.0.0Sencha Touch 1.1.0 librarySencha Touch Charts 1.0 library Android SDKADT PluginXCode 4Blackberry SDK

If you want to try out the recipes on your touch device, you may have to have one.

Who this book is for

This book is for someone who wants to learn about a framework which can be used to develop HTML5-compliant mobile applications, and can work on various different platforms—Android, iOS, and BlackBerry. This cookbook provides numerous recipes for the developers to understand Sencha Touch functionalities, in general, and use them to address practical needs in particular.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Copy phonegap.1.0.0.js from your PhoneGap downloaded earlier to assets/www."

A block of code is set as follows:

Ext.setup({ onReady: function() { if (Ext.is.Android) Ext.Msg.alert("INFO", "Welcome Android user!"); if (Ext.is.Blackberry) Ext.Msg.alert("INFO", "Welcome Blackberry user!"); if (Ext.is.iPad) Ext.Msg.alert("INFO", "Welcome iPad user!"); } });

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

layout: { type: 'hbox', direction: 'reverse' }

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " Launch Eclipse, click on the File menu, and select New | Android Project".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code for this book

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Gear up for the Journey

In this chapter, we will cover:

Setting up the Android-based development environmentSetting up the iOS-based development environmentSetting up the Blackberry-based development environmentSetting up the browser-based development environmentSetting up the production environmentDetecting your deviceFinding information about features that are supported in the current environmentInitializing your applicationLetting your application configure itself using profilesResponding to the orientation change

Introduction

Like any other development, the first and foremost thing which is required, before we embark on our journey, is setting up the right environment so that the development, deployment, and testing becomes easy and effective. Moreover, this calls for a list of tools which are appropriate in this context. In this chapter, we will cover the topics related to setting up the environment using the right set of tools. Sencha Touch works on Android, iOS, and Blackberry platforms. For each of these platforms, we will see what steps we need to follow to set up the complete development and deployment environment. We will be packaging our Sencha Touch-based application using PhoneGap. PhoneGap is another JavaScript framework which provides the following two important capabilities:

The APIs needed to access the device features such as camera, address book, and so on.A build mechanism for writing the code once (in the form of JS, HTML, CSS) and packaging them for different platforms such as iOS, Android, and so on.

Throughout the book, we will be using the following software:

Sun JDK Version 1.5 or aboveEclipse 3.3 or abovePhoneGap 1.0.0Sencha Touch 1.1.0 libraryAndroid SDKADT PluginXCode 4Blackberry SDK

Before we get any further, you should download and install the following, which will act as a common base for all our discussions:

Sun JDK 1.5 or aboveEclipse 3.3 or aboveSencha Touch 1.1.0 library

After downloading the Sencha Touch library, extract it to a folder, say c:\sencha-touch. When you extract the folder, you would see the folders as shown in the following screenshot:

There are many files, however, which are not required for development and testing.

Note

The docs folder contains the documentation for the library and is very handy when it comes to referring to the properties, configs, methods, and events supported by different classes. You may want to copy it to a different folder, so that you can refer to the documentation whenever needed.

Delete the files and folders which are enclosed within the rectangles as shown in the following screenshot:

This prepares us to get started. As Sencha Touch is a JavaScript library, you may want to configure your Eclipse installation for JavaScript development. You may install the Spket plug-in and configure it for Sencha Touch development. Steps to do so are detailed on the Spket website (http://spket.com/) and hence have been excluded from this book.

Setting up the Android-based development environment

This recipe describes the detailed steps we shall follow to set up the environment for the Android-based development. The steps do not include setting up the production environment, which are detailed in a different recipe.

Getting ready

Before you begin, check that JDK is installed and the following environment variables are set correctly:

JAVA_HOMEPATH

How to do it...

Carry out the following steps:

Download and install Android SDK from the following URL: http://developer.android.com/sdk/index.html.Download and install Eclipse ADT Plugin from the following URL:http://developer.android.com/sdk/eclipse-adt.html#installing.Download and install PhoneGapfrom http://www.phonegap.com.Launch Eclipse, click on the File menu, and select New | Android Project. Fill in the details, as shown in the following screenshot, and click on the Finish button:In the root directory of the project, create two new directories:
libs: To keep the third party jar that we will be using. In this case, we will keep the PhoneGap jar in itassets/www: This is the default folder the SDK expects to contain the complete set of JS, CSS, and HTML files
Copy phonegap.1.0.0.js from your PhoneGap downloaded earlier to assets/www.Copy phonegap.1.0.0.jar from your PhoneGap downloaded earlier to libs.Copy the xml folder from the Android folder of your PhoneGap downloaded earlier to the res folder.Make the following changes to App.java, found in the src folder in Eclipse:
Change the class extend from Activity to DroidGapReplace the setContentView() line with super.loadUrl("file:///android_asset/www/index.html");Add import com.phonegap.*;
Right-click on the libs folder and select Build Paths | Configure Build Paths. Then, in the Libraries tab, add phonegap-1.0.0.jar to the Project. You may have to refresh the project (F5) once again.Right-click on AndroidManifest.xml and select Open With | Text Editor.Paste the following permissions under versionName:
<supports-screensandroid:largeScreens="true"android:normalScreens="true"android:smallScreens="true"android:resizeable="true"android:anyDensity="true"/> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWOvRK_STATE" />

Tip

Downloading the example code for this book

You can download the example code files for all Packt books you have purchased from your account at 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.

Add android:configChanges="orientation|keyboardHidden" to the activity tag in AndroidManifest.Move the c:\sencha-touch folder to the assets/www directory.Create and open a new file named ch01_01.js in the assets/www/ch01 directory. Paste the following code into it:
Ext.setup({ onReady: function() { Ext.Msg.alert("INFO", "Welcome to the world of Sencha Touch!"); } });
Now create and open a new file named index.html in the assets/www directory and paste the following code into it:
<!DOCTYPE HTML> <html> <head> <title>Yapps! - Your daily applications!</title> <link rel="stylesheet" href="sencha-touch/resources/css/sencha-touch.css" type="text/css"> <script type="text/javascript" charset="utf-8" src="phonegap.1.0.0.js"></script> <script type="text/javascript" charset="utf-8" src="sencha-touch/sencha-touch-debug.js"></script> <script type="text/javascript" charset="utf-8" src="ch01/ch01_01.js"></script> </head> <body></body> </html>
Deploy to Simulator:
Right-click on the project and go to Run As and click on Android ApplicationEclipse will ask you to select an appropriate AVD (Android Virtual Device). If there is not one, then you will need to create it. In order to create an AVD, follow these steps:
In Eclipse, go to Window | Android SDK and AVD ManagerSelect Virtual Devices and click on the New buttonEnter your virtual device details, for example, the following screenshot shows the virtual device details for the Samsung Galaxy Ace running Android 2.2:
Deploy to Device:
Make sure USB debugging is enabled on your device and plug it into your system. You may enable it by going to Settings | Applications | DevelopmentRight-click on the project, go to Run As, and click on Android Application. This will launch the Android Device Chooser windowSelect the device and click on the OK button

With the preceding steps, you will be able to develop and test your application.

How it works...

In steps 1 through 3, we downloaded and installed Android SDK, its Eclipse plugin, and PhoneGap, 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, build, test, and deploy them using Eclipse.

In step 4, we created an Android 2.2 project by using the ADT plugin.

In steps 5 through 12, we created the required folders and kept the files in those folders, and updated some of the files to make this Android project a PhoneGap-based Android project. In step 5, we created two folders: libs and assets\www. The libs folder is where the PhoneGap and other third party libraries (JAR files) need to be kept. In our case, we only had to put in the PhoneGap JAR file (step 7). This JAR contains the PhoneGap implementation, which takes care of packaging the application for the target device (in this case, Android). The www folder is where the complete application code needs to be kept. PhoneGap will use this folder to create the deployment package.

In step 6, we copied the PhoneGap's JavaScript file which contains the implementation of the PhoneGap APIs. You will do this if you intend to use the PhoneGap APIs in your application (for example, to get the contacts list in your application).

Note

For this book, this is an optional step. However, interested readers may find details about the API at the following URL: http://docs.phonegap.com/en/1.0.0/index.html.

In steps 8 and 9, we added the PhoneGap JAR file to our project's Build Path, so that it becomes available for the application development (and takes care of the compilation errors).

Then, in steps 10 through 13, we made changes to the manifest file to add the required application privileges, for example, access to the phone book, access to the phone status, and so on, when it is run on the Android 2.2 platform. You may learn more about the content of the manifest file and each of the elements that we added to it by referring to http://developer.android.com/guide/topics/manifest/manifest-intro.html.

In step 14, we moved the Sencha Touch library files to the www folder, so that they are included in the package. This is required to run Touch-based applications.

In step 15, we created the ch01_01.js JavaScript file, which contains the entry point for our Sencha Touch application. We have used the Ext.setup API. The important property is onReady, which is a function that Ext.setup registers to invoke as soon as the document is ready.

In step 16, we modified the index.html to include the Sencha Touch library related JavaScript: sencha-touch-debug.js and CSS file: sencha-touch.css, and our application specific JavaScript file: ch01_01.js. The sencha-touch-debug.js file is very useful during development as it contains the nicely formatted code which can be used to analyze the application errors during development. You also need to include the PhoneGap JS file, if you intend to use its APIs in your application. The order of inclusion of the JavaScript and CSS files is PhoneGap | Sencha Touch | Application specific files.

In step 17, we created an Android Virtual Device (an emulator), and deployed and tested the application on it.

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

Setting up the iOS-based development environment

This recipe outlines the steps to set up the environment for the iOS-based (for example, iPhone, iPad, iPod) development.

Getting ready

JDK is installed and the following environment variables are set correctly:

JAVA_HOMEPATH

You should have created the ch01_01.js file as mentioned in the previous recipe.

How to do it...

Carry out the following steps:

Download and install Xcode from Apple Developer Portal (http://developer.apple.com). This requires you to have membership of the iOS and Mac developer programs.Download the copy of PhoneGap-1.0.0 and extract its contents. Navigate to the iOS directory and run the installer until completion.Launch Xcode, and under the File menu, select New and then New Project. Name the new project SenchaTouch.Select PhoneGap-based Application from the list of templates.Click on the Next button. Fill in the Product Name and Company Identifier for your application.Choose a directory in which to store your application.You should see your project in Xcode 4 now. Click on the Run button at the top-left corner. Your build should succeed and launch in the simulator.You should see an error in your simulator informing you that index.html was not found.In order to fix this, we need to copy the www directory into the project. Right-click on the project in the left navigation window and click on Show in finder.In Finder, you should see the www directory beside your project.Drag the www folder into Xcode 4.Move the C:\sencha-touch folder to www.After you drag, you should see a prompt with a few options. Make sure to select Create folder references for any added folders