I need to edit the value of a MenuItem in my Android application.
But the problem is that i don’t want the item edited inside OnCreateOptionMenu, but i want it edited outside MenuHandling routines.
I tried to use that code but it doesn’t work:
Menu myMenu = (Menu) findViewById(R.id.optionsmenu);
menuItem = myMenu.findItem(R.id.timer);
menuItem.setEnable(false);
But it doesn’t work the myMenu variable value is null (i added in an id attribute in the Menu xml tag).
I also tried something like that:
Menu myMenu = (Menu) findViewById(R.menu.options);
menuItem = myMenu.findItem(R.id.timer);
menuItem.setEnable(false);
Same problem.
I also tried:
MenuItem myItem = (MenuItem) findViewById(R.id.timer);
myItem.setEnabled(false);
Same problem myItem is null. Then how i can modify the MenuItem? I know how to id inside the On*OptionsMenu methods. I nedd to do it outside these method.
Thanks in advance for any help/reply.
override the
onPrepareOptionsMenu()in your activity.Refer this doc:
http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu