VenkateshRaghavan
VenkateshRaghavan

Reputation: 291

Android Menu options

I want to create Option menu and display the menu items in option menu in List order..

Menu 1

Menu 2

Menu 3

Upvotes: 0

Views: 3391

Answers (4)

Alex B
Alex B

Reputation: 407

You may want to look at http://solutionsinhand.com/android/MenuDialogDemo.zip. Using MenuDialog you can create all kinds of menus.

Upvotes: 0

Pierre-Luc Paour
Pierre-Luc Paour

Reputation: 1765

I struggled to do the same thing, and here's what ended up working. Basically, instead of displaying the options menu, I used a context menu.

First, you need to capture the menu key and cause it to show a context menu:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        View view = findViewById(R.id.for_context);
        registerForContextMenu(view);
        openContextMenu(view);
        unregisterForContextMenu(view);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

You have to attach this context menu to a view in your window, any view that doesn't have its own real context menu. I used one of the layouts I had in the window.

Then, inflate the menu:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    if (v instanceof TableLayout) {
        inflater.inflate(R.menu.my_context_menu, menu);
    }
}

my_context_menu.xml is defined exactly like an options menu.

The last step is to handle menu presses in onContextItemSelected(MenuItem item), which you can redirect to onOptionsItemSelected(MenuItem item).

Of course, this applies only if you want to display your menu like a context menu: no icons, just menu labels in a vertical list.

Upvotes: 5

springrolls
springrolls

Reputation: 1331

Have you read this? Reading your question, I don't think you've even googled it in the first place..

Upvotes: 2

Related Questions