Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:01:08+00:00 2026-05-25T19:01:08+00:00

I am building a simple application based off of the Google provided Note pad

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:01:09+00:00Added an answer on May 25, 2026 at 7:01 pm

    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…”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a simple web based forum application. I want to allow users to
I'm on the final stretch of my first simple iPhone application. I'm building an
Well, lets say I'm building ActiveMQ based chat application. It's pretty simple. Having only
I am building a web application on Google App Engine that requires users to
I'm building a Zend-based Web app that will permit third-party extensions. Essentially, this application's
I am building a simple book check out application. One of the things that
I'm about to finish building a simple subscription based support ticket Web app. I'm
I am building a simple application that has a few buttons that when clicked
I'm building a simple application. It's a user interface to an online order system.
Been thinking about this for hours now. Im building a simple slideshow application, where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.