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

Creating TextFields for user input


TextFields in Titanium are single-line textboxes used for capturing user input via the keyboard and usually form the most common UI element for user input in any application, along with Labels and Buttons. In this recipe we'll show you how to create a TextField, add it to your application's View, and use it to capture user input. We will style our TextField component by using a Constant value for the first time.

Getting ready

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

How to do it...

Type in the following code after the View is created but before we add that view to our Window. If you have been following along from the previous recipe, this code should be entered after your Labels were created:

//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'
});
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
});

view.add(tfInterestRate);

How it works...

In this example, we are creating a couple of basic TextFields with a rounded border style and introducing some new property types that don't appear in Labels and ImageViews including hintText. The hintText property displays a value in the TextField that disappears when that TextField has focus (for example, when a user taps it to enter some data using their keyboard).

The user input is available in the TextField's property called value. As you will note in the previous recipe, accessing this value is simply a case of assigning it to a variable (that is var myName = txtFirstName.value), or alternatively using the value property directly.

There's more...

TextFields are one of the most common components in any application, and in Titanium, there are a couple of points and options to consider whenever using them.

Retrieving text…

It is important to note that when you want to retrieve the text a user has typed into a TextField, you need to reference the value property and not text, like many of the other string-based controls!

Experimenting with other TextField border styles…

Try experimenting with other TextField border styles to give your app a different appearance. Other possible values are:

Titanium.UI.INPUT_BORDERSTYLE_BEZEL
Titanium.UI.INPUT_BORDERSTYLE_LINE
Titanium.UI.INPUT_BORDERSTYLE_NONE
Titanium.UI.INPUT_BORDERSTYLE_ROUNDED