Question

I am building a simple application based off of the Google provided Note pad app. One of my first steps is converting the app to use XML menus, where possible. In the main activity, the notes list, I am using MenuInflater to show the default 'Compose' menu option:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // menu initialization, use the baseline menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.noteslist, menu);

    // generate any additional actions that can be performed on the
    // overall list.  In a normal install, there are no additional
    // actions found here, but this allows other applications to extend
    // our menu with their own actions.
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);

    return true;
}

With noteslist.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
      android:icon="@drawable/ic_menu_compose"
      android:title="@string/menu_compose"
      android:alphabeticShortcut="c"
      android:numericShortcut="3" />
</menu>

Everything works fine. Now, per the example (and modified as the example tries to add intent options when there are no items selected on the list as well), I want to add some additional options if there are items in the list, and one of them is selected:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    super.onPrepareOptionsMenu(menu);

    // determine if we have any items in the list via the ListAdapter
    final boolean haveItems = (getListAdapter().getCount() > 0);

    // do we have items?
    if (haveItems) {
        // there are items, check if any are selected
        //Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
        if (getSelectedItemPosition() >= 0) {
            // an item is selected, add the intents for one of our list items to the menu
            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

            // build menu on the fly... always starts with the EDIT action
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];

            // now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);

            // finally, add a shortcut to the edit menu item
            if (items[0] != null) {
                items[0].setShortcut('1', 'e');
            }
        }
    } else {
        menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
    }

    return true;
}

Finally, the referenced AndroidManifest.xml, unchanged from the default for the demo app:

http://developer.android.com/resources/samples/NotePad/AndroidManifest.html

So, I have two questions:

1) The resulting menus, Edit Note and Edit Title, generated by onPrepareOptionsMenu() when an item is selected, use the default icon and have no shortcuts assigned. I can set the intent-filter to have a different icon via android:icon="", but no such luck with assigning alphabetic and numeric shortcuts... I'd like to specify these, and was hoping that there might be a way to define these menu items in XML, and when they are to be brought in by the app via being identified by the intent-filters, also pull the XML and inflate/import it somehow. Any suggestions or pointers?

2) In onCreateOptionsMenu(), why is the addIntentOptions() call with CATEGORY_ALTERNATIVE NOT adding the activities with intent-filter set to category.ALTERNATIVE to the menu (not adding is correct behavior in this case, just trying to get my head around how the virtually identical calls to addIntentOptions() in onCreateOptionsMenu() and onPrepareOptionsMenu() result in different menus).

Was it helpful?

Solution

In an effort to close this question, I'll post CommonsWare's solution from the above comments, which is that in essence, Android development has moved away from using Intents to generate menu items due to menu pollution, as indicated on this thread from a member of the Android team:

"...We moved away from this approach because managing the UI for an arbitrary number of additional items is a challenge..."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top