Skip to content

Instantly share code, notes, and snippets.

@mitulbhalia
Created January 6, 2014 13:42
Show Gist options
  • Save mitulbhalia/8283041 to your computer and use it in GitHub Desktop.
Save mitulbhalia/8283041 to your computer and use it in GitHub Desktop.
// ==============================================================================
// Create custom loading indicator
// ==============================================================================
var indWin = null;
var actWin = null;
var indicatorShowing = false;
var actInd;
var actIndAndroid;
var screenWidth = Titanium.Platform.displayCaps.platformWidth;
var screenHeight = Titanium.Platform.displayCaps.platformHeight;
function showIndicator(title, color) {
indicatorShowing = true;
// add iphone elements
if (Titanium.Platform.name == 'iPhone OS') {
// window container
indWin = Titanium.UI.createWindow({
height :screenHeight,
width : screenWidth,
zIndex : 0
});
// black view
var indView = Titanium.UI.createView({
height : 50,
width : 125,
borderRadius : 10,
backgroundColor : color,
opacity : 0.7
});
indWin.add(indView);
// loading indicator
actInd = Titanium.UI.createActivityIndicator({
style : Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN,
font : {
fontFamily : 'Helvetica Neue',
fontSize : 15,
fontWeight : 'bold'
},
color : 'white',
message : title,
width : 210
});
actInd.show();
indWin.add(actInd);
indWin.open();
} else {
actIndAndroid = Titanium.UI.createActivityIndicator({
bottom : 10,
height : 50,
width : 10,
style : Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});
actIndAndroid.show();
actIndAndroid.message = 'Loading...';
}
}
function hideIndicator() {
if (Titanium.Platform.name == 'iPhone OS') {
if (indicatorShowing == true) {
if (actInd) {
actInd.hide();
}
if (indWin != null) {
//Ti.UI.currentWindow.remove(indWin);
indWin.close();
indWin = null
}
indicatorShowing = false;
}
} else {
if (actIndAndroid) {
actIndAndroid.hide();
indicatorShowing = false;
}
}
}
var indicator = {
}
exports.registerListener = function() {
// ==============================================================================
// Add global event handlers to hide/show custom indicator
// ==============================================================================
Titanium.App.addEventListener('show_indicator', function(e) {
if (e.title == null) {
e.title = 'Loading... ';
}
if (e.color == null) {
e.color = "#000";
}
if (indicatorShowing == true) {
// return;
hideIndicator();
}
showIndicator(e.title, e.color);
});
Titanium.App.addEventListener('hide_indicator', function(e) {
hideIndicator();
});
}
exports.indicator = indicator;
===================================================
app.js
var indicator = require('indicator');
indicator.registerListener();
=============================
to show indicator just call Ti.App.fireEvent("show_indicator"); from any window.
to hide call Ti.App.fireEvent("hide_indicator");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment