0

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

Menu 1

Menu 2

Menu 3

VenkateshRaghavan
  • 291
  • 1
  • 5
  • 5

4 Answers4

5

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.

Pierre-Luc Paour
  • 1,725
  • 17
  • 21
  • your train of thought are all very well,but i cannot understand R.id.for_context,i donot want to show this view,can you give more detail or example thanks – pengwang Apr 14 '11 at 05:19
  • R.id.for_context is just one of your views that does not itself have a context menu, and which you can therefore use to get Android to create a context menu (context menus need to be attached to a view). – Pierre-Luc Paour Apr 14 '11 at 09:17
  • thank you ,your method very good,i think R.id.for_context,if this for_context is a linearlayout and other groupView,the result looks more bettet than textview and button, because if your use textView,it appear not smooth. – pengwang Apr 15 '11 at 04:03
2

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

springrolls
  • 1,331
  • 1
  • 14
  • 40
  • +1 I agree, and the link has a helpful explanation. Regardless of whether you've previously searched for an answer, this should solve it. – Matt Jan 03 '11 at 16:36
  • I know that..I wanna to display the menu items from top to bottom like List view not in ordinary manner as side by side.. Try to understand my question n reply.. – VenkateshRaghavan Jan 03 '11 at 16:43
  • For us to understand your question, you have to write a question beforehand. – Kevin Gaudin Jan 03 '11 at 16:53
1

Duplicate of:

Android Option Menu

Custom options menu in Android

Android: customize application's menu (e.g background color)

Community
  • 1
  • 1
Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
0

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

Alex B
  • 347
  • 1
  • 3
  • 9