Last active
December 20, 2015 13:59
-
-
Save ricardoalcocer/6143441 to your computer and use it in GitHub Desktop.
Dynamically changing menu options on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this goes on the open event of your tabgroup | |
// what happens here is that you create a single function for the menu options of all tabs | |
// then upon focus of a tab, you call invalidateOptionsMenu, which triggers the onCreateOptionsMenu event | |
// | |
if (OS_ANDROID){ | |
var tabGroup=Alloy.Globals.tabgroup; | |
var activity = tabGroup.getActivity(); | |
if (activity.actionBar) { | |
activity.actionBar.title = "Title"; | |
} | |
activity.onCreateOptionsMenu = function(e) { | |
var item, menu; | |
menu = e.menu; | |
menu.clear(); | |
switch(tabGroup.activeTab.title){ | |
case 'Schedule': | |
item = menu.add({ | |
title : "Tab1 Item", | |
icon : Ti.Android.R.drawable.ic_menu_search, | |
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS | |
}); | |
item = menu.add({ | |
title : "Test", | |
showAsAction: Ti.Android.SHOW_AS_ACTION_NEVER | |
}); | |
break; | |
case 'Stream': | |
item = e.menu.add({ | |
title : "Share", | |
showAsAction : Ti.Android.SHOW_AS_ACTION_ALWAYS, | |
icon : Ti.Android.R.drawable.ic_menu_share | |
}); | |
item.addEventListener("click", function(e) { | |
var txt = "x"; | |
var intent = Ti.Android.createIntent({ | |
action : Ti.Android.ACTION_SEND, | |
type : "text/plain" | |
}); | |
intent.putExtra(Ti.Android.EXTRA_TEXT, txt); | |
intent.addCategory(Ti.Android.CATEGORY_DEFAULT); | |
try { | |
Ti.Android.currentActivity.startActivity(intent); | |
} catch (ex) { | |
Ti.UI.createNotification({ | |
message : 'WTF dude' | |
}).show(); | |
} | |
}); | |
break; | |
} | |
// these are common options | |
item = menu.add({ | |
title : "About", | |
showAsAction: Ti.Android.SHOW_AS_ACTION_NEVER | |
}); | |
item = menu.add({ | |
title : "Credits", | |
showAsAction: Ti.Android.SHOW_AS_ACTION_NEVER | |
}); | |
} | |
tabGroup.addEventListener("focus", function(e) { | |
activity.invalidateOptionsMenu(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment