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

Adding a TabGroup to your app


TabGroups are one of the most commonly used UI elements and form the basis of the layout for many iPhone and Android apps on the market today. The TabGroup consists of a sectioned set of tabs each containing an individual window, which in turn contains a navigation bar and title. On the iPhone, these tabs appear in a horizontal list on the bottom of the screen. On Android devices, by default, they appear as 'upside-down' tabs at the top of the screen, as shown in the next screenshot:

Getting ready

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

How to do it...

We are going to create two separate Windows—one of these will be defined in-line and the other Window will be loaded from an external JavaScript file called window2.js. Before writing any code, create a new JavaScript file called window2.js and save it to your Resources directory—the same folder where your app.js currently resides.

If you have been following along with the LoanCalc app so far, then delete the current code we created and replace it with the source below:

//create tab group
var tabGroup = Ti.UI.createTabGroup();

//create the window
var win1 = Titanium.UI.createWindow({
  width: 320,
  height: 480,
  top: 0,
  left: 0,
  backgroundImage: 'background.png',
  title: 'Loan Calculator',
  barImage: 'navbar.png'
});

//create the view, this will hold all of our UI controls 
//note the height of this view is the height of the window //minus 134px for the status bar and padding and adjusted for //navbar
var view = Titanium.UI.createView({
  width: 300,
  height: win1.height - 134,
  left: 10,
  top: 10,
  backgroundColor: '#fff',
  borderRadius: 5
});

//we will give the logo a left margin so it centers neatly //within our view
var _logoMarginLeft = (view.width - 253) / 2;

//now let's add our logo to an imageview and add that to our //view object
var logo = Titanium.UI.createImageView({
  image: 'logo.png',
  width: 253,
  height: 96,
  left: _logoMarginLeft,
  top: 0
});
view.add(logo);

//add the view to our window
win1.add(view);

//add the first tab and attach our window object (win1) to it
var tab1 = Ti.UI.createTab({  
    icon:'icon_calculator.png',
    title:'Calculate',

    window: win1
});

//create the second window for settings tab
var win2 = Titanium.UI.createWindow({
  width: 320,
  height: 480,
  top: 0,
  left: 0,
  backgroundImage: 'background.png',
  url: 'window2.js',
  title: 'Settings',
  barImage: 'navbar.png'
});

//add the second tab and attach our external window object //(win2 / window2.js) to it
var tab2 = Ti.UI.createTab({  
    icon:'icon_settings.png',
    title:'Settings',
    window: win2
});

//now add the tabs to our tabGroup object
tabGroup.addTab(tab1);  
tabGroup.addTab(tab2);  

//finally, open the tabgroup to launch the app
tabGroup.open();

How it works...

Logically, it is important to realize that the TabGroup, when used, is the root of the application and cannot be included from any other UI component. Each Tab within the TabGroup is essentially a wrapper for a single Window that can either be defined in-line or by providing the location of an external JavaScript file using the url property. These Windows are loaded only when that Tab gains focus for the first time, normally via the user tapping the Tab icon to gain focus to that particular Window.

The Tab icon is loaded from an image file, generally a PNG, but it's important to note that in both Android and the iPhone, all icons will be rendered in greyscale with alpha transparency—any color information will be discarded when you run the application.

There's more...

Apple can be particularly picky when it comes to using icons in your apps. Whenever a standard icon has been defined by Apple (such as the gears icon for settings) you should use the same.

A great set of additional 200 free tab bar icons are available at: http://glyphish.com.