BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
Appcelerator Titanium Kinetic practices part 1
1. Jeff Batt
eLearning Brothers
Appelerator Titanium - Create Mobile Apps Using
Javascript, CSS & XML: Product Development Part Manager
1
Jeff Batt
Kinetic Media
Owner / Head Trainer
jeff@kinetic-media.com
Contact info: jeff@kinetic-media.
com
2. www.kinetic-media.com
www.youtube.com/jeffbatt01 twitter.com/jeffbatt01
Access Files
Connect with me:
(www.kinetic-media.com)
Approach:
• More hands on. Walk you through how to use the software instead of just talking about it.
• Hard to tell who is on what level so I start from the beginning on the basic level.
• Additional tutorials can be found free on my blog and YouTube channel.
3. Differences between Titanium & Phonegap
What is the difference between PhoneGap & Titanium?
- The basic answer is PhoneGap uses straight HTML so you have to use your own elements in
CSS or use some library like jQuery but it does not allow you to use native elements like tabs,
table views and other elements.
4. Differences between Titanium & Phonegap
jQuery Mobile & Phonegap
What is the difference between PhoneGap & Titanium?
- The basic answer is PhoneGap uses straight HTML so you have to use your own elements in
CSS or use some library like jQuery but it does not allow you to use native elements like tabs,
table views and other elements.
5. Differences between Titanium & Phonegap
jQuery Mobile & Phonegap Appcelerator Titanium
What is the difference between PhoneGap & Titanium?
- The basic answer is PhoneGap uses straight HTML so you have to use your own elements in
CSS or use some library like jQuery but it does not allow you to use native elements like tabs,
table views and other elements.
6. Alloy VS Classic
Alloy Classic
VS
Alloy vs Classic?
- Alloy is a new language introduced by Appcelerator. It uses the MVC model. Although both
languages can produce the same thing Alloy can do it with less code (most of the times). It
also separates your code more for easier maintainability.
- It does come down to preference, which way you prefer to code.
7. Alloy - MVC
index.xml
index.tss index.js
MVC
- Alloy uses uses an MVC model. This separates the code to make it more maintainable.
- The model is used in the index.xml to define the content.
- The view is used in the index.tss to control the look of the elements. The nice thing about
this is you can define different visuals based on device, and OS.
- The controller is within the index.js. This controls what happens with elements are clicked
on or logic for the app.
8. Starting a New Project
Starting a New Project
- To start a new project simply click on File > New > Mobile Project
9. Starting a New Project
Alloy: Classic:
Type of Project
- Depending on the type of project you want you can select one of the default templates or
just select default to start your own.
10. Creating a Window
Classic Code:
app.js
//Establish the window and color
var window = Titanium.UI.createWindow({
backgroundColor:'red'
});
//Open the window
window.open();
Classic:
//Establish the window and color
var window = Titanium.UI.createWindow({
backgroundColor:'red'
});
//Open the window
window.open();
12. Windows and Views
Windows vs Views
- There can only be 1 window open and any time but you can establish a numerous views
within the window. You can either add elements to your window or to any view to group them
together and then you can position your views form there.
13. Windows and Views
Window 1
Windows vs Views
- There can only be 1 window open and any time but you can establish a numerous views
within the window. You can either add elements to your window or to any view to group them
together and then you can position your views form there.
14. Windows and Views
Window 1
View 1
View 2
View 3
Windows vs Views
- There can only be 1 window open and any time but you can establish a numerous views
within the window. You can either add elements to your window or to any view to group them
together and then you can position your views form there.
15. Different Types of Views
Window 1
Image View
Table View
Scroll View
Different Types of Views
- Like we will learn there are many different types of views you can add or you can have just a
normal view.
16. Analogy - Creating Objects then Adding to Window
Get the Actor
Get the Object
var actor = Ti.UI.createView({
});
Compare to Actors on Stage/Camera
- Just like getting an actor ready for the stage defining objects to place on the stage is
similar. You first define which actor you want to use for your “production”. For the code you
choose from the different objects (actors) available and then wrap them in a variable most of
the time using the Ti.UI.create...
- This isn’t all you need to do. In order for them to be ready you need to define some
attributes of how you want them to look/act.
17. Analogy - Creating Objects then Adding to Window
Add Attributes to Actor
- Makeup
- Costume
- etc
var actor = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
Add Attributes to the Object
- Width
- Height
- etc
Compare to Actors on Stage/Camera
- Just like an actor cannot just walk off the street and portray the character you have in your
mind. You need to define some attributes with them. Such as costume, make up and other
things. In this same way you add attributes to your objects such as width, height and others.
- This defines what the actor will look like and how they will appear but it still does not place
them where they need to be.
18. Analogy - Creating Objects then Adding to Window
Add the actor to the stage/
camera
Add object to the window or view
var actor = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
window.add(actor);
image.addEventListener('click', function(){
alert('This is a test');
})
Compare to Actors on Stage/Camera
- The final step is to place the actor on stage or on camera. This is where they need to be to
be viewed by the audience. If they are not on the camera then your end audience will not see
them.
- After this is done you then define what the actor should do with the script or in the case of
coding javascript or adding event listeners.
19. Analogy - Creating Objects then Adding to Window
<Alloy>
<Window class="container">
<ImageView id="actor" onClick="doClick"
image="images/Checkmark.png" />
</Window>
</Alloy>
XML - Get the Actor
TSS - Define Attributes
"#actor":{
top: 10,
width: 260,
height: 95
function doClick(e) {
//Tell the actor what to do
}
JS - Script what the actor does
}
Compare to Actors on Stage/Camera
- Alloy works the same. You add the object in the XML then add attributes in the TSS file and
finally add the script within the JS file.
20. Creating a View
Classic Code:
app.js
//Create the view and it's attributes
var view1 = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
//Add the view to the window or view
window.add(view1);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'white'
});
//Create the view and it's attributes
var view1 = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
//Add the view to the window or view
window.add(view1);
window.open();
22. Adding Objects to a View
Classic Code:
app.js
//Create the view and it's attributes
var view1 = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
//Create the object and its attributes
var view2 = Ti.UI.createView({
backgroundColor:'black',
width: 20,
height: 20,
top: 10
});
//Add the second object to the view not the window
view1.add(view2);
//Add the view to the window or view
window.add(view1);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create the view and it's attributes
var view1 = Ti.UI.createView({
backgroundColor:'red',
width:100,
height:100,
top:20
});
//Create the object and its attributes
var view2 = Ti.UI.createView({
backgroundColor:'black',
width: 20,
height: 20,
top: 10
});
//Add the second object to the view not the window
view1.add(view2);
//Add the view to the window or view
window.add(view1);
window.open();
24. Adding Content to Views - Creating Labels
Classic Code:
app.js
//This is the code to create a label
var label1 = Ti.UI.createLabel({
color:'#999',
text:'This is a text',
font:{fontSize:20, fontfamily:'Helvetica Neue'},
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
width: Ti.UI.SIZE, height: Ti.UI.SIZE,
})
//You then add your label to the window or view
window.add(label1)
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//This is the code to create a label
var label1 = Ti.UI.createLabel({
color:'#999',
text:'This is a text',
font:{fontSize:20, fontfamily:'Helvetica Neue'},
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
width: Ti.UI.SIZE, height: Ti.UI.SIZE,
})
//You then add your label to the window or view
window.add(label1)
window.open();
27. Adding Content to Views - Image View
Classic Code:
app.js
//Create the image and point to the file in a folder
var image = Ti.UI.createImageView({
image: 'images/Checkmark.png',
//You can also add position or other attributes
})
//Add the image to the window or view
window.add(image);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create the image and point to the file in a folder
var image = Ti.UI.createImageView({
image: 'images/Checkmark.png',
//You can also add position or other attributes
})
//Add the image to the window or view
window.add(image);
window.open();
28. Adding Content to Views - Image View
Alloy Code:
index.xml
<ImageView id="image" image="images/Checkmark.png" />
index.tss
"#image": {
}
**NOTE**
Alloy assets have to be within the assets folder
//NOTE: Alloy assets have to be within the assets folder
Alloy:
index.xml:
<Alloy>
<Window class="container">
<ImageView id="image" image="images/Checkmark.png" />
</Window>
</Alloy>
index.tss:
(Optional)
"#image": {
}
29. Adding Event Listeners
Classic Code:
app.js
var image = Ti.UI.createImageView({
image: 'images/Checkmark.png',
})
window.add(image);
image.addEventListener('click', function(){
alert('This is a test');
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var image = Ti.UI.createImageView({
image: 'images/Checkmark.png',
})
window.add(image);
//You can add an event listener to any view, window or object
image.addEventListener('click', function(){
alert('This is a test');
})
window.open();
30. Adding Event Listeners
Alloy Code:
index.xml
<ImageView id="image" onClick="doClick" image="images/
Checkmark.png" />
index.js
function doClick(e) {
alert("This is a test");
}
Alloy:
index.xml:
<Alloy>
<Window class="container">
<ImageView id="image" onClick="doClick" image="images/Checkmark.png" />
</Window>
</Alloy>
index.js:
function doClick(e) {
alert("This is a test");
}
$.index.open();
31. Adding Content to Views - Creating a Button
Classic Code:
app.js
var button = Ti.UI.createButton({
title:'Click Me',
top: 10,
width: 100,
height: 50
});
window.add(button);
button.addEventListener('click', function(){
alert('You clicked me')
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create the button and establish it's attributes
var button = Ti.UI.createButton({
title:'Click Me',
top: 10,
width: 100,
height: 50
});
//Add the button to the window or view
window.add(button);
//Add the function to the button that runs when clicked
button.addEventListener('click', function(){
alert('You clicked me')
})
window.open();
32. Adding Content to Views - Creating a Button
Alloy Code:
index.xml
<Button id="button" onClick="doClick" title="Click
Me" />
index.js
function doClick(e) {
alert("This is a test");
}
index.tss
"#button":{
top: 10,
width: 100,
height: 50
}
Alloy:
index.xml:
<Alloy>
<Window class="container">
<Button id="button" onClick="doClick" title="Hello"/>
</Window>
</Alloy>
index.tss:
".container": {
backgroundColor:"white"
},
"#button":{
top: 10,
width: 100,
height: 50
}
index.js:
function doClick(e) {
alert("This is a test");
}
$.index.open();
34. Adding Content to Views - Creating a CUSTOM Button
Alloy Code:
index.xml
<Button id="button" onClick="doClick" title="Hello"/>
index.js
function doClick(e) {
alert("Hello");
}
index.tss
"#button":{
backgroundImage: 'images/btn_up.png',
backgroundSelectedImage: 'images/btn_down.png',
top: 10,
width: 260,
height: 95
} **NOTE**
Alloy assets have to be within the assets folder
//NOTE: Alloy assets have to be within the assets folder
Alloy:
index.xml:
<Alloy>
<Window class="container">
<Button id="button" onClick="doClick" title="Hello"/>
</Window>
</Alloy>
index.tss:
".container": {
backgroundColor:"white"
},
"#button":{
backgroundImage: 'images/btn_up.png',
backgroundSelectedImage: 'images/btn_down.png',
top: 10,
width: 260,
height: 95
}
index.js:
function doClick(e) {
alert("Hello");
}
$.index.open();
35. Adding Content to Views - Creating a Switch
Classic Code:
app.js
var basicSwitch = Ti.UI.createSwitch({
value:true,
})
window.add(basicSwitch);
basicSwitch.addEventListener('change', function(){
alert('Switch Value: ' + basicSwitch.value)
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create the switch and establish all the attributes for the switch
var basicSwitch = Ti.UI.createSwitch({
value:true,
})
//Add the switch to the window or view
window.add(basicSwitch);
//Add an event listener to fire when the switch changes values
basicSwitch.addEventListener('change', function(){
alert('Switch Value: ' + basicSwitch.value)
})
window.open();
37. Adding Content to Views - Creating a Text Field
Classic Code:
app.js
var textField = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color:’#336699’,
top: 10, left: 10,
width: 250, height: 60
})
window.add(textField);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//You first create the text field and add all of it's attributes
var textField = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
top: 10, left: 10,
width: 250, height: 60
})
//Add the textfield to the window or the view
window.add(textField);
window.open();
39. Creating Tables
Classic Code:
app.js
var tableData = [ {title:'Apples'},
{title: 'Bananas'}, {title: 'Bananas'},
{title: 'Potatoes'} ];
var table = Ti.UI.createTableView({
data: tableData
})
window.add(table);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Establish the data for the table - This is just one possible way
var tableData = [{title:'Apples'}, {title: 'Bananas'}, {title: 'Bananas'}, {title: 'Potatoes'} ];
//Create the table view and assign the data from the table data array
var table = Ti.UI.createTableView({
data: tableData
})
//Adding the table view to the window or view
window.add(table);
window.open();
41. Adding Events to Tables
Classic Code:
app.js
table.addEventListener('click', function(e){
if(e.index == 0){
alert('You clicked 1')
} else if (e.index == 1){
alert('You clicked 2')
}
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Establish the data for the table - This is just one possible way
var tableData = [{title:'Apples'}, {title: 'Bananas'}, {title: 'Bananas'}, {title: 'Potatoes'} ];
//Create the table view and assign the data from the table data array
var table = Ti.UI.createTableView({
data: tableData
})
//Adding the table view to the window or view
window.add(table);
//Adding events to the table
table.addEventListener('click', function(e){
//Check to see which table row was clicked and then you run the code for the table row
if(e.index == 0){
alert('You clicked 1')
} else if (e.index == 1){
alert('You clicked 2')
}
})
window.open();
43. Creating Tables Sections
Classic Code:
app.js
var sectionFruit = Ti.UI.createTableViewSection({headerTitle: 'Fruit'});
sectionFruit.add(Ti.UI.createTableViewRow({title:'Apples'}));
sectionFruit.add(Ti.UI.createTableViewRow({title:'Bananas'}));
var sectionVeg = Ti.UI.createTableViewSection({headerTitle: 'Veggies'});
sectionVeg.add(Ti.UI.createTableViewRow({title:'Carrots'}));
sectionVeg.add(Ti.UI.createTableViewRow({title:'Potatoes'}));
var table = Ti.UI.createTableView({
data: [sectionFruit, sectionVeg]
})
window.add(table);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Creating a section for the table. This includes creating a header for the section.
var sectionFruit = Ti.UI.createTableViewSection({headerTitle: 'Fruit'});
//Add rows to this section
sectionFruit.add(Ti.UI.createTableViewRow({title:'Apples'}));
sectionFruit.add(Ti.UI.createTableViewRow({title:'Bananas'}));
//Creating a section for the table. This includes creating a header for the section.
var sectionVeg = Ti.UI.createTableViewSection({headerTitle: 'Vegetables'});
//Add rows to this section
sectionVeg.add(Ti.UI.createTableViewRow({title:'Carrots'}));
sectionVeg.add(Ti.UI.createTableViewRow({title:'Potatoes'}));
//Create the table view and assign the data from the sectionFruit and sectionVeg arrays
var table = Ti.UI.createTableView({
//Assigning both sections to the table
data: [sectionFruit, sectionVeg]
})
//Adding the table view to the window or view
window.add(table);
window.open();
47. Creating a Web View
Classic Code:
app.js
var webView = Ti.UI.createWebView({
url:'http://www.kinetic-media.com/jquery'
});
window.add(webView);
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create a webView - The only attribute it needs is the URL but you can add other attributes.
var webView = Ti.UI.createWebView({
url:'http://www.kinetic-media.com/jquery'
});
//Add the webview to the window or view
window.add(webView);
window.open();
53. Playing Sound
Classic Code:
app.js
var player = Ti.Media.createSound({
url:'audio/start_race.mp3'
**NOTE**
})
player.play();
Classic assets have to be within a folder
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Create the player element and establish the path to the audio
var player = Ti.Media.createSound({
url:'audio/start_race.mp3'
})
//Play the audio file. This can also be within a function
player.play();
window.open();
54. Playing Sound
<Button id="button" onClick="doClick" title="Hello"/>
var player = Ti.Media.createSound({
url:'audio/start_race.mp3'
**NOTE**
Alloy Code:
index.xml
index.js
})
player.play();
Alloy assets have to be within the assets folder
//NOTE: Alloy assets have to be within the assets folder
Alloy:
index.xml:
<Alloy>
<Window class="container">
</Window>
</Alloy>
index.js:
var player = Ti.Media.createSound({
url:'audio/start_race.mp3'
})
player.play();
$.index.open();
55. Playing Sound in a Function
Classic Code:
app.js
var buttonSound = Ti.UI.createButton({
title: 'Play Sound',
width: 100,
height: 50
})
var player = Ti.Media.createSound({
url:'audio/Wrong.mp3'
})
window.add(buttonSound);
buttonSound.addEventListener('click', function(){
player.play();
});
**NOTE**
Classic assets have to be within the a folder
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var buttonSound = Ti.UI.createButton({
title: 'Play Sound',
width: 100,
height: 50
})
//Create the player element and establish the path to the audio
var player = Ti.Media.createSound({
url:'audio/Wrong.mp3'
})
window.add(buttonSound);
//Firing the play audio when the image is tapped
buttonSound.addEventListener('click', function(){
//Play the audio file. This can also be within a function
player.play();
});
window.open();
56. Playing Sound in a Function
Alloy Code:
index.xml
<Button id="button" onClick="doClick" title="Hello"/>
index.js
var player = Ti.Media.createSound({
url:'audio/Wrong.mp3'
})
function doClick(e) {
player.play();
}
**NOTE**
Alloy assets have to be within the assets folder
Alloy:
index.xml:
<Alloy>
<Window class="container">
<Button id="button" onClick="doClick" title="Play Sound"/>
</Window>
</Alloy>
index.js:
var player = Ti.Media.createSound({
url:'audio/Wrong.mp3'
})
function doClick(e) {
player.play();
}
57. Playing Video
Classic Code:
app.js
var videoPlayer = Ti.Media.createVideoPlayer({
url: 'video/Silly_Walks.mp4',
top: 10,
autoplay: false,
height: 230,
width: 300,
mediaControlStyle: Ti.Media.VIDEO_CONTROL_DEFAULT,
scalingMode: Ti.Media.VIDEO_SCALING_ASPECT_FIT
});
window.add(videoPlayer);
**NOTE**
Classic assets have to be within a folder
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Establish the video player and add all the attributes
var videoPlayer = Ti.Media.createVideoPlayer({
//Video Location
url: 'video/Silly_Walks.mp4',
//Video position on the stage
top: 10,
//Audtoplay video
autoplay: false,
height: 230,
width: 300,
//Media controller style
mediaControlStyle: Ti.Media.VIDEO_CONTROL_DEFAULT,
//Video scaling mode
scalingMode: Ti.Media.VIDEO_SCALING_ASPECT_FIT
});
//Add the video player to the window or view
window.add(videoPlayer);
window.open();
58. Playing Video
Alloy Code:
index.xml
<VideoPlayer id="videoPlayer" ns="Ti.Media" url="video/
Silly_Walks.mp4" autoplay="true" />
index.tss
top:2,
height:300,
width:300,
backgroundColor: 'black'
**NOTE**
"#videoPlayer": {
}
Alloy assets have to be within the assets folder
Alloy:
index.xml:
<Alloy>
<Window class="container">
<VideoPlayer id="videoPlayer" ns="Ti.Media" url="video/Silly_Walks.mp4"
autoplay="true" />
</Window>
</Alloy>
index.tss:
"#videoPlayer": {
top:2,
height:300,
width:300,
backgroundColor: 'black'
}
59. Swiping Events
Classic Code:
app.js
window.addEventListener('swipe', function(e){
if(e.direction == 'left'){
alert('You swiped left')
} else if (e.direction == 'right'){
alert('You swiped right')
}
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
//Add event listener to the object you want to swipe
window.addEventListener('swipe', function(e){
//If the swipe is left or right
if(e.direction == 'left'){
alert('You swiped left')
} else if (e.direction == 'right'){
alert('You swiped right')
}
})
window.open();
60. Swiping Events
Alloy Code:
index.xml
<Alloy>
<Window class="container" onSwipe="swipeEvent">
</Window>
</Alloy>
index.tss
function swipeEvent(e){
if(e.direction == 'left'){
alert('You swiped left')
} else if (e.direction == 'right'){
alert('You swiped right')
}
}
Alloy:
index.xml:
<Alloy>
<Window class="container" onSwipe="swipeEvent">
</Window>
</Alloy>
index.js:
function swipeEvent(e){
//If the swipe is left or right
if(e.direction == 'left'){
alert('You swiped left')
} else if (e.direction == 'right'){
alert('You swiped right')
}
}
61. Two Finger Tap
Classic Code:
app.js
window.addEventListener('twofingertap', function(e){
alert('Two fingers')
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'white'
});
//Add event listener to any object but in this example it is the window. To test in simulator
hold down the option key.
window.addEventListener('twofingertap', function(e){
alert('Two fingers')
})
window.open();
62. Two Finger Tap
Alloy Code:
index.xml
<Window class="container" onTwofingertap="twoFingers">
</Window>
index.js
function twoFingers(){
alert("Two fingers");
}
Alloy:
index.xml:
<Alloy>
<Window class="container" onTwofingertap="twoFingers">
</Window>
</Alloy>
index.js:
function twoFingers(){
alert("Two fingers");
}
63. Shake Events
Classic Code:
app.js
Titanium.Gesture.addEventListener('shake', function(e)
{
alert('Stop shaking me')
})
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var label1 = Ti.UI.createLabel({
color:'#999',
text:'Shake the phone',
font:{fontSize:20, fontfamily:'Helvetica Neue'},
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
width: Ti.UI.SIZE, height: Ti.UI.SIZE,
})
//You then add your label to the window or view
window.add(label1)
Titanium.Gesture.addEventListener('shake', function(e){
alert('Stop shaking me')
})
window.open();
65. Shake Events
Classic Code:
app.js
Titanium.Gesture.addEventListener('orientationchange',
function(e){
if(e.orientation == 1){
alert('You are in PORTRAIT');
} else if (e.orientation == 2){
alert('You are in UPSIDE_PORTRAIT');
} else if (e.orientation == 3){
alert('You are in LANDSCAPE_LEFT');
} else if (e.orientation == 4){
alert('You are in LANDSCAPE_RIGHT');
}
});
Titanium.Gesture.addEventListener('orientationchange', function(e){
if(e.orientation == 1){
alert('You are in PORTRAIT');
} else if (e.orientation == 2){
alert('You are in UPSIDE_PORTRAIT');
} else if (e.orientation == 3){
alert('You are in LANDSCAPE_LEFT');
} else if (e.orientation == 4){
alert('You are in LANDSCAPE_RIGHT');
}
});
66. Shake Events
Alloy Code:
index.js
Titanium.Gesture.addEventListener('orientationchange',
function(e){
if(e.orientation == 1){
alert('You are in PORTRAIT');
} else if (e.orientation == 2){
alert('You are in UPSIDE_PORTRAIT');
} else if (e.orientation == 3){
alert('You are in LANDSCAPE_LEFT');
} else if (e.orientation == 4){
alert('You are in LANDSCAPE_RIGHT');
}
});
Titanium.Gesture.addEventListener('orientationchange', function(e){
if(e.orientation == 1){
alert('You are in PORTRAIT');
} else if (e.orientation == 2){
alert('You are in UPSIDE_PORTRAIT');
} else if (e.orientation == 3){
alert('You are in LANDSCAPE_LEFT');
} else if (e.orientation == 4){
alert('You are in LANDSCAPE_RIGHT');
}
});
67. Toolbar
Classic Code:
app.js
var window = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'Main Window'
});
var send = Titanium.UI.createButton({
title: 'Send',
style: Titanium.UI.iPhone.SystemButtonStyle.DONE,
});
var camera = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.CAMERA,
});
var cancel = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.CANCEL
});
var flexSpace = Titanium.UI.createButton({
systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
var toolbar = Titanium.UI.iOS.createToolbar({
items:[send, flexSpace, camera, flexSpace, cancel],
bottom:0,
borderTop:true,
borderBottom:false
});
window.add(toolbar);
window.open();
Classic:
app.js:
var window = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'Main Window'
});
var send = Titanium.UI.createButton({
title: 'Send',
style: Titanium.UI.iPhone.SystemButtonStyle.DONE,
});
var camera = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.CAMERA,
});
var cancel = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.CANCEL
});
var flexSpace = Titanium.UI.createButton({
systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
var toolbar = Titanium.UI.iOS.createToolbar({
items:[send, flexSpace, camera, flexSpace, cancel],
bottom:0,
borderTop:true,
borderBottom:false
});
68. Toolbar
Alloy Code:
index.xml
<Alloy>
<Window class="container">
<Toolbar platform="ios" bottom="0" borderTop="true"
borderBottom="false" barColor="#000">
<!-- The Items tag sets the Toolbar.items property. -->
<Items>
<Button id="send" title="Send"
style="Ti.UI.iPhone.SystemButtonStyle.DONE" />
<FlexSpace/>
<Button id="camera" systemButton="Ti.UI.iPhone.SystemButton.CAMERA" />
<FlexSpace/>
<Button id="cancel" systemButton="Ti.UI.iPhone.SystemButton.CANCEL" />
</Items>
<!-- Place additional views for the Toolbar here. -->
</Toolbar>
<Label id="label">I am Window 1</Label>
</Window>
</Alloy>
Alloy:
index.xml:
<Alloy>
<Window class="container">
<Toolbar platform="ios" bottom="0" borderTop="true" borderBottom="false" barColor="#000">
<!-- The Items tag sets the Toolbar.items property. -->
<Items>
<Button id="send" title="Send" style="Ti.UI.iPhone.SystemButtonStyle.DONE" />
<FlexSpace/>
<Button id="camera" systemButton="Ti.UI.iPhone.SystemButton.CAMERA" />
<FlexSpace/>
<Button id="cancel" systemButton="Ti.UI.iPhone.SystemButton.CANCEL" />
</Items>
<!-- Place additional views for the Toolbar here. -->
</Toolbar>
<Label id="label">I am Window 1</Label>
</Window>
</Alloy>
Or for top
<Toolbar platform="ios" top="0" borderTop="true" borderBottom="false" barColor="#000">
69. Tabbed Bar
Classic Code:
app.js
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var bb1 = Titanium.UI.iOS.createTabbedBar({
labels:['One', 'Two', 'Three'],
backgroundColor:'#336699',
top:50,
style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
height:25,
width:200
});
window.add(bb1);
window.open();
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var bb1 = Titanium.UI.iOS.createTabbedBar({
labels:['One', 'Two', 'Three'],
backgroundColor:'#336699',
top:50,
style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
height:25,
width:200
});
window.add(bb1);
window.open();
70. Tabbed Bar
Alloy Code:
index.xml
<Alloy>
<Window class="container">
<TabbedBar id="bb1" platform="ios" backgroundColor="#336699"
top="50" height="25" width="200"
style="Titanium.UI.iPhone.SystemButtonStyle.BAR">
<Labels>
<Label> One</Label>
<Label> Two</Label>
<Label> Three</Label>
</Labels>
</TabbedBar>
</Window>
</Alloy>
Alloy:
index.xml:
<Alloy>
<Window class="container">
<TabbedBar id="bb1" platform="ios" backgroundColor="#336699" top="50" height="25"
width="200" style="Titanium.UI.iPhone.SystemButtonStyle.BAR">
<!-- The Labels tag sets the TabbedBar.labels property. -->
<Labels>
<!-- Specify text with node text or the title attribute. -->
<!-- Can also specify the enabled, image and width attributes. -->
<Label> One</Label>
<Label> Two</Label>
<Label> Three</Label>
</Labels>
<!-- Place additional views for the TabbedBar here. -->
</TabbedBar>
</Window>
</Alloy>
71. Picker
Classic Code:
app.js
var picker = Ti.UI.createPicker({
top:50
});
var data = [];
data[0]=Ti.UI.createPickerRow({title:'Bananas'});
data[1]=Ti.UI.createPickerRow({title:'Strawberries'});
data[2]=Ti.UI.createPickerRow({title:'Mangos'});
data[3]=Ti.UI.createPickerRow({title:'Grapes'});
picker.add(data);
picker.selectionIndicator = true;
win.add(picker);
win.open();
// must be after picker has been displayed
picker.setSelectedRow(0, 2, false); // select Mangos
Classic:
app.js: Single Picker
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow({
exitOnClose: true,
layout: 'vertical'
});
var picker = Ti.UI.createPicker({
top:50
});
var data = [];
data[0]=Ti.UI.createPickerRow({title:'Bananas'});
data[1]=Ti.UI.createPickerRow({title:'Strawberries'});
data[2]=Ti.UI.createPickerRow({title:'Mangos'});
data[3]=Ti.UI.createPickerRow({title:'Grapes'});
picker.add(data);
picker.selectionIndicator = true;
win.add(picker);
win.open();
// must be after picker has been displayed
picker.setSelectedRow(0, 2, false); // select Mangos
app.js: Multi-Column Picker
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow({
73. Custom Alerts
Classic Code:
app.js
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var dialog = Ti.UI.createAlertDialog({
message: 'The file has been deleted',
ok: 'Okay',
title: 'File Deleted'
});
window.addEventListener('click', function(e){
dialog.show();
});
window.open();
Classic:
Option 1
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
window.addEventListener('click', function(e){
alert('The file has been deleted');
});
window.open();
Option 2
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var dialog = Ti.UI.createAlertDialog({
message: 'The file has been deleted',
ok: 'Okay',
title: 'File Deleted'
});
window.addEventListener('click', function(e){
dialog.show();
});
window.open();
74. Custom Alerts
Classic Code:
app.js
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var dialog = Ti.UI.createAlertDialog({
cancel:1,
buttonNames: ['Confirm', 'Cancel', 'Help'],
message: 'The file has been deleted',
title: 'File Deleted'
});
window.addEventListener('click', function(e){
dialog.show();
});
dialog.addEventListener('click', function(e){
if(e.index === e.source.cancel){
Ti.API.info('The cancel button was clicked');
} else if (e.index === 1){
Ti.API.info('The help button was clicked');
}
});
window.open();
Classic:
Option 3 (Three Buttons):
var window = Titanium.UI.createWindow({
backgroundColor:'#ffffff'
});
var dialog = Ti.UI.createAlertDialog({
cancel:1,
buttonNames: ['Confirm', 'Cancel', 'Help'],
message: 'The file has been deleted',
title: 'File Deleted'
});
window.addEventListener('click', function(e){
dialog.show();
});
dialog.addEventListener('click', function(e){
if(e.index === e.source.cancel){
Ti.API.info('The cancel button was clicked');
} else if (e.index === 1){
Ti.API.info('The help button was clicked');
}
});
window.open();
75. Opening Up Another Page - Part 1
Classic Code:
app.js
var window = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'Main Window'
});
//Add button to first window
var b3 = Titanium.UI.createButton({
title:'Open New Win',
width:200,
height:40,
top:110
});
window.add(b3);
//Event listener to open new window
b3.addEventListener('click', function()
{
var w = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'New Window',
barColor:'black',
url:'new_window.js'
});
w.open();
});
window.open();
Classic:
var window = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'Main Window'
});
//Add button to first window
var b3 = Titanium.UI.createButton({
title:'Open New Win',
width:200,
height:40,
top:110
});
window.add(b3);
//Event listener to open new window
b3.addEventListener('click', function()
{
var w = Titanium.UI.createWindow({
backgroundColor:'#336699',
title:'New Window',
barColor:'black',
url:'new_window.js'
});
w.open();
});
window.open();
76. Opening Up Another Page - Part 2
Classic Code:
new_window.js
var win = Ti.UI.currentWindow;
var label = Ti.UI.createLabel({
color:'#fff',
text:'test label on new window',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto',
top: 20
});
label.addEventListener('click', function(){
win.close();
})
win.add(label);
Classic:
var win = Ti.UI.currentWindow;
var label = Ti.UI.createLabel({
color:'#fff',
text:'test label on new window',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto',
top: 20
});
label.addEventListener('click', function(){
win.close();
})
win.add(label);
79. Opening Up Another Page - Part 3 JS
Alloy Code:
index.js
function showWin1(e) {
var w=Alloy.createController('win2').getView();
w.open();
}
$.index.open();
win2.js
function closeme(){
$.container.close();
};
Alloy:
index.js:
function showWin1(e) {
var w=Alloy.createController('win2').getView();
w.open();
}
$.index.open();
win2.js:
function closeme(){
$.container.close();
};