I am having some trouble getting an options menu working in Android. I build apps before, and they all worked fine, but now the menu just doesn't pop up.
The code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_video, menu);
return true;
}
the whole method is never even called (checked by setting a breakpoint). The activity is super-simple, it just has a VideoView
in it, with an OnTouchListener
set.
I am using Android 4.0.4 on a Samsung Galaxy 10.1, API level 15, minSDK 15. Am I missing something?
Try This It works for me:---
If the phone you test on has a menu button onCreateOptionsMenu wont't be called on start with the theme:
android:theme="@android:style/Theme.Black.NoTitleBar"
But when you click the menu button the
onCreateOptionsMenu
will be called. I don't know what happens on phones without hardware buttons...Maybe you also have overrode onKeyDown method and made it always return true. Returning true means that keyEvent will be prevented from being propagated further. See code below:
I had a similar issue, but a different solution I am sharing with the community (as it took me one hour to understand what was happening):
where my_toolbar is an object created in my xml file through dataBinding.
The issue looks the same, no toolbar appear, no call to onCreateOptionsMenu.
My solution was to promote this code to the child class and not to that basic, as my_toolbar is only initialized as the child class is built.
In the latest versions of Android when using the compat library for toolbar, is very common that this happens, in order to get the menu items to display in the toolbar you must do the following:
I was having the same problem (menu not showing up,
onCreateOptionsMenu
not being called).If you are calling this within a fragment, you need to override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
notpublic boolean onCreateOptionsMenu(Menu menu)
.Fragments
do not use the latter, so they will never even call it.Activity menu
Fragment menu