<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cssgallery.info &#187; Development tools</title>
	<atom:link href="http://cssgallery.info/category/development-tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://cssgallery.info</link>
	<description>Resources for web and mobile developers</description>
	<lastBuildDate>Mon, 09 Jan 2012 18:54:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Seven days with Titanium – day 5 – GPS and Maps</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 18:13:53 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[add a route]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[forwardGeocoder]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[getCurrentHeading]]></category>
		<category><![CDATA[getCurrentPosition]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[Maproute]]></category>
		<category><![CDATA[mapview.addAnnotation]]></category>
		<category><![CDATA[reverseGeocoder]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[Titanium.Geolocation]]></category>
		<category><![CDATA[Titanium.Map]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1660</guid>
		<description><![CDATA[This doesn't need any kind of introduction. You can use the location on the devices to find the user's place and act accordingly. One thing good to know is that this needs the approval of the user, and for iOs &#62; 3.2 you need to set a <strong>purpose</strong> property to let Apple know what you need it for.

<pre lang="javascript">Titanium.Geolocation.purpose = "GPS user coordinates";</pre>

<h2>How geolocation works?</h2>

]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-5-gps-and-maps%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-5-gps-and-maps%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This doesn&#8217;t need any kind of introduction. You can use the location on the devices to find the user&#8217;s place and act accordingly. One thing good to know is that this needs the approval of the user, and for iOs &gt; 3.2 you need to set a <strong>purpose</strong> property to let Apple know what you need it for.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Titanium.<span style="color: #660066;">Geolocation</span>.<span style="color: #660066;">purpose</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;GPS user coordinates&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>How geolocation works</h2>
<p>When you start the app <strong>the coordinates are the ones cached on the device</strong>, in other words the ones detected at the last time you used the geolocation on your device. Detecting the current position takes time so if you have an app based on geolocation you might want to take this in account to improve the user experience and avoid to get false results.</p>
<p><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Geolocation-module">Titanium.Geolocation</a> provides the needed methods to manage geolocation.</p>
<p>In general the most used method is <strong>getCurrentPosition</strong> that gives you the current position and fires once and the <strong>location</strong> event that triggers repeatedly on the location change. To be more clear think you want to log a route the user follow: when the app starts you get its start position then while walking you can get the points of the route with a adjustable granularity.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
Titanium.<span style="color: #660066;">Geolocation</span>.<span style="color: #660066;">distanceFilter</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// set the granularity of the location event</span>
&nbsp;
	Titanium.<span style="color: #660066;">Geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// manage the error</span>
                <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> longitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> latitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> altitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">altitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> heading <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">heading</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> accuracy <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">accuracy</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> speed <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">speed</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">timestamp</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> altitudeAccuracy <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">altitudeAccuracy</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// we use the above data the way we need it</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	Titanium.<span style="color: #660066;">Geolocation</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'location'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// manage the error</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> longitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> latitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> altitude <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">altitude</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> heading <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">heading</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> accuracy <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">accuracy</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> speed <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">speed</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">timestamp</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> altitudeAccuracy <span style="color: #339933;">=</span> e.<span style="color: #660066;">coords</span>.<span style="color: #660066;">altitudeAccuracy</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #006600; font-style: italic;">// again we use the gathered data</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are some other methods that can be used like <strong>reverseGeocoder</strong>, <strong>forwardGeocoder</strong>, and <strong>getCurrentHeading</strong> &#8211;  for compass. Because not all the devices have the GPS location (like iPod) or the compass and because the user can have the location services disabled you have to check if it exists, if it&#8217;s enabled and then let the user know about this.</p>
<p>As the documentation page is not complete yet, I suggest you to <strong>take a look</strong> at the <a href="http://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/geolocation.js">geolocation example</a> that shows very well how to use this.</p>
<h2>Maps</h2>
<p>The maps are created using the <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Map-module">Titanium.Map</a> methods. The maps are a little bit complicated but allows you to do some real nice interfaces. You can use a <strong>Mapview</strong> and add a route to it, or pins(markers) and to the pins to attach popups with images, titles, even &#8220;links&#8221; o another windows for a more detailed view of the specified place or whatever you may need.</p>
<h3>Create a map</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> mapview <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">createView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	mapType<span style="color: #339933;">:</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">STANDARD_TYPE</span><span style="color: #339933;">,</span>
	region<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">33.74511</span><span style="color: #339933;">,</span> longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">84.38993</span><span style="color: #339933;">,</span> latitudeDelta<span style="color: #339933;">:</span><span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> longitudeDelta<span style="color: #339933;">:</span><span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	animate<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	regionFit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	userLocation<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nothing complicated: the constructor can receive different arguments including the <strong>annotations</strong> (pins), but of course you will have to declare it first.</p>
<p>And this is done like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> apple <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">createAnnotation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">37.33168900</span><span style="color: #339933;">,</span>
	longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">122.03073100</span><span style="color: #339933;">,</span>
	title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Steve Jobs&quot;</span><span style="color: #339933;">,</span>
	subtitle<span style="color: #339933;">:</span><span style="color: #3366CC;">'Cupertino, CA'</span><span style="color: #339933;">,</span>
	pincolor<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">ANNOTATION_GREEN</span><span style="color: #339933;">,</span>
	animate<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	rightButton<span style="color: #339933;">:</span> <span style="color: #3366CC;">'apple_logo.jpg'</span><span style="color: #339933;">,</span>
	myid<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span> <span style="color: #006600; font-style: italic;">// CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> atlanta <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">createAnnotation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">33.74511</span><span style="color: #339933;">,</span>
		longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">84.38993</span><span style="color: #339933;">,</span>
		title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Atlanta, GA&quot;</span><span style="color: #339933;">,</span>
		subtitle<span style="color: #339933;">:</span><span style="color: #3366CC;">'Atlanta Braves Stadium<span style="color: #000099; font-weight: bold;">\n</span>foo'</span><span style="color: #339933;">,</span>
		animate<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		leftButton<span style="color: #339933;">:</span><span style="color: #3366CC;">'atlanta.jpg'</span><span style="color: #339933;">,</span>
		rightButton<span style="color: #339933;">:</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">SystemButton</span>.<span style="color: #660066;">DISCLOSURE</span><span style="color: #339933;">,</span>
		myid<span style="color: #339933;">:</span><span style="color: #CC0000;">3</span> <span style="color: #006600; font-style: italic;">// CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and the map constructor gets</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	annotations<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>atlanta<span style="color: #339933;">,</span>apple<span style="color: #009900;">&#93;</span></pre></div></div>

<p>or later you can add this annotation using</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	mapview.<span style="color: #660066;">addAnnotation</span><span style="color: #009900;">&#40;</span>atlanta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Map events</h3>
<p>One thing you need to know is that <strong>the events are registered at the map level</strong>. So you cannot get a click event on a pin. You will have to get the click event of the mapview ad use the <strong>clicksource</strong> and <strong>the custom property (myId) </strong> properties to detect the annotation and what has been clicked (pin, leftbutton, rightbutton, title, subtitle)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">source</span>.<span style="color: #660066;">myid</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">3</span> <span style="color: #339933;">&amp;&amp;</span> evt.<span style="color: #660066;">clicksource</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'rightButton'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// do your stuff</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>MapRoute</h3>
<p>Another thing you can do with a map is to <strong>add a route</strong> (iOs only for now). Basically you can draw a line on the map specifying the points (gpd point) it needs to pass through.</p>
<p>A route can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> route <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;some name&quot;</span><span style="color: #339933;">,</span>
	points<span style="color: #339933;">:</span>points<span style="color: #339933;">,</span>
	color<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Where  <strong>points</strong> it&#8217;s an array of objects containing latitude and longitude coordinates.</p>
<p><img src="http://cssgallery.info/wp-content/uploads/2011/02/gmap-capture-on-iphone.jpg" alt="screen capture showing a google map on an iphone screen" title="Maproute with Titanium" width="320" height="460" class="alignright size-full wp-image-1947" /></p>
<h3>Example &#8211; add a route to a Mapview</h3>
<p>Below is an example that adds a route to a mapview using some readymade coordinates. It will generate something similar to the screenshot in the right.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> center <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">42.30</span><span style="color: #339933;">,</span>longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">71.18</span><span style="color: #339933;">,</span>latitudeDelta<span style="color: #339933;">:</span><span style="color: #CC0000;">0.03</span><span style="color: #339933;">,</span> longitudeDelta<span style="color: #339933;">:</span><span style="color: #CC0000;">0.1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> mapview <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">createView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	mapType<span style="color: #339933;">:</span> Titanium.<span style="color: #660066;">Map</span>.<span style="color: #660066;">STANDARD_TYPE</span><span style="color: #339933;">,</span>
	region<span style="color: #339933;">:</span> center<span style="color: #339933;">,</span>
	animate<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	regionFit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
points <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
	<span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">42.31</span><span style="color: #339933;">,</span>longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">71.11</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">42.32</span><span style="color: #339933;">,</span>longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">71.13</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">42.31</span><span style="color: #339933;">,</span>longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">71.22</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#123;</span>latitude<span style="color: #339933;">:</span><span style="color: #CC0000;">42.28</span><span style="color: #339933;">,</span>longitude<span style="color: #339933;">:-</span><span style="color: #CC0000;">71.26</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// route object</span>
<span style="color: #003366; font-weight: bold;">var</span> route <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;some name&quot;</span><span style="color: #339933;">,</span>
	points<span style="color: #339933;">:</span>points<span style="color: #339933;">,</span>
	color<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#00f&quot;</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// add a route</span>
mapview.<span style="color: #660066;">addRoute</span><span style="color: #009900;">&#40;</span>route<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>What next?</h2>
<p>A good exercise would be maybe to create an application that it&#8217;s watching your position while you are driving or walking and display it on a map. All you would have to do is to use the <strong>location</strong> event and use the returned value to append a new item to the <strong>points</strong> array.</p>
<p>Anybody wanna try ? <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-5-gps-y-mapas/" rel="me">Siete días con Titanium – día 5 – GPS y Mapas</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1660"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-5-gps-and-maps%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-5-gps-and-maps%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-5-gps-and-maps%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/seven-days-with-titanium-day-7-orientation-and-accelerometer/' rel='bookmark' title='Seven days with Titanium &#8211; day 7 &#8211; orientation and accelerometer'>Seven days with Titanium &#8211; day 7 &#8211; orientation and accelerometer</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-4-media-images-movies-and-sounds/' rel='bookmark' title='Seven days with Titanium – day 4 – Media – images, movies and sounds'>Seven days with Titanium – day 4 – Media – images, movies and sounds</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-6%e2%80%93animations-and-transforms/' rel='bookmark' title='Seven days with Titanium – day 6 – animations and transforms'>Seven days with Titanium – day 6 – animations and transforms</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Seven days with Titanium – day 4 – Media – images, movies and sounds</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-4-media-images-movies-and-sounds/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-4-media-images-movies-and-sounds/#comments</comments>
		<pubDate>Sat, 06 Nov 2010 14:52:15 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[ImageView]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[Titanium Appcelerator media]]></category>
		<category><![CDATA[Titanium Appcelerator movies]]></category>
		<category><![CDATA[Titanium Appcelerator sounds]]></category>
		<category><![CDATA[Titanium Appcelerator videos]]></category>
		<category><![CDATA[Titanium.Media.createSound]]></category>
		<category><![CDATA[Titanium.Media.createVideoPlayer]]></category>
		<category><![CDATA[Titanium.Media.showCamera]]></category>
		<category><![CDATA[Titanium.UI.createImage]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1654</guid>
		<description><![CDATA[The day 4 comes very late because it was a very busy period so I had no time to continue the tutorials. 
Ok, so we should treat in this tutorial the media part of Appcelerator. This includes images - display and capture them with the camera and how to play movie and sounds.

<h2>Display images with Appcelerator</h2>

To display an image we use the ImageView. The image view has some simple arguments, like any view in the Titanium framework. I won't repeat them except for the important one. Obviously the source if the image is the one we are interested in set by the <strong>image</strong> argument, and accepts local or remote files.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-4-media-images-movies-and-sounds%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-4-media-images-movies-and-sounds%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The day 4 comes very late because it was a very busy period so I had no time to continue the tutorials.<br />
Ok, so we should treat in this tutorial the media part of Appcelerator. This includes images &#8211; display and capture them with the camera &#8211; and how to play movie and sounds.</p>
<h2>Display images with Appcelerator</h2>
<p>To display an image we use the ImageView. The image view has some simple arguments, like any view in the Titanium framework. I won&#8217;t repeat them except for the important one. Obviously the source if the image is the one we are interested in set by the <strong>image</strong> argument, and accepts local or remote files.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> the_img <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createImageView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
image<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;test.jpg&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>How does Titanium treat the path for local images? The path of the files are simply relative to the <strong>Resources</strong> folder. So you can use any folder/subfolder under the Resources one to locate the images of your application.</p>
<p>For the remote images a simple url to the image it&#8217;s enough.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> the_img <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createImageView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
image<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://example.com/test.png&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Titanium accepts all the formats the targeted OS supports.</p>
<h2>How to take pictures using the camera?</h2>
<p>The method you have to use is <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.showCamera-method.html"><strong>Titanium.Media.showCamera</strong></a> that will display the OS interface for taking photos and expose the <strong>3 possible events</strong> (success, cancel, error) to us, so we can decide what to do next.</p>
<p>The function triggered by the success event receives as argument an object containing the media (the image itself) and the width and height of it. Using this data you can create a ImageView and add it to your window for example.</p>
<p>Below is a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> win <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span><span style="color: #339933;">;</span>
Titanium.<span style="color: #660066;">Media</span>.<span style="color: #660066;">showCamera</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	success<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> image <span style="color: #339933;">=</span> event.<span style="color: #660066;">media</span><span style="color: #339933;">;</span>
&nbsp;
		Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Our type was: '</span><span style="color: #339933;">+</span>event.<span style="color: #660066;">mediaType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">mediaType</span> <span style="color: #339933;">==</span> Ti.<span style="color: #660066;">Media</span>.<span style="color: #660066;">MEDIA_TYPE_PHOTO</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> imageView <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createImageView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>width<span style="color: #339933;">:</span>win.<span style="color: #660066;">width</span><span style="color: #339933;">,</span>height<span style="color: #339933;">:</span>win.<span style="color: #660066;">height</span><span style="color: #339933;">,</span>image<span style="color: #339933;">:</span>event.<span style="color: #660066;">media</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>imageView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;got the wrong type back =&quot;</span><span style="color: #339933;">+</span>event.<span style="color: #660066;">mediaType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	cancel<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You canceled the action.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	error<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// create alert</span>
		<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAlertDialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Camera'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// set message</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #660066;">code</span> <span style="color: #339933;">==</span> Titanium.<span style="color: #660066;">Media</span>.<span style="color: #660066;">NO_CAMERA</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			a.<span style="color: #660066;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Please run this test on device'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			a.<span style="color: #660066;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Unexpected error: '</span> <span style="color: #339933;">+</span> error.<span style="color: #660066;">code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// show alert</span>
		a.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	saveToPhotoGallery<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	allowEditing<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	mediaTypes<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>Ti.<span style="color: #660066;">Media</span>.<span style="color: #660066;">MEDIA_TYPE_VIDEO</span><span style="color: #339933;">,</span>Ti.<span style="color: #660066;">Media</span>.<span style="color: #660066;">MEDIA_TYPE_PHOTO</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Like the previous examples this one is found in the <a href="http://github.com/appcelerator/titanium_mobile/downloads">Kitchensink</a> provided by Titanium.</p>
<h2>Basic sounds with Appcelerator</h2>
<p>I will talk only about the basic sounds that are located on the device and can be used as games sounds or as sounds in your application.</p>
<p>The sounds are loaded by the method</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sound <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Media</span>.<span style="color: #660066;">createSound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	url<span style="color: #339933;">:</span><span style="color: #3366CC;">'cricket.wav'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>From here you can <strong>play</strong>, <strong>stop</strong>, <strong>pause</strong>, <strong>reset</strong>, and <strong>set the volume</strong> of the sound with the methods presented in the documentation page of <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.Sound-object">Titanium.Media.Sound</a>.</p>
<p>Each sound has its events, the most notable being the <strong>complete</strong> one that triggers when the sound finished to play.</p>
<p>If the sound file is big you should use the <strong>preload</strong> set to <strong>true</strong> when you build the sound. If you don&#8217;t do this the sound won&#8217;t start instantly when you call the <strong>play</strong> method.</p>
<p>One thing that you need to pay attention to is to <strong>release</strong> the sound if you don&#8217;t need it anymore. For example you play an intro sound only when the user starts the app, then after the sounds completes, you have to release it to free some memory on the device.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> intro_sound <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Media</span>.<span style="color: #660066;">createSound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	url<span style="color: #339933;">:</span><span style="color: #3366CC;">'cricket.wav'</span><span style="color: #339933;">,</span>
	preload<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
intro_sound.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'complete'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> intro_sound.<span style="color: #660066;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intro_sound.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//... more code here</span></pre></div></div>

<p>The above code will play the sound and autorelease it when it ends.</p>
<h2>Play Videos</h2>
<p>Using videos is similar with using sounds.  You will have to create a  player using the <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.createVideoPlayer-method.html"><strong>Titanium.Media.createVideoPlayer</strong></a> method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> videoURL <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://movies.apple.com/media/us/ipad/2010/tours/apple-ipad-video-us-20100127_r848-9cie.mov'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> activeMovie <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Media</span>.<span style="color: #660066;">createVideoPlayer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	url<span style="color: #339933;">:</span> videoURL<span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span>
	left<span style="color: #339933;">:</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span>
	backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#0f0'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
view.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>activeMovie<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
activeMovie.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The url of the videoplayer can be a remote or a local file but you have to take care to be supported by the OS.</p>
<p>On the <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.VideoPlayer-object.html">videoplayer documentation page</a> you will find more about the properties, methods and events supported by this control.</p>
<p>A notable thing is that in the SDK > 3.2 you can embed the videoplayer in any view and play it here without the need of making it fullscreen as in the next screenshot: </p>
<p><img src="http://cssgallery.info/wp-content/uploads/2010/11/embeded_titanium_videoplayer.png" alt="embeded_titanium_videoplayer" title="embeded titanium videoplayer" width="357" height="361" class="alignnone size-full wp-image-1678" /></p>
<p>That&#8217;s it for today, see you on Day 5.</p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-4-media-imagenes-peliculas-y-sonidos/" rel="me">Siete días con Titanium – día 4 – Media – Imágenes, películas y sonidos</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1654"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-4-media-images-movies-and-sounds%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-4-media-images-movies-and-sounds%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-4-media-images-movies-and-sounds%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/seven-days-with-titanium-day-0-introduction/' rel='bookmark' title='Seven days with Titanium &#8211; day 0 &#8211; introduction'>Seven days with Titanium &#8211; day 0 &#8211; introduction</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/' rel='bookmark' title='Seven days with Titanium – day 5 – GPS and Maps'>Seven days with Titanium – day 5 – GPS and Maps</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-1-windows-views-navigation-and-tabs/' rel='bookmark' title='Seven days with Titanium – day 1 – windows, views, navigation and tabs'>Seven days with Titanium – day 1 – windows, views, navigation and tabs</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-4-media-images-movies-and-sounds/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
<enclosure url="http://movies.apple.com/media/us/ipad/2010/tours/apple-ipad-video-us-20100127_r848-9cie.mov" length="322" type="video/quicktime" />
		</item>
		<item>
		<title>Seven days with Titanium – day 3 &#8211; ajax(xhr) and webviews</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-3-ajaxxhr-and-webviews/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-3-ajaxxhr-and-webviews/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 14:58:59 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[httpclient]]></category>
		<category><![CDATA[iphone titanium httpclient]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[JSON.parse]]></category>
		<category><![CDATA[JSON.stringify]]></category>
		<category><![CDATA[titanium ajax]]></category>
		<category><![CDATA[titanium webview]]></category>
		<category><![CDATA[Titanium.Network.online]]></category>
		<category><![CDATA[Titanium.UI.createWebView]]></category>
		<category><![CDATA[Titanium.XML]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yql]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1626</guid>
		<description><![CDATA[In "day 3" we will learn <strong>how to make our application communicate with a webserver</strong>. There are 2 ways we can do this: one is to use a <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.WebView-object">webview</a></strong> and point it to a real web page or using the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object">HttpClient</a></strong> that Appcelerator provides us.

<h2>The WebView</h2>
What is <strong>WebView</strong>? A WebView is nothing more than a chromeless browser or, if you want, a "naked" browser (it misses the interface we're used to) that allows us to open remote or local webpages and display it in our apps.

You can use the webview for many purposes starting from displaying some nice html/css formatted text to building your own in-app browser. One thing you must NOT forget is that the webview it's very expensive for the device (in terms of CPU load and memory usage) so try to use it only when it's really really needed.

The webview is created using the function <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.createWebView-method.html">Titanium.UI.createWebView</a></strong>. Some things to specify about it would be that you have to choose between managing the touch events as a Titanium entity, or let the webpage do this. You cannot have both in the same time.

Two <strong>events</strong> that deserve more attention are <strong>beforeLoad</strong> and <strong>load</strong> that can be used to show and hide a <em>"loading..."</em> screen from the moment you try to load a remote page until the webpage is fully loaded.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-3-ajaxxhr-and-webviews%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-3-ajaxxhr-and-webviews%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In &#8220;day 3&#8243; we will learn <strong>how to make our application communicate with a webserver</strong>. There are 2 ways we can do this: one is to use a <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.WebView-object">webview</a></strong> and point it to a real web page or using the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object">HttpClient</a></strong> that Appcelerator provides us.</p>
<h2>The WebView</h2>
<p>What is <strong>WebView</strong>?  A WebView is nothing more than a chromeless browser or, if you want, a &#8220;naked&#8221; browser (it misses the interface we&#8217;re used to) that allows us to open remote or local webpages and display it in our apps.</p>
<p>You can use the webview for many purposes starting from displaying some nice html/css formatted text to building your own in-app browser. One thing you must NOT forget is that the webview it&#8217;s very expensive for the device (in terms of CPU load and memory usage) so try to use it only when it&#8217;s really really needed.</p>
<p>The webview is created using the function <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.createWebView-method.html">Titanium.UI.createWebView</a></strong>. Some things to specify about it would be that you have to choose between managing the touch events as a Titanium entity, or let the webpage do this. You cannot have both in the same time.</p>
<p>Two <strong>events</strong> that deserve more attention are <strong>beforeLoad</strong> and <strong>load</strong> that can be used to show and hide a <em>&#8220;loading&#8230;&#8221;</em> screen from the moment you try to load a remote page until the webpage is fully loaded.</p>
<h2>The HttpClient</h2>
<p>If you need to communicate with a server on a lower level (REST APIs, your own server implementation, etc) you can use the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object.html">HttpClient</a></strong> that is equivalent of XHR (or ajax) in web.</p>
<p>When you use <strong>HttpClient</strong> you need to take in account two things:</p>
<ol>
<li>that the call is made asynchronous so your app logic needs to work this way </li>
<li>that you need to make it in a certain sequence: <strong>create</strong>, <strong>set events</strong>, <strong>open</strong>, <strong>send</strong>.</li>
</ol>
<p>So you need to create the <strong>HttpClient</strong>, set its onload and onerror events (that triggers when the call reply is received or an error occurs), open the channel and send the request &#8211;  with or without parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Network</span>.<span style="color: #660066;">createHTTPClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	Titanium.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'loaded'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #000066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	Titanium.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;http://google.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Within the function used with onload event you can use the <strong>responseText</strong> (normal or json reply), <strong>responseXML</strong>(XML reply) or <strong>responseData</strong> (binary reply) for further proccessing.</p>
<h2>Things to mention when you use networking with your apps</h2>
<p>Always let the user know that something is processing in the background &#8211; on a slow internet connection the users will believe the app crashed and will probably kill it. </p>
<p>For iPhone: if there is no network you need to alert the user about it &#8211; this is an Apple request and failing to do so might lead them to reject your app. This can be verified with the boolean <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network-module">Titanium.Network.online</a></strong>.</p>
<h2>JSON , XML</h2>
<p>When it&#8217;s about sending structured data over httpthe server can reply in a JSON or XML format. I prefer JSON for two reasons:</p>
<ol>
<li>The JSON format is Javascript native</li>
<li>the overall size of the information received from the server is smaller, so the reply comes faster.</li>
</ol>
<p><strong>Titanium can use both formats</strong>: <strong>JSON</strong> with <strong>JSON.parse(string) </strong>and <strong>JSON.stringify(object)</strong> to manipulate the data and <strong>XML</strong> with the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.XML-module">Titanium.XML</a></strong>. The XML string needs to be parsed and the result, an DOM2 object, needs further processing with the DOM2 own methods.</p>
<p>So as you see XML can get ugly( or at least for me). Because of this I use when possible <strong>YQL</strong>.</p>
<h2>YQL</h2>
<p><strong>YQL</strong> is the SQL for the web (a SQL like implementation for HTML build by the guys from Yahoo) that allows you to use a syntax similar to SQL for the <strong>DOM</strong> elements in a webpage. I won&#8217;t go further into details, you can do it by yourself on <strong><a href="http://developer.yahoo.com/yql/">the YQL page</a></strong>.</p>
<p>What I will show you is how to convert an XML reply into JSON using the  <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Yahoo.yql-method.html">Titanium.Yahoo.yql</a></strong> module. </p>
<p>First of all you need to go to the <strong>YQL Console</strong> page and try to transform an <strong>rss feed</strong> &#8211; that is pure <strong>XML</strong> into JSON. Go to <strong><a href="http://developer.yahoo.com/yql/console/?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F2Fdeveloper.appcelerator.com%2Ffeed%2Fquestions%2Fcreated%22">this</a></strong> link, select JSON as format, empty the callback function input, un-check the Diagnostics checkbox and run the test. You will get the <strong>XML</strong> file converted in JSON.</p>
<p>We can do the same using the YQL module that Titanium provides us:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
Titanium.<span style="color: #660066;">Yahoo</span>.<span style="color: #660066;">yql</span><span style="color: #009900;">&#40;</span>
   <span style="color: #3366CC;">'select * from xml where url=&quot;http://developer.appcelerator.com/feed/questions/created&quot;'</span><span style="color: #339933;">,</span>
   <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// e.data holds the reply in json format</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We covered a little the basic networking in Titanium but there are unlimited ways on how you can use it.</p>
<h2>More resources</h2>
<p>As always <strong><a href="http://github.com/appcelerator/titanium_mobile/tree/master/demos/KitchenSink/Resources/examples/">KitchenSink</a></strong> scroll at the end and take a look at all <strong>xhr*,xml*, yql*</strong> files.</p>
<p>A full <a href="http://mobile.tutsplus.com/tutorials/appcelerator/appcelerator-using-json-to-build-a-twitter-client/">Twitter APIs/Titanium XHR tutorial</a> &#8211;  a must read.</p>
<p>And of course the official documentation:</p>
<ul>
<li><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.WebView-object"><strong>WebView</strong></a></li>
<li><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object"><strong>HttpClient</strong></a></li>
<li><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Yahoo.yql-method.html"><strong>YQL</strong></a></li>
</ul>
<p>If you have any questions or suggestions don&#8217;t hesitate to post them here. </p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-3-ajaxxhr-y-webviews/" rel="me">Siete días con Titanium – día 3 – ajax(xhr) y webviews</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1626"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-3-ajaxxhr-and-webviews%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-3-ajaxxhr-and-webviews%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-3-ajaxxhr-and-webviews%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-3-ajaxxhr-and-webviews/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Seven days with Titanium – day 2 &#8211; tables and pickers</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 19:05:53 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[android titanium tables]]></category>
		<category><![CDATA[Appcelerator Titanium tutorial]]></category>
		<category><![CDATA[iphone tables titanium]]></category>
		<category><![CDATA[iphone titanium picker]]></category>
		<category><![CDATA[PickerViews]]></category>
		<category><![CDATA[The Data property]]></category>
		<category><![CDATA[The Table element]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[titanium tables]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1586</guid>
		<description><![CDATA[Welcome to <strong>day 2 with Titanium - Tables and pickers.</strong> A short reminder of what we've done till now:
<ul>
	<li><strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Introduction</a></strong></li>
	<li><strong><a href="http://cssgallery.info/seven-days-with-titanium-–-day-1-–-windowsviewsnavigation-and-tabs/">Day 1 - Windows, Views, navigation and Tabs</a></strong>.</li>
</ul>
<br/>
Believe it or not, the <strong>table</strong> is the most used UI element in the iPhone SDK. If you play a little with the applications that come with your "iDevice" you will see that almost all the native apps have tables. Sometimes they are so well designed and the appearance so changed that you don't even think of them as tables. But in time, while you will learn more, you will be able to spot it and a lot of ideas will come to you :). Also I think that that the <strong>pickers</strong> are some sort of "undercover tables" so that's why I'll present both in the same tutorial. ]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-2-tables-and-pickers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-2-tables-and-pickers%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Welcome to <strong>day 2 with Titanium &#8211; Tables and pickers.</strong> A short reminder of what we&#8217;ve done till now:</p>
<ul>
<li><strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Introduction</a></strong></li>
<li><strong><a href="http://cssgallery.info/seven-days-with-titanium-–-day-1-–-windowsviewsnavigation-and-tabs/">Day 1 &#8211; Windows, Views, navigation and Tabs</a></strong>.</li>
</ul>
<p>Believe it or not, the <strong>table</strong> is the most used UI element in the iPhone SDK. If you play a little with the applications that come with your &#8220;iDevice&#8221; you will see that almost all the native apps have tables. Sometimes they are so well designed and the appearance so changed that you don&#8217;t even think of them as tables. But in time, while you will learn more, you will be able to spot it and a lot of ideas will come to you <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Also I think that that the <strong>pickers</strong> are some sort of &#8220;undercover tables&#8221; so that&#8217;s why I&#8217;ll present both in the same tutorial. </p>
<h2>The Table element</h2>
<p>The table element is created using  <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.createTableView-method.html">Titanium.UI.createTableView</a></strong> function. This function accepts an object as argument like we are used to &#8211; with the different settings we might want to give to the table. Besides the usual properties ( like <strong>top</strong>, <strong>left</strong>, etc) we have a very important one named <strong>data</strong>. </p>
<h2>The Data property</h2>
<p>The <strong>data</strong> property will hold the rows of the table as an array. The below code will show you what I&#8217;m talking about:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> win1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#fff&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> table1 <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	data<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 1 - simple row&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 2 -  with child&quot;</span><span style="color: #339933;">,</span> hasChild<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 3 -  with detail&quot;</span><span style="color: #339933;">,</span> hasDetail<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 4 -  with check&quot;</span><span style="color: #339933;">,</span> hasCheck<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 5 -  red background&quot;</span><span style="color: #339933;">,</span> backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#f00&quot;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#93;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win1.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>table1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
win1.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>So, what are we doing here? </h3>
<p>We create a <strong>window</strong>. As we said in <strong><a href="http://cssgallery.info/seven-days-with-titanium-–-day-1-–-windowsviewsnavigation-and-tabs/">day 1</a></strong>, the application needs to have at least one window. Into this window we add <strong>table1</strong> with the <strong>rows</strong> set as above.</p>
<p>This is the simplest way to do it, with the properties of the rows set as the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TableViewRow-object">Titanium.UI.TableViewRows</a></strong> documentation page specifies.</p>
<p>If you develop cross-platform you&#8217;ll need to take in account that each platform will show the data in it&#8217;s way (and some specific things may be missing). Below are 2 screenshots on how the above code looks on iPhone and Android.</p>
<p><img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-1.png" alt="Simple table with Titanium on iPhone" title="Simple table with Titanium on iPhone" width="295" height="444" class="alignleft size-full wp-image-1591" /><img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-21.png" alt="Simple table with Titanium on Android" title="Simple table with Titanium on Android" width="295" height="444" class="alignleft size-full wp-image-1592" /></p>
<h2>A very important thing to know about tables</h2>
<p>The rows into tables are rendered &#8220;on request&#8221;, so let&#8217;s say you have 20 rows &#8211;  it&#8217;s clear that visible will be only a few (10 on a standard height). To save memory and to speed up the application  the other rows will be rendered when the user scrolls the table. To do this faster the OS of the device creates some sort of template for each layout of the rows and reuse it changing only what&#8217;s different. To take advantage of this you <strong>MUST</strong> set a <strong>className</strong> attribute for the rows that share the same layout ( I didn&#8217;t do it above to simplify the code).</p>
<p><strong>It is important</strong> mainly because for a few rows the memory usage is not too high, but for many rows &#8211; depending on how many and what&#8217;s inside the row &#8211; the app will move slow or even crash. The <strong>className</strong> can be any string you like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Row 1 - simple row&quot;</span><span style="color: #339933;">,</span> className<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;layout_one&quot;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>More about tables</h2>
<p>Each table has at least one section  and each table can be plain (as above) or grouped. Let&#8217;s create a grouped table:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> table1 <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	style<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">TableViewStyle</span>.<span style="color: #660066;">GROUPED</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> section1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewSection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section1.<span style="color: #660066;">headerTitle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> row1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Hello 1&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> row2 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Hello 2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section1.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>row1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section1.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>row2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> section2 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewSection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section2.<span style="color: #660066;">headerTitle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> row3 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Hello 3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> row4 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableViewRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Hello 4&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section2.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>row3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
section2.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>row4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
table1.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>section1<span style="color: #339933;">,</span>section2<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>And the result</h3>
<p><img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-31.png" alt="Grouped table with Titanium on Iphone" title="Grouped table with Titanium on Iphone" width="295" height="444" class="alignleft size-full wp-image-1595" /><img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-4.png" alt="Grouped table with Titanium on Android" title="Grouped table with Titanium on Android" width="295" height="444" class="alignleft size-full wp-image-1598" /></p>
<p>As you see I used the method <strong>setData()</strong> to set the content of the table after it is created. </p>
<p>This method accepts any array of <strong>sections</strong> created with <strong><a href="http://developer.appcelerator.com/apidoc/mobile/1.3/Titanium.UI.createTableViewSection-method.html">Titanium.UI.createTableViewSection</a></strong> , <strong>rows</strong> created with <strong><a href="http://developer.appcelerator.com/apidoc/mobile/1.3/Titanium.UI.createTableViewRow-method.html">Titanium.UI.createTableViewRow</a></strong>  or <strong>objects</strong> that describe the <strong>rows</strong> as in the first example.</p>
<h2>Custom tables</h2>
<p>All the above examples show you how to create simple tables. If you want to create a more complicated layout you will need to create custom rows for this and <strong><a href="http://cssgallery.info/custom-row-for-tableview-in-appcelerator-titanium/">The Custom Rows tutorial</a></strong> explains how to do it..</p>
<h2>The Pickers or PickerViews</h2>
<p>The pickers are the equivalent of the <strong>select</strong> tag in html. The only difference is that you can create a multicolumn picker (which in HTML it&#8217;s not possible without using some tricks). The function that creates a picker is <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.createPicker-method">Titanium.UI.createPicker</a></strong></p>
<p>A simple picker it&#8217;s created with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> picker <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Bananas'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Strawberries'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Mangos'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Grapes'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
picker.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sadly for now it is implemented only on the Apple platform (please correct me if I&#8217;m wrong)  so here it is how it should look like:<br />
<img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-5.png" alt="Picker with Titanium" title="Picker with Titanium" width="320" height="480" class="alignnone size-full wp-image-1600" /></p>
<p>The type property of the picker lets you chose form the next predefined types but you always can create your own multicolumn picker.</p>
<p><strong>Titanium.UI.PICKER_TYPE_PLAIN (default)</strong>,<br />
<strong>Titanium.UI.PICKER_TYPE_DATE_AND_TIME</strong>,<br />
<strong>Titanium.UI.PICKER_TYPE_DATE</strong>,<br />
<strong>Titanium.UI.PICKER_TYPE_TIME</strong> or<br />
<strong>Titanium.UI.PICKER_TYPE_COUNT_DOWN_TIMER</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> picker <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
type<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">PICKER_TYPE_DATE_AND_TIME</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><img src="http://cssgallery.info/wp-content/uploads/2010/08/capture-6.png" alt="Date and time picker" title="Date and time picker" width="320" height="480" class="alignnone size-full wp-image-1601" /></p>
<p>Since the tables and the rows are similar  you will be able to create your custom rows in the same way as for the tables using of course the specific functions for the picker (linked at the end of the tutorial).</p>
<p>I won&#8217;t go further as the post is big enough already  but I&#8217;ll try in the future to create a set of posts that will be specific to an UI element and where I&#8217;ll try to &#8220;dissect&#8221; it as much as possible.</p>
<h2>More resources</h2>
<p>Here are some links to follow for more information:</p>
<ul>
<li><strong><a href="http://cssgallery.info/custom-row-for-tableview-in-appcelerator-titanium/">Custom table rows with Titanium</a></strong></li>
<li>A more advanced tutorial on <strong><a href="http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/">how to create a picker that behaves like the keyboard</a></strong> on iPhone</li>
<li>Titanium documentation for <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.Picker-object">pickers</a></strong>, <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.PickerColumn-object">picker columns</a></strong>, <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.PickerRow-object">picker rows</a></strong> and <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TableView-object">tables</a></strong>, <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TableViewSection-object">table sections</a></strong>, <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TableViewRow-object">table rows</a></strong>.</li>
</ul>
<p>As always, thank you for reading this and I&#8217;m waiting for your questions and suggestions. If you have your own tutorials or links I&#8217;ll be glad to add them here to improve the quality of the posts.</p>
<p>See you on <a href="http://cssgallery.info/seven-days-with-titanium-%E2%80%93-day-3-ajaxxhr-and-webviews/">day 3</a>.</p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-2-tablas-y-selectores/" rel="me">Siete días con Titanium – día 2 – tablas y selectores</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1586"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-2-tables-and-pickers%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-2-tables-and-pickers%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-2-tables-and-pickers%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/custom-row-for-tableview-in-appcelerator-titanium/' rel='bookmark' title='Custom row for TableView in Appcelerator Titanium'>Custom row for TableView in Appcelerator Titanium</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-0-introduction/' rel='bookmark' title='Seven days with Titanium &#8211; day 0 &#8211; introduction'>Seven days with Titanium &#8211; day 0 &#8211; introduction</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/' rel='bookmark' title='Seven days with Titanium – day 5 – GPS and Maps'>Seven days with Titanium – day 5 – GPS and Maps</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Seven days with Titanium – day 1 – windows, views, navigation and tabs</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-1-windows-views-navigation-and-tabs/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-1-windows-views-navigation-and-tabs/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 22:38:07 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[basics of the interface]]></category>
		<category><![CDATA[createWindow]]></category>
		<category><![CDATA[download code]]></category>
		<category><![CDATA[download project]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[NavigationGroup]]></category>
		<category><![CDATA[TabGroup]]></category>
		<category><![CDATA[tableGroup]]></category>
		<category><![CDATA[tabs]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[titanium tabs]]></category>
		<category><![CDATA[titanium views]]></category>
		<category><![CDATA[titanium windows]]></category>
		<category><![CDATA[views]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1543</guid>
		<description><![CDATA[Today is going to be the first day of the Titanium tutorial for beginners  and a pretty long post.
Before we begin here is the recap of what we have written until now (and updated what will come): <strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Seven days with Titanium – day 0 – introduction</a></strong>

As specified in the <strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Introduction</a></strong> post I will try in this lesson to say some things about <strong>navigation</strong>, <strong>windows</strong>, <strong>views</strong> and <strong>tabs</strong> – the basics of the interface.
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-1-windows-views-navigation-and-tabs%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-1-windows-views-navigation-and-tabs%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Today is going to be the first day of the Titanium tutorial for beginners  and a pretty long post.<br />
Before we begin here is the recap of what we have written until now (and updated what will come): <strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Seven days with Titanium – day 0 – introduction</a></strong></p>
<p>As specified in the <strong><a href="http://cssgallery.info/seven-days-with-titanium-day-0-introduction/">Introduction</a></strong> post I will try in this lesson to say some things about <strong>navigation</strong>, <strong>windows</strong>, <strong>views</strong> and <strong>tabs</strong> – the basics of the interface.</p>
<p>I will try to first to enumerate each of these elements and then explain what they do and how to use them. Also each interface element has a link to it&#8217;s documentation page on Appcelerator site.</p>
<p>Everything that is related to the interface you will find it into the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI-module" target="_blank">Titanium.UI</a></strong> module. There are also some platform specific modules that cover elements of interface present only on the specific platform: <strong>Titanium.UI.Android,Titanium.UI.iPhone,Titanium.UI.iPad</strong> but for now we won&#8217;t talk about them.</p>
<h2>The Window element</h2>
<p>The basic element in a Titanium application is a <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.Window-object" target="_blank">Window</a></strong>. There is nothing complicated here, it&#8217;s a &#8220;container&#8221; for all the other elements if you wish. An application can have one or more windows but it must to have at least one window. A Window it&#8217;s one of the few elements that can exist by itself without the need to be a child of something.</p>
<h2>The View element</h2>
<p>Another  basic element  it&#8217;s a <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.View-object" target="_blank">View</a></strong>. The <strong>View</strong> can be the analog of a <strong>div</strong> tag in HTML so basically it&#8217;s just a rectangle that can hold other elements for an easy and convenient control of the application&#8217;s layout. A <strong>View</strong> cannot exists by itself  it will have to be a child of a <strong>Window</strong> or other element that accepts <strong>Views</strong> as child (a <strong>TableRowView</strong> or another <strong>View</strong> for example).</p>
<h2>The TabGroup and the Tabs</h2>
<p>The main navigational elements (and common to all platforms) is the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TabGroup-object" target="_blank">TabGroup</a></strong> and the <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.Tab-object" target="_blank">Tabs</a></strong>. Exactly what the names says &#8211; analog with a browser with tabs capabilities , a <strong>TabGroup</strong> will hold more <strong>Tabs</strong>. Each <strong>Tab</strong> will control (open) a window. If you decide that you application will have a <strong>TabGroup</strong> this will be the &#8220;root&#8221; of the application. What does it mean? That any window opened by the <strong>Tabs</strong> in <strong>TabGroup</strong> will be controlled by this  and the <strong>TabGroup</strong> has one instance only  and it cannot be closed or removed, but only hidden.</p>
<p>There is also an iPhone specific navigation element, named <strong><a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.iPhone.NavigationGroup-object" target="_blank">NavigationGroup</a></strong> that allows to navigate through windows without the need to create the navigation toolbar/buttons by ourselves.</p>
<h2>Example</h2>
<p>This is how an application with<strong> a TabGroup, 2 Windows, 2 Tabs and a View </strong>looks loke on the Android and the iPhone platforms.</p>
<p><img class="alignleft size-full wp-image-1555" title="Seven days with Titanium- day 1 - android" src="http://cssgallery.info/wp-content/uploads/2010/08/capture-3.png" alt="Seven days with Titanium- day 1 - android" width="295" height="444" /><img class="alignleft size-full wp-image-1556" title="Seven days with Titanium- day 1 - iPhone" src="http://cssgallery.info/wp-content/uploads/2010/08/capture-2.png" alt="Seven days with Titanium- day 1 - iPhone" width="295" height="444" /></p>
<h2>More details about each element</h2>
<p>Before we continue it would be great to take a look at each element documentation page because there is a lot of  valuable info there. As you see, each interface element has some <strong>properties</strong> that are setters and/or getters  like <strong>top</strong>, <strong>backgroundColor</strong>, etc), some <strong>methods</strong> (<strong>add()</strong>, <strong>open()</strong>) and <strong>events</strong> &#8211;  usually user interaction events (touchstart, doubletap, etc) or specific events (open,close). Very similar with the web elements.</p>
<p>I want to specify some things:<br />
<strong>sizes</strong> are in pixels but <strong>without</strong> the usual &#8220;px&#8221; notation, or in percents &#8211;  but this requires to be written as a string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
<span style="color: #660066;">top</span><span style="color: #339933;">:</span><span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
width<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;50%&quot;</span>
...</pre></div></div>

<p><strong>colors</strong> are in hexa rgb , the short or the long form , don&#8217;t use names:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#f00&quot;</span><span style="color: #339933;">,</span>
borderColor<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#ff0055&quot;</span>
...</pre></div></div>

<p>Let&#8217;s try to elaborate a little.</p>
<h2>The Window element, again</h2>
<p>The window element is created using the function <strong>Titanium.UI.createWindow</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> win1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>  
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Tab 1'</span><span style="color: #339933;">,</span>
    backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#fff'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What the above code means? That we pass to the <strong>createWindow </strong>function an object argument with all the properties that we want to set.</p>
<p>This could be written like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> win1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win1.<span style="color: #660066;">title</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'Tab 1'</span>
win1.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">;</span></pre></div></div>

<p>I think it&#8217;s pretty clear so I won&#8217;t go further with the code for <strong>view</strong>, <strong>tabs</strong> and so on. It&#8217;s similar to this only that you use <strong>Titanium.UI.createView</strong>, etc</p>
<h2>How the tabGroup, tabs and the windows are related</h2>
<p>The logic behind the above UI elements it&#8217;s as it follows:</p>
<p>The <strong>tabGroup</strong> is the root of the application, the &#8220;master&#8221;, it cannot be included in any other UI element.<br />
This holds the <strong>tabs</strong>( at least one)  and a tab, when clicked, opens the window associated with it.</p>
<p>Here is a sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #006600; font-style: italic;">// create tab group</span>
<span style="color: #003366; font-weight: bold;">var</span> tabGroup <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTabGroup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create base UI tabs and  windows</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> win1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>  
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Tab 1'</span><span style="color: #339933;">,</span>
    backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#fff'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> tab1 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTab</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>  
    icon<span style="color: #339933;">:</span><span style="color: #3366CC;">'KS_nav_views.png'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Tab 1'</span><span style="color: #339933;">,</span>
    window<span style="color: #339933;">:</span>win1
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> win2 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>  
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Tab 2'</span><span style="color: #339933;">,</span>
    backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#fff'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> tab2 <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTab</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>  
    icon<span style="color: #339933;">:</span><span style="color: #3366CC;">'KS_nav_views.png'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Tab 2'</span><span style="color: #339933;">,</span>
    window<span style="color: #339933;">:</span>win2
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  add tab</span>
&nbsp;
tabGroup.<span style="color: #660066;">addTab</span><span style="color: #009900;">&#40;</span>tab1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
tabGroup.<span style="color: #660066;">addTab</span><span style="color: #009900;">&#40;</span>tab2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #006600; font-style: italic;">// open tab group</span>
tabGroup.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above code will generate a similar interface with the screenshots without the labels &#8220;I am window 1&#8243; etc and the red view. This would be a good exercise for you <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Important !</h3>
<p>As you see we set the same <strong>titles</strong> to <strong>tab1 &#8211; win1</strong> and to <strong>tab2 &#8211; win2</strong>. This is not mandatory from the code point of view. The window associated to each tab is specified using the <strong>window</strong> property that points to the previous declared window. Why it&#8217;s like this then? It&#8217;s about usability. The user needs to have the same values when he/she has to associate two visual elements or will get confused. Also Apple might not like this when you will submit your app.</p>
<h2>Resources</h2>
<p>The above code it&#8217;s found in the default app.js file that you get when you create a new project. I only explained a little how things are.</p>
<p>A <strong>very very</strong> good resource it&#8217;s the KitcheSink project form Appcelerator team, found <strong><a href="http://github.com/appcelerator/titanium_mobile/downloads">here</a></strong>.</p>
<p>For the advanced coders <strong><a href="http://cssgallery.info/create-a-nifty-tooltip-in-titanium/">this is another tutorial</a></strong> on how to create a <strong>tooltip</strong> with Titanium  that basically it&#8217;s just a view with a background image.</p>
<p>I think it&#8217;s enough for <strong>day 1</strong>, the post is too long and I&#8217;ll only add that I&#8217;m waiting for your questions, suggestions and critics if I made any mistake <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  With your questions/suggestions I&#8217;ll update the post or create a new one linked to &#8220;day 1&#8243;.</p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-1-ventanas-vistas-navegacion-y-pestanas/" rel="me">Siete días con Titanium – Día 1 – Ventanas, vistas, navegación y pestañas</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1543"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-1-windows-views-navigation-and-tabs%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-1-windows-views-navigation-and-tabs%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-1-windows-views-navigation-and-tabs%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/' rel='bookmark' title='Seven days with Titanium – day 2 &#8211; tables and pickers'>Seven days with Titanium – day 2 &#8211; tables and pickers</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-6%e2%80%93animations-and-transforms/' rel='bookmark' title='Seven days with Titanium – day 6 – animations and transforms'>Seven days with Titanium – day 6 – animations and transforms</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-0-introduction/' rel='bookmark' title='Seven days with Titanium &#8211; day 0 &#8211; introduction'>Seven days with Titanium &#8211; day 0 &#8211; introduction</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-1-windows-views-navigation-and-tabs/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Seven days with Titanium &#8211; day 0 &#8211; introduction</title>
		<link>http://cssgallery.info/seven-days-with-titanium-day-0-introduction/</link>
		<comments>http://cssgallery.info/seven-days-with-titanium-day-0-introduction/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 14:58:16 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[Appcelerator]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1521</guid>
		<description><![CDATA[<h2>What is all about?</h2>
I intend this to be a set of seven tutorials to help beginners understand how to work with the Titanium mobile framework.

The guys at <a href="http://www.appcelerator.com/">Appcelerator</a> did a great job with the framework allowing to us, the web developers, to have access on native applications for mobile devices ( Apple iDevices, Android and soon BlackBerry).

The framework reached the 1.4 version and each day more developers seem to be interested in using it - especially because it's free. The community is great and you'll find answers on almost any problem you might have. But  for the beginners is still a little difficult to start. From here surged the idea of this set of tutorials in which I will try to cover the basics of the development process for iPhone and Android.
<img src="http://cssgallery.info/wp-content/uploads/2010/08/Titanium-Appcelerator.png" alt="Titanium Appcelerator" title="Titanium Appcelerator" width="630" class="size-full wp-image-1530" />]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-0-introduction%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-0-introduction%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h2>What is all about?</h2>
<p>I intend this to be a set of seven tutorials to help beginners understand how to work with the Titanium mobile framework.</p>
<p>The guys at <a href="http://www.appcelerator.com/">Appcelerator</a> did a great job with the framework allowing to us, the web developers, to have access on native applications for mobile devices ( Apple iDevices, Android and soon BlackBerry).</p>
<p>The framework reached the 1.4 version and each day more developers seem to be interested in using it &#8211; especially because it&#8217;s free. The community is great and you&#8217;ll find answers on almost any problem you might have. But  for the beginners is still a little difficult to start. From here surged the idea of this set of tutorials in which I will try to cover the basics of the development process for iPhone and Android.<br />
<img src="http://cssgallery.info/wp-content/uploads/2010/08/Titanium-Appcelerator.png" alt="Titanium Appcelerator" title="Titanium Appcelerator" width="630" class="size-full wp-image-1530" /></p>
<h2>Some things to start</h2>
<p>Each post will contain a header with the previous tutorials written, for an easy follow.  It will also have a footer part where some links of interest will be published.</p>
<p>It won&#8217;t be daily posts, I will write when I&#8217;ll have enough time to test and write the examples. I prefer a better written post to a fast one. And each post might be updated to add forgotten things (or to fix issues) so don&#8217;t forget to bookmark it.</p>
<p>The provided code will be licensed under the MIT license  so you will be able to use it as you wish.</p>
<h2>What should I know and/or what do I need?</h2>
<p>Basically you need only Javascript knowledge. Some  OOP coding routine would be great but is not mandatory ( we learn on the fly <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<ul
	<li>To develop for Android you will need Android SDK( Linux / OsX / Windows )</li>
<li>To develop for iPhone you will need a Mac ( Apple&#8217;s rules not mine)</li>
</ul>
<p>I will not show you how to install any of the frameworks, SDKs, etc.  There are good tutorials and HowTo&#8217;s that will show you this so I don&#8217;t want to repeat what&#8217;s already out there.</p>
<h2>What do I need to forget?</h2>
<p>May look strange but you really have to forget about something: <strong>DOM</strong>. Why? Because there is no DOM in there. We are not anymore in the HTML world. So nothing from  HTML applies here. There are similar things (events for example) but everything it&#8217;s just an analogy to make it easier to use it.</p>
<p>Forget also about using  a library ( yes, no MooTools, jQuery, etc…), or at least for a start. Again why? Because all these libraries are DOM oriented  and are used to fight with multiple browsers, to please each of them  and this adds bloatware to your code &#8211; code that will not be used by the devices anyway.</p>
<p>Why &#8220;at least for a start&#8221; ? Well, later you might miss some functions and methods these libraries have (let&#8217;s say the array methods like filter, each, unique, etc… ) but you will be able to implement this by your own and avoid a 150k library.</p>
<h2>What you will NOT learn in these tutorials:</h2>
<p>You won&#8217;t learn Javascript (you need to know it already). You will not learn ObjC and/or Java (in the end this is the purpose of Titanium). I will try to provide the code for each thing you will learn about  as well as screenshots and maybe videos.</p>
<p>And at last</p>
<h2>What will you learn:</h2>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-–-day-1-–-windowsviewsnavigation-and-tabs/">Day 1</a>:</h3>
<p>Some things about navigation, windows, views and tabs &#8211;  the basics of the interface. I will try to briefly explain what does each of these elements and how to use them.</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-%E2%80%93-day-2-tables-and-pickers/">Day 2:</a></h3>
<p>Tables and pickers (or option pickers, the &#8216;select&#8217; tag equivalent)</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-–-day-3-ajaxxhr-and-webviews/">Day 3:</a></h3>
<p>Ajax (actually XHR) &#8211; on how to use the HttpClient for webpages, XML, JSON and YQL; The webview &#8211;  how to embed html code in your apps.</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-–-day-4-–-media-–-images-movies-and-sounds/">Day 4:</a></h3>
<p>Media &#8211; images, movies and sounds</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/">Day 5:</a></h3>
<p>GPS and maps.</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-day-6%E2%80%93animations-and-transforms/">Day 6:</a></h3>
<p>Some eye-candy with animations and transforms</p>
<h3><a href="http://cssgallery.info/seven-days-with-titanium-day-7-orientation-and-accelerometer/" title="Orientation and accelerometer">Day 7:</a></h3>
<p>About orientation and accelerometer.</p>
<h3>Day 8:</h3>
<p>This will be a post-tutorials day  where I&#8217;ll sum up the lessons,  add links and so on.</p>
<p>I know I said seven days  and in the end are 9, but this one and the last do  not count as &#8220;learning&#8221; days. There is a lot of stuff left out  like scrollviews, sliders, some modules &#8211; Facebook? &#8211;  more advanced techniques  &#8211;  like app events &#8211;  but hopefully these will be the subject of another tutorials.</p>
<p>Let me know your thoughts, if you have ideas of what should I write about &#8211;  remember that this is a beginner&#8217;s guide &#8211;  or to ask questions.</p>
<p>See you in <strong>day 1</strong>.</p>
<h2 style="color:#ff0000">Update:</h2>
<p><a href="http://developer.appcelerator.com/blog/2010/08/introducing-new-getting-started-guides.html">PDF documents on how to install Titanium on each of the supported platforms</a> ( Linux / OsX / Windows ) can be found on Titanium Appcelerator blog, go get them <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/siete-dias-con-titanium-dia-0-introduccion/" rel="me">Siete días con Titanium – Día 0 – Introducción</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1521"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-0-introduction%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-0-introduction%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fseven-days-with-titanium-day-0-introduction%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/seven-days-with-titanium-day-1-windows-views-navigation-and-tabs/' rel='bookmark' title='Seven days with Titanium – day 1 – windows, views, navigation and tabs'>Seven days with Titanium – day 1 – windows, views, navigation and tabs</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/' rel='bookmark' title='Seven days with Titanium – day 2 &#8211; tables and pickers'>Seven days with Titanium – day 2 &#8211; tables and pickers</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-4-media-images-movies-and-sounds/' rel='bookmark' title='Seven days with Titanium – day 4 – Media – images, movies and sounds'>Seven days with Titanium – day 4 – Media – images, movies and sounds</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/seven-days-with-titanium-day-0-introduction/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Making a combo-box in Titanium Appcelerator &#8211; code and video</title>
		<link>http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/</link>
		<comments>http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/#comments</comments>
		<pubDate>Sat, 29 May 2010 10:08:12 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[getSelectedRow]]></category>
		<category><![CDATA[iphone combobox]]></category>
		<category><![CDATA[iphone tutorial]]></category>
		<category><![CDATA[picker_view]]></category>
		<category><![CDATA[textField]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[titanium appcelerator combobox]]></category>
		<category><![CDATA[titanium tutorial]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1221</guid>
		<description><![CDATA[A true iPhone or Ipad combobox that allows you to use  the same textfield to input arbitrary text or select a value from a UIPicker element. All built with Titanium Appcelerator to include in your iPhone application.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fmaking-a-combo-box-in-titanium-appcelerator-code-and-video%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fmaking-a-combo-box-in-titanium-appcelerator-code-and-video%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Sometimes we need a true combobox for our projects but iPhone SDK does not have a native one (at least from what I know)  and  of course neither has Titanium. </p>
<p>So we will build one. A &#8220;true&#8221; iPhone or Ipad combobox that allows you to use  the same textfield to input arbitrary text or select a value from a UIPicker element.</p>
<p>Updated with <a href="http://twitter.com/CJ_Reed">@CJ_Reed&#8217;s</a> screenshot and code at the final of the tutorial.</p>
<p>Let&#8217;s see the video first, then we&#8217;ll get to work:</p>
<p><object width="650" height="400"><param name="movie" value="http://www.youtube.com/v/2H-w_hUQtPw&#038;hl=en&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/2H-w_hUQtPw&#038;hl=en&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="650" height="400"></embed></object></p>
<h2>Ok, what do we need for this iPhone combobox ?</h2>
<p>First of all we need a <strong>textField</strong> to accept input from the user. Titanium lets you set the <strong>leftButton</strong> and <strong>rightButton</strong> for this <strong>textField</strong> while constructing it. So we will take advantage of this and create a textField as it follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_combo <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTextField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	hintText<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;write your name or select one&quot;</span><span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
	borderStyle<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BORDERSTYLE_ROUNDED</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nothing special, a regular <strong>textField</strong> with a hint for the user  that will disappear when the <strong>textField</strong> has a value.</p>
<p>Now we need to create the <strong>rightButton</strong> for it.</p>
<p>We will  use a system button provided by Apple (<strong>Titanium.UI.iPhone.SystemButton.DISCLOSURE</strong>)  only that we will rotate it 90 degrees to serve our purpose. This is the code that creates the <strong>rightButton</strong> and the transformation applied to it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> tr <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">create2DMatrix</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tr <span style="color: #339933;">=</span> tr.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> drop_button <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		style<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">SystemButton</span>.<span style="color: #660066;">DISCLOSURE</span><span style="color: #339933;">,</span>
		transform<span style="color: #339933;">:</span>tr
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now that we have the <strong>rightButton</strong> as we need it, the <strong>textField</strong> constructor becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_combo <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTextField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	hintText<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;write your name or select one&quot;</span><span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
	borderStyle<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BORDERSTYLE_ROUNDED</span><span style="color: #339933;">,</span>
	rightButton<span style="color: #339933;">:</span>drop_button<span style="color: #339933;">,</span>
	rightButtonMode<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BUTTONMODE_ALWAYS</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Please note the <strong>rightButtonMode:Titanium.UI.INPUT_BUTTONMODE_ALWAYS</strong> declaration, it makes this button visible all the time.</p>
<p>This is how it looks:</p>
<p><img class="alignnone size-full wp-image-1223" title="iPhone combobox with Titanium" src="http://cssgallery.info/wp-content/uploads/2010/05/capture-11.png" alt="iPhone combobox with Titanium" width="400" height="83" /></p>
<p>Pretty sexy, isn&#8217;t it? Well we&#8217;re not done yet.</p>
<h2>Building the modal picker</h2>
<p>When the user focuses on the <strong>textField</strong>, the keyboard appears &#8211; so we will have to build our <strong>picker</strong> to emulate the same behaviour and to maximize the usability of our form. For this we will need a <strong>Picker</strong>  and two buttons: <strong>Done</strong> and <strong>Cancel</strong>. These two buttons will be positioned in a <strong>Toolbar</strong>, again, to  emulate as good as possible the keyboard behaviour.</p>
<p>Let&#8217;s build everything:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> picker_view <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	height<span style="color: #339933;">:</span><span style="color: #CC0000;">251</span><span style="color: #339933;">,</span>
	bottom<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> cancel <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Cancel'</span><span style="color: #339933;">,</span>
	style<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">SystemButtonStyle</span>.<span style="color: #660066;">BORDERED</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> done <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Done'</span><span style="color: #339933;">,</span>
	style<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">SystemButtonStyle</span>.<span style="color: #660066;">DONE</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> spacer <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	systemButton<span style="color: #339933;">:</span>Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">SystemButton</span>.<span style="color: #660066;">FLEXIBLE_SPACE</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> toolbar <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createToolbar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
	items<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>cancel<span style="color: #339933;">,</span>spacer<span style="color: #339933;">,</span>done<span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> picker <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		top<span style="color: #339933;">:</span><span style="color: #CC0000;">43</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
picker.<span style="color: #660066;">selectionIndicator</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> picker_data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'John'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Alex'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Marie'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Eva'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createPickerRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'James'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
picker.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>picker_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
picker_view.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>toolbar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
picker_view.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>picker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code is a little long but is not rocket science. Some stuff to talk about though:</p>
<ul>
<li>Everyting is wrapped inside a view &#8211; <strong>picker_view</strong> &#8211;  because we will have to animate like the keyboard does, so it&#8217;s faster to animate one element only.</li>
<li>The height of <strong>picker_view</strong> is the height of the <strong>toolbar</strong> (43px) + the height of the <strong>picker</strong> (208px). How do I know this? I just used a ruler <img src='http://cssgallery.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </li>
</ul>
<p>The combobox interface looks like this:</p>
<p><img class="alignnone size-full wp-image-1226" title="picker Titanium for combobox" src="http://cssgallery.info/wp-content/uploads/2010/05/capture-2.png" alt="picker Titanium for combobox" width="326" height="465" /></p>
<h2>Creating the picker animation</h2>
<p>We also need to create 2 animations: <strong>slide_in</strong> and <strong>slide_out</strong>. We will animate the <strong>bottom</strong> property of the <strong>picker_view</strong>. We will need to start with the <strong>picker_view</strong> off the screen, so we will build it with:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> bottom<span style="color: #339933;">:-</span><span style="color: #CC0000;">251</span></pre></div></div>

<p>instead of 0 as it was initially.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> slide_in <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAnimation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>bottom<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> slide_out <span style="color: #339933;">=</span>  Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAnimation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>bottom<span style="color: #339933;">:-</span><span style="color: #CC0000;">251</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The logic behind the animations is this:</p>
<ul>
<li>The user focuses the <strong>textField</strong> &#8211;  the keyboard appears ( it&#8217;s done by the OS , no worries here) and if the <strong>picker_view</strong> is visible we need to hide it.</li>
<li>The user clicks the <strong>rightButton</strong> &#8211; we need to hide the keyboard and show the <strong>picker_view</strong>.</li>
</ul>
<p>Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">my_combo.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	picker_view.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>slide_out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
drop_button.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	picker_view.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>slide_in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	my_combo.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
cancel.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	picker_view.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>slide_out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I also added the click event on the <strong>cancel</strong> button to hide the <strong>picker_view</strong>.</p>
<h2>Filling the textField with the picker&#8217;s value</h2>
<p>The only thing we have left is to actually put the value of the picker in the <strong>my_combo</strong> textField when the user clicks the <strong>done</strong> button  and hide the <strong>picker_view</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">done.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	my_combo.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span>  picker.<span style="color: #660066;">getSelectedRow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
	picker_view.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>slide_out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <strong>getSelectedRow</strong> method of the picker is returning the selected row, and we use its <strong>title</strong>. The <strong>getSelectedRow</strong> argument is the <strong>index</strong> of the columns in the <strong>picker</strong>, and since we have only one, this is <strong>0</strong>.</p>
<h2>Download the project</h2>
<p>The Resource folder of the project can be downloaded from <a href="http://cssgallery.info/wp-content/combobox_tutorial.zip">here</a>.</p>
<p>Everything is MIT licensed, but as usual, spread the word <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>We have an update</h2>
<p><a href="http://twitter.com/CJ_Reed">@CJ_Reed</a> used this tutorial &#8220;to produce multiple selection type data entry in a single window.&#8221;<br />
Here is how it looks like:<br />
<img src="http://cssgallery.info/wp-content/uploads/2010/05/large_multiCombo.jpg" alt="to produce multiple selection type data entry in a single window." title="multiple selection type data entry in a single window." width="550" height="254" class="alignnone size-full wp-image-1251" /><br />
You can take a look at his code <a href="http://www.pastie.org/983594">here</a>.<br />
Great work!</p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
 <a href="http://nosoloweb.es/haciendo-una-lista-de-opciones-desplegables-en-titanium-appcelerator-codigo-y-video-2/" rel="me">Haciendo una lista de opciones desplegables en Titanium Appcelerator – Código y Vídeo</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1221"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fmaking-a-combo-box-in-titanium-appcelerator-code-and-video%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fmaking-a-combo-box-in-titanium-appcelerator-code-and-video%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fmaking-a-combo-box-in-titanium-appcelerator-code-and-video%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/seven-days-with-titanium-day-2-tables-and-pickers/' rel='bookmark' title='Seven days with Titanium – day 2 &#8211; tables and pickers'>Seven days with Titanium – day 2 &#8211; tables and pickers</a></li>
<li><a href='http://cssgallery.info/create-a-countdown-timer-with-titanium-appcelerator/' rel='bookmark' title='Create a countdown timer with Titanium Appcelerator'>Create a countdown timer with Titanium Appcelerator</a></li>
<li><a href='http://cssgallery.info/seven-days-with-titanium-day-5-gps-and-maps/' rel='bookmark' title='Seven days with Titanium – day 5 – GPS and Maps'>Seven days with Titanium – day 5 – GPS and Maps</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Create a countdown timer with Titanium Appcelerator</title>
		<link>http://cssgallery.info/create-a-countdown-timer-with-titanium-appcelerator/</link>
		<comments>http://cssgallery.info/create-a-countdown-timer-with-titanium-appcelerator/#comments</comments>
		<pubDate>Thu, 27 May 2010 13:37:18 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium Appcelerator]]></category>
		<category><![CDATA[appcelearor]]></category>
		<category><![CDATA[countdoun timer]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[reset timer]]></category>
		<category><![CDATA[set timer]]></category>
		<category><![CDATA[start timer]]></category>
		<category><![CDATA[stop timer]]></category>
		<category><![CDATA[timer]]></category>
		<category><![CDATA[titanium]]></category>
		<category><![CDATA[titanium appcelerator]]></category>
		<category><![CDATA[titanium tutorial]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1048</guid>
		<description><![CDATA[How to create a countdown timer for your iPhone with Titanium Appcelerator. Source code available for download. ]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fcreate-a-countdown-timer-with-titanium-appcelerator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fcreate-a-countdown-timer-with-titanium-appcelerator%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this post we will try to show you how to make a countdown timer using Titanium Appcelerator for Iphone or Android applications.  </p>
<p>First we will create an <strong>OOP countDown</strong> object that can be used independently in the background of the application and then we will connect it with some interface elements.</p>
<h3>The countDown object</h3>
<p>To have functional a countdown timer we&#8217;ll need to declare some properties and some methods.</p>
<p>As properties we will set:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">time<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span> m <span style="color: #339933;">:</span> minutes<span style="color: #339933;">,</span> s <span style="color: #339933;">:</span> seconds <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
total_seconds <span style="color: #339933;">:</span> m<span style="color: #339933;">*</span><span style="color: #CC0000;">60</span><span style="color: #339933;">+</span>s</pre></div></div>

<p>I think this is pretty clear, <strong>time</strong> is an object with minutes and seconds and <strong>total_seconds</strong> contains the number of seconds until we reach 00:00.</p>
<p>We will need to set methods that will allow us to:</p>
<ul>
<li><strong>set</strong> a time to count from</li>
<li><strong>stop</strong> the timer </li>
<li><strong>start</strong> it.</li>
</ul>
<p>We also ned to set some callback functions that will be executed on each tick of the timer   &#8211; <strong> fn_tick</strong> &#8211;  and one that will execute when the timer reaches 00:00 named <strong>fn_end.</strong></p>
<p>We won&#8217;t make a callback for start because we know when this occurs  we can trigger it by ourselves.</p>
<h2>The countDown timer will look like this:</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> countDown<span style="color: #009900;">&#40;</span> minutes<span style="color: #339933;">,</span> seconds<span style="color: #339933;">,</span> fn_tick<span style="color: #339933;">,</span> fn_complete<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>on this we will be able to apply</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">my_timer.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span> minutes<span style="color: #339933;">,</span> seconds <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
my_timer.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
my_tmer.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Below is the code that defines the countDown (pretty simple).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> countDown <span style="color: #339933;">=</span>  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> m <span style="color: #339933;">,</span> s<span style="color: #339933;">,</span> fn_tick<span style="color: #339933;">,</span> fn_end  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
		total_sec<span style="color: #339933;">:</span>m<span style="color: #339933;">*</span><span style="color: #CC0000;">60</span><span style="color: #339933;">+</span>s<span style="color: #339933;">,</span>
		timer<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">timer</span><span style="color: #339933;">,</span>
		set<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span>s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">total_sec</span> <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">60</span><span style="color: #339933;">+</span>parseInt<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">time</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>m<span style="color: #339933;">:</span>m<span style="color: #339933;">,</span>s<span style="color: #339933;">:</span>s<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		start<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">timer</span> <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">total_sec</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					self.<span style="color: #660066;">total_sec</span><span style="color: #339933;">--;</span>
					self.<span style="color: #660066;">time</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> m <span style="color: #339933;">:</span> parseInt<span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">total_sec</span><span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> s<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">total_sec</span><span style="color: #339933;">%</span>60<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
					fn_tick<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					self.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					fn_end<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #000066;">stop</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			clearInterval<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">timer</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">time</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>m<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>s<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">total_sec</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now to use it in our code we will do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span> <span style="color: #339933;">,</span>
	<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//something here...</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// something here...</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my_timer.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ok, the above example is useless, we will need to display the current time on each tick of the countdown times (so on each second) and at the end we&#8217;ll have to alert the user when we reached 00:00.</p>
<p>We will also need some interface elements to access the countdown methods and to display the current time.</p>
<p>For this we will create a label named <strong>display_lbl</strong>, that will show the time and also 3 buttons : <strong>set_btn</strong> , <strong>start_btn</strong> and <strong>stop_btn</strong>.</p>
<p>Creating the code is pretty simple so I won&#8217;t put the code here but you will be able to take a look at it <a href="http://cssgallery.info/wp-content/countdown_tutorial.zip">downloading the countdown project</a>.</p>
<p>What is most important is to connect the interface with the countdown timer.</p>
<p>So we create the timer  and set the <strong>fn_tick</strong> and <strong>fn_end</strong> functions</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> 
		<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			display_lbl.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> my_timer.<span style="color: #660066;">time</span>.<span style="color: #660066;">m</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; : &quot;</span><span style="color: #339933;">+</span>my_timer.<span style="color: #660066;">time</span>.<span style="color: #660066;">s</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The time is up!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is how we connect the buttons with <strong>my_timer</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
set_btn.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	display_lbl.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5 : 30&quot;</span><span style="color: #339933;">;</span>
	my_timer.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
stop_btn.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	my_timer.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
start_btn.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	my_timer.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you see we also set the label&#8217;s text to the initial value when we set the timer because otherwise we&#8217;ll loose the first second. We do this here to keep the timer object clean of any external interaction so we can use it even without an interface.</p>
<h2>This is how the final project looks like</h2>
<p><img src="http://cssgallery.info/wp-content/uploads/2010/05/capture-1.png" alt="Countdown timer with Appcelerator" title="Countdown timer with Appcelerator" width="464" height="558" class="alignnone size-full wp-image-1208" /></p>
<p>Please keep in mind that this is just a simple example and we don&#8217;t have a control mechanism to detect when the user stopped the timer or it reached by itself 00:00 &#8211; but you can add it as an exercise <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You can download the <strong>Resources</strong> folder of the  project from <a href="http://cssgallery.info/wp-content/countdown_tutorial.zip">here</a>.</p>
<p>Everything is MIT licensed and you can use it in any application you want to, but spreading the word would be nice.</p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
<a href="http://nosoloweb.es/crear-un-temporizador-regresivo-con-titanium-appcelerator/" rel="me">Crear un temporizador regresivo con Titanium Appcelerator</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1048"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fcreate-a-countdown-timer-with-titanium-appcelerator%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fcreate-a-countdown-timer-with-titanium-appcelerator%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fcreate-a-countdown-timer-with-titanium-appcelerator%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/' rel='bookmark' title='Making a combo-box in Titanium Appcelerator &#8211; code and video'>Making a combo-box in Titanium Appcelerator &#8211; code and video</a></li>
<li><a href='http://cssgallery.info/custom-row-for-tableview-in-appcelerator-titanium/' rel='bookmark' title='Custom row for TableView in Appcelerator Titanium'>Custom row for TableView in Appcelerator Titanium</a></li>
<li><a href='http://cssgallery.info/create-a-nifty-tooltip-in-titanium/' rel='bookmark' title='Create a nifty tooltip in Titanium'>Create a nifty tooltip in Titanium</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/create-a-countdown-timer-with-titanium-appcelerator/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Google Fonts tutorial</title>
		<link>http://cssgallery.info/google-fonts-tutorial/</link>
		<comments>http://cssgallery.info/google-fonts-tutorial/#comments</comments>
		<pubDate>Thu, 20 May 2010 11:43:57 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[fonts api tutorial]]></category>
		<category><![CDATA[free fonts]]></category>
		<category><![CDATA[google api]]></category>
		<category><![CDATA[google fonts api]]></category>
		<category><![CDATA[google fonts api tutorial]]></category>
		<category><![CDATA[google fonts example]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=1179</guid>
		<description><![CDATA[Google came with a new idea in their list of web toys.: a  font directory that allows you as a web developer to include some "fancy" fonts in your pages without struggle. We prepared a tutorial to teach you how to play with this new API and a test page so you can download the code and use it in your website.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fgoogle-fonts-tutorial%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fgoogle-fonts-tutorial%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Google came with a new idea in their list of web toys. A font directory that allows you as a web developer to include some &#8220;fancy&#8221; fonts in your pages without struggle.</p>
<h2>What is Google Fonts Directory all about?</h2>
<p>All the fonts in the Google Font Directory are licensed under open source licenses that let you use them on any website, from a private blog to a big commercial site. The idea seems to be good and comes to complete the Google AJAX Libraries API.</p>
<p>For now there are only a few fonts available but in time I think the list will grow.</p>
<h2>There are two ways to use this on your site</h2>
<p>The first one is simple and only requires a css file retrieved from Google&#8217;s servers like below:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; 
          href=&quot;http://fonts.googleapis.com/css?family=Tangerine&quot;&gt;
&nbsp;
    &lt;style&gt;
      body {
        font-family: 'Tangerine';
        font-size: 48px;
      }
    &lt;/style&gt;</pre></div></div>

<p>You need to specify the family of the font and if it contains spaces you need to replace this with a &#8220;+&#8221; (plus character) sign</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; 
          href=&quot;http://fonts.googleapis.com/css?family=Droid+Sans&quot;&gt;</pre></div></div>

<p>You can load multiple fonts in a single call. This will decrease the loading time because you use only one http request. The multiple fonts will be separated by a &#8220;|&#8221; ( pipe character ) sign.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; 
          href=&quot;http://fonts.googleapis.com/css?family=Tangerine|Inconsolata|Droid+Sans&quot;&gt;</pre></div></div>

<p>Normaly the fonts are loaded using the regular version but you have more control over the loaded font <strong>using some selectors</strong> for the style and weight, using a &#8220;:&#8221; ( colon character ) sign, and the values if more than one separated by a &#8220;,&#8221; ( comma )</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; 
          href=&quot;http://fonts.googleapis.com/css?family=Tangerine:bold,bolditalic&quot;&gt;</pre></div></div>

<p>The<strong> style</strong> and <strong>weight</strong> of the font can be specified using a fullname, an abbreviation or a numeric value ( for weight )</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">bold:b
bold:700
italic:i
bolditalic:bi</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; 
          href=&quot;http://fonts.googleapis.com/css?family=Cantarell:i|Droid+Serif:700&quot;&gt;</pre></div></div>

<p>Take into account that not all the fonts have the styles you might need so first of all verify in the font directory if it does. </p>
<h2>The second option</h2>
<p>The second option to load the fonts is to use the webloader (Javascript) that Google offer for this. This script can be called directly ( which is faster) or using their AJAX API functions. The main advantage of this method is that you can load fonts provided by TypeKit and Google or from any other font provider at the same time from the same library.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">WebFont.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    google<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      families<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'Tangerine'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Cantarell'</span> <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    typekit<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myKitId'</span><span style="color: #339933;">,</span>
&nbsp;
    custom<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> 
       families<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'OneFont'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'AnotherFont'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
       urls<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'http://myotherwebfontprovider.com/stylesheet1.css'</span><span style="color: #339933;">,</span>
       <span style="color: #3366CC;">'http://yetanotherwebfontprovider.com/stylesheet2.css'</span> <span style="color: #009900;">&#93;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The disadvantage of this method is the increased loading time comparing to the simple method.</p>
<h2>Other things to consider</h2>
<p>Loading external fonts is slow, at least the first time, until the font is cached on the visitor&#8217;s computer. Don&#8217;t overuse it.</p>
<p>You need to specify a fallback version for the font in case the browser is not supported. Here the browser uses a serif font in case it&#8217;s not able to load the &#8220;Tangerine&#8221; one.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  body <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Tangerine'</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">serif</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">48px</span><span style="color: #00AA00;">;</span>
      <span style="color: #00AA00;">&#125;</span></pre></div></div>

<h2>The crossbrowsing issue</h2>
<p>Each browser behaves in a different way, of course IE being the worse. To make a consistent behaviour across all the supported browsers you should use the webloader. This gives you the chance to display the styled elements with an initial font until the requested font file is loaded, and when this occurs to change it.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">    <span style="color: #6666ff;">.wf-inactive</span> p <span style="color: #00AA00;">&#123;</span> // Show paragraphs in <span style="color: #993333;">serif</span> <span style="color: #000000; font-weight: bold;">font</span> until fonts have loaded.
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">serif</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.wf-active</span> p <span style="color: #00AA00;">&#123;</span> // Show paragraphs in Tangerine when the fonts have loaded.
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Tangerine'</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">serif</span>
  <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>If the number of fonts will increase then Google Fonts Directory will be a good alternative compared to other webfonts providers that offer this as a paid service. But you still can have fun with it, I&#8217;m sure google will continue to improve this service.</p>
<h2>Check the demo</h2>
<p>A demo page can be seen <a href="http://cssgallery.info/wp-content/google-fonts-tutorial-demo.html">here</a>. Interesting to see how IE behaves. <img src='http://cssgallery.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>More resources</h2>
<p><a href="http://code.google.com/webfonts">Google Font Directory</a><br />
<a href="http://code.google.com/apis/webfonts/">Google Fonts Directory API</a></p>
<p>and of course </p>
<p><a href="http://typekit.com/">Typekit</a></p>
<p><strong class="spanish">Check the Spanish version of this post:<br />
<a href="http://nosoloweb.es/tutorial-de-fuentes-de-google/" rel="me">Tutorial de Fuentes de Google</a></strong></p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-1179"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fgoogle-fonts-tutorial%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fgoogle-fonts-tutorial%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fgoogle-fonts-tutorial%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<h3>Related posts:</h3><ol><li><a href='http://cssgallery.info/wacky-fonts-anyone/' rel='bookmark' title='Wacky fonts anyone?'>Wacky fonts anyone?</a></li>
<li><a href='http://cssgallery.info/5-free-retro-fonts-that-you-should-use/' rel='bookmark' title='5 free retro fonts that you should use'>5 free retro fonts that you should use</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/google-fonts-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>speedup your site &#8211; 2static.it</title>
		<link>http://cssgallery.info/speedup-your-site-2static-it/</link>
		<comments>http://cssgallery.info/speedup-your-site-2static-it/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 16:19:52 +0000</pubDate>
		<dc:creator>Dan Tamas</dc:creator>
				<category><![CDATA[Development tools]]></category>
		<category><![CDATA[2static]]></category>
		<category><![CDATA[cookieless]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[getify]]></category>

		<guid isPermaLink="false">http://cssgallery.info/?p=719</guid>
		<description><![CDATA[Since google launched pagespeed add-on for firefox, everybody is talking about how to speedup you site, but there are few instructions about one aspect : the cookieless domain. We should serve static components (css, images, javascript) with no cookies at all. Cookies are sent and received each time we make a request to the server. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin: 10px !important;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcssgallery.info%2Fspeedup-your-site-2static-it%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcssgallery.info%2Fspeedup-your-site-2static-it%2F&amp;source=dan_tamas&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Since google launched pagespeed add-on for firefox, everybody is talking about how to speedup you site, but there are few instructions about one aspect : the cookieless domain.</p>
<p>We should serve static components (css, images, javascript) with no cookies at all. Cookies are sent and received each time we make a request to the server. So from the bandwidth point of view, and of course load time for the user&#8217;s browser, this is bad.</p>
<p>More data about this <a href="http://code.google.com/speed/page-speed/docs/request.html#MinimizeCookieSize" target="_blank">here</a>.</p>
<p>After talking more with Kyle Simpson or <a href="http://twitter.com/getify" target="_blank">@getify</a> on twitter , the author of <a href="http://labjs.com/" target="_blank">labjs</a> about this, he came with the next comments:</p>
<blockquote><p>Most people don&#8217;t realize that &#8220;global cookies&#8221; (those that are set by session cookies and usually by analytics packages) creep over even into sub-domains of your main page domain.<br />
It&#8217;s very common for people to set up something like &#8220;static.mydomain.com&#8221; and load all their assets that way<br />
they think this protects those requests.<br />
It usually doesn&#8217;t because they have some analytics package that sets cookies like &#8220;.mydomain.com&#8221;&#8230; notice the leading &#8220;.&#8221;, it means that ALL subdomains are included.<br />
So it defeats the purpose of having your own sub-domain to &#8220;strip cookies&#8221;, so using an entirely separate domain is the only answer.</p></blockquote>
<h3>Now there are some solutions to address this issue:</h3>
<p>-  to use a CDN, and set it to remove cookies( which could be expensive)</p>
<p>- to use a cookieless server, and make a bunch of  changes in your httpd.conf( in the case of apache ), but this requires root access on the server, and for the majority of shared hosting solutions this is not available.</p>
<p>Another  easy and free solution would be to use <a href="http://2static.it/faq/" target="_blank">2static.it</a> ( in beta now ).</p>
<p>2static.it  is offering this as a <strong>FREE</strong> service, it strips the cookies, assigns you a DNS alias and makes the DNS redirect to your site.</p>
<p>This way the most complication you might have is to ask your hosting to create the alias for you, in case you cannot do it by yourself.</p>
<p>Starting form here all you need to do is to use the new alias for the static resources of your site.</p>
<p>I suggest you to read the faq page  as it explains in details what it does.</p>
<div style="clear:both;">&nbsp;</div><div class="shr-publisher-719"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' shr_layout='button_count' shr_showfaces='false' shr_href='http%3A%2F%2Fcssgallery.info%2Fspeedup-your-site-2static-it%2F'></a><a class='shareaholic-fbsend' shr_href='http%3A%2F%2Fcssgallery.info%2Fspeedup-your-site-2static-it%2F'></a><a class='shareaholic-googleplusone' shr_size='medium' shr_count='true' shr_href='http%3A%2F%2Fcssgallery.info%2Fspeedup-your-site-2static-it%2F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://cssgallery.info/speedup-your-site-2static-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (User is logged in)
Page Caching using disk: basic (User agent is rejected)
Database Caching using disk: basic

Served from: www.cssgallery.info @ 2012-02-04 20:09:51 -->
