0

Possible Duplicate:
How to open the options menu programmatically?

I read through quite a few posts from here and i found a code which i might use it to help me in showing the menu option instead of click the menu button from the emulator. but i do not know where to place the code in.

This is the code:

Activity.openOptionsMenu();

Is it placed in the onCreate method?

Community
  • 1
  • 1
Sarah Phil
  • 263
  • 2
  • 8
  • 16

2 Answers2

2

Place it in the method onAttachedToWindow() which is called when the activity and it's views have been created and the window attached to the screen. (Your issue is that the menu doesn't exist in onCreate, as it is still being initalised).

You will want to do something like this in your activity:

public void onAttachedToWindow() {
    openOptionsMenu(); 
}
Baz
  • 36,440
  • 11
  • 68
  • 94
biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • Thanks it works! but... how do i make it permanently there cause when i click on the other area the menu option closed. =( – Sarah Phil Sep 17 '12 at 14:23
1

Apparently it doesn't work if you do it in the onCreate method. Check out these posts:

https://stackoverflow.com/a/8676419/349012

https://stackoverflow.com/a/10220312/349012

Community
  • 1
  • 1
manavo
  • 1,839
  • 2
  • 14
  • 17
  • Then what does work? Please summarize links to other questions/answers when posting. – Baz Sep 17 '12 at 14:15
  • This works. but is it possible if the menu bar is static because when i click other area, the menu bar disappear which i dont want it to happen – Sarah Phil Sep 17 '12 at 14:35
  • As far as I know you can't have it permanently open. If you want it to be always open, you might have to add it in your layout, and make it look like the menu. Why would you want it open all the time anyway though? – manavo Sep 17 '12 at 14:37
  • oohh. because my tablet do not have menu button – Sarah Phil Sep 17 '12 at 14:38
  • If it doesn't have a menu button, then trying to use the options menu is kinda wrong altogether, right? :) If you're developing for tablets, then you might want to look into using the ActionBar instead of the options menu! Here's the reference in the Android docs: https://developer.android.com/guide/topics/ui/actionbar.html – manavo Sep 17 '12 at 14:41
  • I need to provide for phones too thats the reason why i chose option menu – Sarah Phil Sep 17 '12 at 14:46
  • i read through actionbar but im not sure how does it work. is it the same as option menu? im still new to android – Sarah Phil Sep 17 '12 at 14:48
  • 1
    Ah, if you need both, the best option might be http://actionbarsherlock.com/. Option menus are slowly being deprecated by Android in favour of the ActionBars. But, like you said, older versions don't support the actionbar. So, using the library above, it gives older versions the same actionbar functionality and you can have a consistent interface for all versions! :) If you'd still rather have the menu and not mess with the actionbar (as it is a bit more complicated), you could maybe just add a button on your activity that manually opens the menu? – manavo Sep 17 '12 at 14:55
  • means i need to go with both? But i try to put this is the menu item android:showAsAction="ifRoom|withText" and it tells me this error No resource identifier found for attribute 'showAsAction' in package 'android' – Sarah Phil Sep 17 '12 at 14:57
  • Implementing ActionBarSherlock is probably going out of the scope of this question and we probably shouldn't clog up the comments with it. The usual problem with that I think is the target SDK that you've set. It eplains here: http://actionbarsherlock.com/usage.html that you need to compile with SDK 4.0 for this to work, so it's probably a problem with that (if it's not recognised) – manavo Sep 17 '12 at 15:04
  • is it possible if i create my own actionbar? – Sarah Phil Sep 17 '12 at 15:15