Monday, May 13, 2013

Cross Platform Development with Titanium - differences between iOS and Android pt1

Developing Android apps with Titanium is a lot of fun.  As I get deeper into developing on my current Android app, it struck me that whilst the documentation provided by Appcelerator on Titanium is generally excellent, there are some best practice steps and "gotchas" that are not highlighted and are worthy of exposition.  I thought I would use this blog to share some of them. I intend to expand on these in future posts.  Here is a simple one to start with;

Screen Resolution

In Titanium 2.0, Appcelerator introduced a new layout system.  In addition to this, they introduced a concept called Universal Unit Support, allowing the developer to specify units for size and position values. 

The following table lists the supported units:

Unit
Abbreviation
Note
pixels
px

density-independent pixels
dip or dp
Equivalent to Apple "points."
inches
in

millimeters
mm
Android, iOS only
centimeters
cm
Android, iOS only
points
pt
1/72 of an inch. Android only. Not to be confused with Apple "points."
On Android, a density-independent pixel (DIP) corresponds to one pixel on a 160DPI display.
On iOS, a DIP corresponds to one pixel on a non-Retina display, which
is 163DPI for iPhone/iPod touch and 132DPI for the iPad. A DIP
corresponds to 2 pixels of width or height on a Retina display.
On Mobile Web, both DIP and pixel are equal to one browser pixel, which may
not correspond to a pixel on the display. From testing, most browsers seem to
report a DPI value of 96DPI, regardless of the actual display density.
The absolute measures, such as inches, are dependent on the device correctly reporting
its density. On Mobile Web, these values are unlikely to be exact.
If no units are specified, a system-default unit is assumed. The system default unit is:
  • pixels on Android
  • DIP on iOS
  • DIP on Mobile Web
When developing cross platform applications, you can save yourself time (and write less code) simply by changing the default units to DIP to make it consistent across the different platforms you are working on.  This is done simply by changing the default unit setting in tiapp.xml;

<property name="ti.ui.defaultunit" type="string">dip</property>

This is a not a silver bullet, but experience has shown me when developing an Android/iOS cross platform app, this should be your default setting.



Friday, May 3, 2013

Mobile App Development with Titanium - plist entries

In the past whenever I have wanted to modify a plist entry for a titanium app, it has involved building the app and then copying back the info.plist file from the build folder into my project root.  I discovered today that you can simply edit the tiapp.xml with the plist dictionary items you need.  The following is an example for adding custom fonts in iOS



<ios>
    <plist>
        <dict>
            <key>UIAppFonts</key>
            <array>
                <string>fonts/Handlee-Regular.ttf</string>
                <string>fonts/Oswald-Bold.ttf</string>
                <string>fonts/Oswald-Light.ttf</string>
                <string>fonts/Oswald-Regular.ttf</string>
            </array>
        </dict>
    </plist>
</ios>