To make sure your lay-outs look pretty much the same on all devices, including the thousands of different Android resolutions and densities, it's always best to use the 'dp' unit. However, typing '10dp'
instead of 10
is quite a pain. A pain you can easily take away by changing the default unit in your tiapp.xml
.
var v = Ti.UI.createView(); | |
var s = new require('spinner').Spinner( | |
v, // View to spin | |
30 // Degrees to spin per millisecond | |
); // Auto-starts | |
// Stop | |
s.stop(); |
The Titanium docs tell you Ti.version
is a number, but it's not... it's a string, that can include stuff like beta, GA etc.
- Thanks to Mads Møller for bringing the string-thing to my attention.
- Thanks to TheAzureShadow for the original code.
I like easy drop-in CommonJS libraries that need minimal configuration to perform common tasks.
This CommonJS library checks with Apple every X days to see if there's a new version available. If so, it asks the user if he/she would like to update and then opens the app in the App Store.
At minimum, the library requires no configuration at all and just a single line of code.
Look at advanced.js
for all available options and methods.
Also check out my rating library:
If you want to your CommonJS modules to work in both Alloy and plain Titanium projects, you might need a way to detect if you're in Alloy. For instance, if you're in Alloy you would get Underscore from the alloy-module, while in plain Titanium you would require Underscore directly.
Well, you can:
var _ = require((typeof ENV_TEST === 'boolean') ? 'alloy' : 'underscore')._;
The above works by utilizing Alloy's optimization process. In this process, constants like ENV_TEST
will be either TRUE
or FALSE
. The actual expressions in wich they are used will then be evaluated. If FALSE
the code block will be removed. In plain Titanium projects the constants are undefined and this typeof ENV_TEST
will be undefined
, so the code block will be executed.
Often I need to display a user-provided picture in an ImageView in such a way that the whole ImageView is filled with as much of the picture possible.
This is how I do it:
var image = require('image');
Ti.Media.showCamera({
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
success: function (e) {
If you have a CommonJS module that exposes properties that are expensive to create (e.g. other CommonJS modules or some external resource), you can load them lazy using Object.defineProperty()
.
I guess just like me you like to do alert('My message');
instead of:
Ti.UI.createAlertDialog({
message: 'My message'
}).show();
But I'd even more like to do alert('My message', 'My title', myCallback);
instead of: