Simple mobile app in Titanium Developer
Check out the new site at https://rkblog.dev.
25 February 2010
Comments
Now it's time to write some simple mobile app using Titanium Developer. Venturing in the APIs we some help - KitchenSink example app and documentation.
Application files and folders
Mobile app created in Developer will look like this:build CHANGELOG.txt LICENSE LICENSE.txt manifest README Resources tiapp.xml
<window>
<id>initial</id>
<url>index.html</url>
<backgroundColor>#FFFFFF</backgroundColor>
<icon>ti://featured</icon>
<barColor>#000</barColor>
<fullscreen>false</fullscreen>
</window>
about.html android index.css index.html index.js
How do you make an application?
It's quite strange to code a mobile app in HTML/CSS/JS. What you have to do is:- Make HTML templates
- Write some CSS styles
- Write JavaScript code that will handle the app functionality
The Mobile API isn't documented well, and you will have to digg in Kitchen Sink or do a lot of debuging, testing.
Simple mobile application
So we start with an application that will show RSS channel entries from a selected website (in this case filmaster.com). For comparison I'll make two windows - two tabs. One will show RSS entries using native widgets, and second one using HTML.- Create mobile project in Titanium Developer
- In tiapp.xml define two windows:
<windows> <window> <id>initial</id> <title>Native</title> <icon>static/native.png</icon> <url>index.html</url> <backgroundColor>#FFFFFF</backgroundColor> <barColor>#000</barColor> <fullscreen>false</fullscreen> </window> <window> <id>weblike</id> <title>Weblike</title> <icon>/static/weblike.png</icon> <url>weblike.html</url> <backgroundColor>#EE4508</backgroundColor> <barColor>#000</barColor> <fullscreen>false</fullscreen> </window> </windows>
- Create weblike.html, and add some initial code to this file and to index.html:
Where jquery.jfeed.pack.js is a jfeed jQuery plugin (RSS parsing), jquery.utils.min.js is a jquery-utils plugin (needed by me to handle UTF-8 strings with non-ascii characters - like Polish language ;)). The HTML/CSS page will show the site logo on orange background and message about loading RSS channel. index.js and weblike.js for the second window (tab) will contain code implementing their functionality.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Filmaster Mobile</title> <script type="text/javascript" src="static/jquery.js"></script> <script type="text/javascript" src="static/jquery.jfeed.pack.js"></script> <script type="text/javascript" src="static/jquery.utils.min.js"></script> <script type="text/javascript" src="index.js"></script> <link href="index.css" rel="stylesheet" type="text/css"/> </head> <body> <div id="logo"><img src="static/logo.png" alt="logo" /></div> <div id="rss"> Loading RSS Channel </div> </body> </html>
- Now it looks like this:
- I wrote such code for weblike.js:
The code uses jFeed plugin to download RSS channel and put the result in the page weblike.html.
window.onload = function() { html = ''; jQuery.getFeed({ url: 'http://filmaster.pl/planeta/rss/', success: function(feed) { for(var i = 0; i < feed.items.length && i < 5; i++) { var item = feed.items[i]; html = html + '<h4>' + item.title + '</h4><p>' + item.description + '</p>'; } jQuery('#rss').html(html); } }); };
- The window looks like this: . For Polish feed there are problems with non-ascii characters. Such problems can be solved by $.UTF8decode() from jQuery-utils.
- After the fix code looks like this:
window.onload = function() { html = ''; jQuery.getFeed({ url: 'http://filmaster.pl/planeta/rss/', success: function(feed) { for(var i = 0; i < feed.items.length && i < 5; i++) { var item = feed.items[i]; html = html + '<h4>' + $.UTF8decode(item.title) + '</h4><p>' + $.UTF8decode(item.description) + '</p>'; } jQuery('#rss').html(html); } }); };
- For index.js I wrote such code:
window.onload = function() { jQuery.getFeed({ url: 'http://filmaster.pl/planeta/rss/', success: function(feed) { var data = []; var desc_data = []; for(var i = 0; i < feed.items.length && i < 5; i++) { var item = feed.items[i]; data[i] = {title: $.UTF8decode(item.title), hasChild: true}; desc_data[$.UTF8decode(item.title)] = $.UTF8decode(item.description); } var tableView = Titanium.UI.createTableView({data:data,title:'Tytuł'}, function(eventObject) { // Now call some eventhandler function to handle the click event object // Took this from the Kitchen Sink var a = Titanium.UI.createAlertDialog(); a.setTitle(eventObject.rowData.title); a.setMessage(desc_data[eventObject.rowData.title]); a.show(); }); //add view to current window Titanium.UI.currentWindow.addView(tableView); // show view Titanium.UI.currentWindow.showView(tableView); } }); };
- Which in android gives:
- In this JavaScript code there is Titanium.UI.createTableView (doc) - native widget available through the API. This will show a native table view on each system. Clicking on an entry will show it's description through Titanium.UI.createAlertDialog widget - doc.
Source
Import project in Titanium Developer.
RkBlog
Check out the new site at https://rkblog.dev.
Comment article