Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android OptionMenu missing from 1.5 CI builds

I'm trying to use the Android OptionMenu in the latest 1.5 CI build with the same code that worked in 1.4:

function createMenu() {
    var menu = null;
    if (isAndroid) {
        menu = Ti.UI.Android.OptionMenu.createMenu();
    }
    //refresh button.
    var btnRefresh = null;
    if (!isAndroid) {
        btnRefresh = new Button({ title: 'Refresh', style: Ti.UI.iPhone.SystemButtonStyle.BORDERED });
    } else {
        btnRefresh = Ti.UI.Android.OptionMenu.createMenuItem({ title: "Refresh" });
    }
    btnRefresh.addEventListener('click', function () { getVehicles(true); });
    //add menu to toolbar.
    if (!isAndroid) {
        var flexSpace = new Button({ systemButton: Ti.UI.iPhone.SystemButton.FLEXIBLE_SPACE });
        win.setToolbar([flexSpace, btnRefresh, flexSpace]);
    } else {
        menu.add(btnRefresh);
        Ti.UI.Android.OptionMenu.setMenu(menu);
    }
}

However it errors on OptionMenu.createMenu():

TypeError: Cannot find function createMenu in object [Ti.Android.OptionMenu]

Is this something that was left out on accident or is there new syntax for creating android menus now?

— asked December 7th 2010 by Justin Toth
  • android
  • menu
1 Comment
  • It's been deprecated. Menus are now tied to Ti.actvity. I stripped out all my old menu codes… Here is an example of how I fixed it in my code.

    var tb1 = null;
    var tb2 = null;
    
    var menuHandler = function() {
        tb1.addEventListener('click', function() {
            prowin.fireEvent('open');
        });
        tb2.addEventListener('click', function() {
            //todo : build it!
        });
    };
    
    var activity = Ti.Android.currentActivity;
    activity.onCreateOptionsMenu = function(e) {
        var menu = e.menu;
        tb1 = menu.add({title : 'Refresh'});
        tb2 = menu.add({title : 'Comment Users Lookup'});
        menuHandler();
    };
    
    — commented December 7th 2010 by Patrick Glass

1 Answer

  • Accepted Answer

    I had to remove that API. I'm in the process of documenting the new one. If you look at the updated KS example map_view.js you can see how I reworked it for the new API.

    Basically, two callbacks are exposed on Ti.Android.currentActivity. onCreateOptionsMenu and onPrepareOptionsMenu. These map one to one with the underlying methods on the Android Activity class. onCreateOptionsMenu is only called once and only in response to the menu button being pressed. onPrepareOptionsMenu is called each time before the menu is shown to the user. The menu proxy is passed in the argument as e.menu it is ok to hold onto this proxy outside of the method for the lifetime of the activity (heavyweight window). A MenuItem is created via add on the menu proxy. Look at line 203. Notice how I capture the proxy and store it so I can wire up the handlers in the wireClickHandlers() method which can be used for both iPhone and Android because MenuItems and Buttons respond to the click event.

    It's not shown in the example, but to set an icon add a menu item then menu.setIcon(path-to-icon)

    — answered December 7th 2010 by Don Thorp
    permalink
    1 Comment
    • Works great, thanks! 1.5 looks good, keep up the good work.

      — commented December 7th 2010 by Justin Toth
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.