How to access Menu using Menuinflater [duplicate] - android

This question already has answers here:
create new menuInflater or getMenuInflater() from activity?
(3 answers)
Closed 8 years ago.
I am new to android and would like to create menu using menu inflater. To access menu on android emulator, one could simply press the menu button and menu would get displayed. However on a real device, how can I access the menu using menu inflater?
It probably sounds silly but like I said before, new to android.
The app's min. api level is 8.
Thanks

Here is the working link for it :
http://androidcodejunction.wordpress.com/2012/07/25/ok/

Use this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuInflater exampleInflater = getActivity().getMenuInflater();
exampleInflater.inflate(R.menu.example_menu, menu);
}
The "example_menu" being the name off the xml you have in the "menu" folder. Also there are many duplicates of this question.

Related

What is the menuInflater in Android Studio?

I've been studying Kotlin and Android development, and studying the code samples in Android Studio, I've encountered this block:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
I know that first you have to instantiate a variable for the inflater to use the inflate() method, but there is no menuInflater variable in this code. Looking at it, I see that it is similar to getMenuInflater() but I don't get how this is possible. I looked at the documentation and I have not found any explanation. Is menuInflater a variable, class, method?
Thank you in advance for the answer.
menuInflater is an object you can take from activity since activity and Menu class which you take menuInflater
shares common classes in the inheritance hierarchy. You can get the menuInflater object in activity, and it gives you chance
to get a menu file to your activity and use it there, Inflaters are mostly used to get some layouts and use it in another
layout e.g LayoutInflater, MenuInflater
MenuInflater is class, You can find detailed Documentation Here
Also you can find a simple example for MenuInflater Here
It's Kotlin's syntactic sugar. getXxx() Java method can be invoked as if it was a Kotlin xxx property.
Reference: https://kotlinlang.org/docs/java-interop.html#getters-and-setters

menuItem.getActionView() == null in API 15

I'm supporting down to API 15 in my app, and I'm getting a crash for those users when I try to get the searchView from my menu.
Below is my code:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.getItem(0);
searchView = (SearchView)menuItem.getActionView();
searchView.setIconifiedByDefault(false);
searchView.setQuery("", true);
menuItem.expandActionView();
}
I'm getting a NullPointerException on this line:
searchView.setIconifiedByDefault(false);
because the searchView is null. This works perfectly fine on devices at API 16 and above. Has anyone run into this issue before?
While inflating a layout usually causes an immediate crash if there is a problem, inflating a menu resource does not. If there is some problem, a stack trace is logged, but otherwise the exception is handled, and so execution continues. It is only some time later that we realize that something did not work, when things break later on.
Custom action bar items (actionLayout, actionViewClass, actionProvider) are especially prone to this. If there is some problem loading any of those -- such as the actionViewClass not implementing the proper constructor -- we only find out about it when we try to retrieve the custom item and get null back. The solution is to rummage through LogCat and look for the stack trace associated with the handled exception, to see what really went wrong.
In an API level-dependent case, like this one, the most likely scenario would be where initialization of custom action item refers to a method that does not exist on the older version of Android, and therefore fails.
I also had just the same problem as you and came to this stackoverflow question. With some struggle, I have found the heart of this problem and a solution.
In API15, during app's initialization, only onCreateOptionsMenu is called but onPrepareOptionsMenu is not.
In API16 and later, onPrepareOptionsMenu is called right after onCreateOptionsMenu.
So my solution is to call onPrepareOptionsMenu at the ending point of onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
(...)
if (Build.VERSION.SDK_INT < 16) {
onPrepareOptionsMenu(menu);
}
}

Error with onCreateOptionsMenu

Getting an error and not sure why.
I'm trying to get icons beside the icon of my app and the title of my app at the top of the screen.
I've tried to implement the onCreateOptionsMenu method however I'm getting this error.
'The method inflate(int, Menu) in the type MenuInflater is not applicable for the arguments (int, Menu).
This is the code I tried
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.testmenu, menu);
return true;
}
I've read that sometimes it can be related to the theme or the SDK version you're trying to target however I've checked both and they're fine. Version 11 is the min and I have the Holo.Light theme currently
Are you using ActionBarSherlock? If so, you have to use
getSupportMenuInflater().inflate(R.menu.testmenu, menu);

android - change options menu dynamically , but by inflating from XML

i need to be able to change the options menu (the one that is shown upon pressing the menu button) on android , so that on one case (for example upon a button being pressed) , it will use a specific menu resource (XML file as in /res/menu/... ) for the menu , and on another case , use a different XML file.
so far i've seen only examples of doing it without xml (example here and here) , and they worked fine , but i want to be able to change the entire menu on some cases.
i've tried to modify the solutions i've found , but none of my trials worked.
if possible , i would prefer to re-create the menu only if the it needs to be updated with a menu resource that is different from the current one.
please help me.
If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method.
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (CASE_1 == 0) {
CASE_1 = 1;
getMenuInflater().inflate(R.menu.secondmenu, menu);
}
else {
CASE_1 = 0;
getMenuInflater().inflate(R.menu.firstmenu, menu);
}
return super.onPrepareOptionsMenu(menu);
}
where CASE_1 refer to the which menu you want to display depending on your requirement.

Android OnCreateOptionsMenu

I've made menu's before, so I'm not a complete rookie, but I've encountered a problem where Eclipse tells me I have to remove #Override, but obviously if I do then my code won't run to inflate the menu. the code is:
#Override
public boolean OnCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
return super.onCreateOptionsMenu(menu);
}
The "on" in "OnCreateOptionsMenu" should not be capitalized
You can add overridden methods in eclipse by going Source > Override/implement methods and then check the methods you want to override,it helps with the spelling and all.

Categories

Resources