I have two problems with the menu. The menu is created by .xml file.
example:
<Menu>
<MenuItem id = "search" onclick = "search" />
<MenuItem id = "add" onclick = "add" />
</ Menu>
1) The first problem is that sometimes strangely the menu is not loaded, that is, do not see the buttons. Can you tell me why.
2) The second problem is that I have a menuItem field normally not visible, only determiate conditions in the field will have to be visible. Could you tell me how I can have access to the field.
.xml
<MenuItem id = "profile" />
.tss
"#profile[platform=android]": {
title: "Profile",
icon: "/global/profile.png"
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS,
visible: false
}
.js
$ .profile.visible = True;
Error message: Can not set property visible of undefined.
I apologize for my bad English.
Thank you.
Edit:
Example:
var activity = $ .index.activity;
activity.onPrepareOptionsMenu = function (e) {
var favoriteGroup e.menu.add = ({
title: "Profile",
icon: "/global/profile.png"
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS,
});
favoriteGroup.addEventListener ('click', function () {
Alloy.createController ("favorite_group", args) .getView (). Open ();
});
};
activity.invalidateOptionsMenu ();
You will need to reload the menus created in Alloy XML. Mostly people create menus in .js file in window's open events so that they can be created only after a window is properly opened:
To create menus in .js file, do it like this:
- Solution using window.js only
var searchMenu, addMenu;
$.window.addEventListener('open', function () {
var activity = $.window.activity;
activity.onCreateOptionsMenu = function (e) {
searchMenu = e.menu.add({
title: 'Search',
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS
});
searchMenu.addEventListener('click', function () {
// open my profile here or do other tasks
});
addMenu = e.menu.add({
title: 'Add',
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS
});
};
// this is the code to validate menu items once they are added into an activity
activity.invalidateOptionsMenu();
});
- Solution using window.xml + window.js
So, to solve your issue no. 1, you might need to call this code in your controller file of that xml (activity is only available once a window is opened, so use open event):
window.xml
<Menu platform="android">
<MenuItem id="MENU_SEARCH" title="Search" onClick='search' showAsAction="Ti.Android.SHOW_AS_ACTION_ALWAYS" />
<MenuItem id="MENU_ADD" title="Add" onClick='add' showAsAction="Ti.Android.SHOW_AS_ACTION_ALWAYS" />
</Menu>
window.js
$.window.addEventListener('open', function () {
$.window.activity.invalidateOptionsMenu();
});
It could be due to 2 reasons:
window.js
$.window.addEventListener('open', function () {
setTimeout(function () {
$.profile.visible = true;
}, 400);
});
.tss
"#profile[platform=android]": {
title: "Profile",
icon: "/global/profile.png",
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS,
visible: false
}