Book Image

Appcelerator Titanium Smartphone App Development Cookbook

By : Boydlee Pollentine
Book Image

Appcelerator Titanium Smartphone App Development Cookbook

By: Boydlee Pollentine

Overview of this book

<p>Appcelerator Titanium Mobile allows developers to realize their potential to develop full native iPhone and Android applications by using free Titanium Studio tools without the need to know Objective-C or Java. This practical hands-on cookbook shows you exactly how to leverage the Titanium API to its full advantage and become confident in developing mobile applications in no time at all.<br /><br />Appcelerator Titanium Smartphone App Development Cookbook offers a set of practical and clear recipes with a step-by-step approach for building native applications for both the iPhone and Android platforms using your existing knowledge of JavaScript.<br /><br />This cookbook takes a pragmatic approach to using your JavaScript knowledge to create applications for the iPhone and Android platforms, from putting together basic UIs to handling events and implementation of third party services such Twitter, Facebook and Push notifications. This book shows you how to utilize both remote and local datasources using XML, JSON and the SQLite database system. The topics covered will guide you to use popular Titanium Studio tools effectively and help you leverage all the advanced mobile features such as Geolocation, Accelerometer, animation and more. Finally, you’ll learn how to register developer accounts and how to publish your very own apps to the Android and Apple marketplaces.</p>
Table of Contents (18 chapters)
Appcelerator Titanium Smartphone App Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Working with keyboards and keyboard toolbars


When a TextField or TextArea control gains focus in either the iPhone or Android, the default keyboard is what springs up onto the screen. However, there will be times when you wish to change this behavior example, you may only want to have the user input numeric characters into a TextField when they are providing a numerical amount (such as their age, or a monetary value). Additionally, Keyboard Toolbars can be created to appear above the keyboard itself, which will allow you to provide the user with other options such as removing the keyboard from the Window, or allowing copy/paste operations via a simple button tap.

In the following recipe, we are going to create a toolbar that contains both a system button, and another system component called FlexibleSpace. These will be added to the top of our numeric keyboard which will appear whenever the TextField for amount or interest rate gains focus. Note that in this example we have updated the tfAmount and tfInterestRate TextField objects to now contain keyboardType and returnKeyType properties.

Getting ready

Note that toolbars are iPhone-specific, and that they may not be available for Android in the current Titanium SDK.

Note

The complete source code for this recipe can be found in the /Chapter 1/Recipe 5 folder.

How to do it...

Open up your app.js file and type in the following code. If you have been following along from the previous recipe, this code should replace the previous recipe's code for adding the amount and interest rate TextFields:

//flexible space for button bars
var flexSpace = Titanium.UI.createButton({     
  systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
 
//done system button
var buttonDone = Titanium.UI.createButton({
    systemButton:Titanium.UI.iPhone.SystemButton.DONE,
	 bottom: 0
});

//add the event listener 'click' event to our done button
buttonDone.addEventListener('click', function(e){
    tfAmount.blur();
   tfInterestRate.blur();
   tfInterestRate.top = 150;
   labelInterestRate.top = 150;
   interestRate = tfInterestRate.value;
   tfAmount.visible = true;
   labelAmount.visible = true;
});

//creating the textfield for our loan amount input
var tfAmount = Titanium.UI.createTextField({
   width: 140,
   height: 30,
   top: 100,
   right: 20,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
    returnKeyType:Titanium.UI.RETURNKEY_DONE,
   hintText: '1000.00',
   keyboardToolbar: [flexSpace,buttonDone],
    keyboardType:Titanium.UI.KEYBOARD_PHONE_PAD
});
view.add(tfAmount);
//creating the textfield for our percentage interest rate //input
var tfInterestRate = Titanium.UI.createTextField({
   width: 140,
   height: 30,
   top: 150,
   right: 20,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
    returnKeyType:Titanium.UI.RETURNKEY_DONE,
   value: interestRate,
   keyboardToolbar: [flexSpace,buttonDone],
    keyboardType:Titanium.UI.KEYBOARD_PHONE_PAD
});

//if the interest rate is focused change its top value so we //can see it (only for the iphone platform though!)
tfInterestRate.addEventListener('focus', function(e){
  if(Ti.Platform.osname == 'iphone') {
    tfInterestRate.top = 100;
    labelInterestRate.top = 100;
    tfAmount.visible = false;
    labelAmount.visible = false;
  }
});

view.add(tfInterestRate);

How it works...

In this recipe we are creating a TextField and adding it to our View. By now, you should have noticed how many properties are universal among the different UI components; width, height, top, and right are just four that are used in our TextField called tfAmount that have been used in previous recipes for other components. Many touch screen phones do not have physical keyboards; instead we are using a touch screen keyboard to gather our input data. Depending on the data you require, you may not need a full keyboard with all of the QWERTY keys and may want to just display a numeric keyboard (as seen in the following screenshot); such as when you were using the telephone dialling features on your iPhone or Android device. Additionally, you may require the QWERTY keys but in a specific format; custom keyboards make user input quicker and less frustrating for the user by presenting custom options such as keyboards for inputting web addresses and emails with all of the 'www' and '@' symbols in convenient touch locations.

There's more...

Try experimenting with other Keyboard styles in your Titanium app!

Experimenting with keyboard styles

Other possible values are:

Titanium.UI.KEYBOARD_DEFAULT
Titanium.UI.KEYBOARD_EMAIL
Titanium.UI.KEYBOARD_ASCII
Titanium.UI.KEYBOARD_URL
Titanium.UI.KEYBOARD_NUMBER_PAD
Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION
Titanium.UI.KEYBOARD_PHONE_PAD