In a recent project I worked with Titanium Appcelerator, a plattform to code once (using a Javascript-API) and publish twice (iOS and Android). So goes the story… In real life it is not sooooo easy, because while you are coding you realize that iOS and Android do have some differences.
For example: a little growl-like notification-window which fades out after a few seconds. For Android there is a Titanium-API-function, for iOS not.
Anyway, I just wanted to share a function which works for both:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
var isAndroid = (Ti.Platform.osname=='android') ? true : false; var isIphone = (Ti.Platform.osname=='iphone') ? true : false; /** * wrapper for Notification/Tooltip/MessageLayer on Android & iPhone * - shows a message (customMessage) for n (interval) miliseconds and then self closes * - iPhone: alternative to Android toast notification * * @param string customMessage * @param int interval */ showMessageTimeout = function(customMessage,interval){ if(isIphone){ // window container indWin = Titanium.UI.createWindow(); // view var indView = Titanium.UI.createView({height:150,width:250,borderRadius:10,backgroundColor:'#bbb',opacity:.8}); indWin.add(indView); // message var message = Titanium.UI.createLabel({ text: customMessage && typeof(customMessage!=='undefined') ? customMessage : L('please_wait'), color:'#fff',width:'auto',height:'auto',textAlign:'center', font:{fontSize:14,fontWeight:'bold'}}); indView.add(message); indWin.open(); interval = interval ? interval : 2500; setTimeout(function(){ indWin.close({opacity:0,duration:1000}); },interval ); } if(isAndroid){ var n = Ti.UI.createNotification({ message: customMessage && typeof(customMessage!=='undefined') ? customMessage : L('please_wait'), }); n.duration = Ti.UI.NOTIFICATION_DURATION_SHORT; n.show(); } } |
Source: http://developer.appcelerator.com/question/137662/android-toast-notification-in-iphone