Inside of my onCreateOptionsMenu function I implemented this:
mymenuitem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
public boolean onMenuItemClick(MenuItem item){
update_freq=1;
showChosen(); (some user defined function)
update_time();
return true;
}
});
However, the .setOnMenuItemClickListener only be called the first time I click my preference, later on when I went back to menu and click preference buttons, it never be called.
can anyone tell me what is the problem? My menu is written in xml file and inflated.
I think you need to override onOptionsItemSelected. The OnMenuItemClickListener is set on a single, specific MenuItem.
Related
I'm practising adding menu items and trying to react to menu item clicks. According to the developer's guide, it says:
Tip: Android 3.0 adds the ability for you to define the on-click behavior for a menu item in XML, using the android:onClick attribute. The value for the attribute must be the name of a method defined by the activity using the menu. The method must be public and accept a single MenuItem parameter—when the system calls this method, it passes the menu item selected. For more information and an example, see the Menu Resource document.
However, the sample code in the same page doesn't follow the rule: the methods do not pass the MenuItem parameter. The sample code is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
My question is: Shouldn't method calls be newGame(MenuItem item) and showHelp(MenuItem item), instead of newGame() and showHelp()? When I tested my own, (MenuItem item) argument was needed in fact, otherwise, the app was crashing, even though it compiles correctly.
Any help would be appreciated.
onOptionsItemSelected is the alternative to defining onClick attributes and what is available prior to Android 3.0 (important if you want to be backward compatible). It is simply a different way of providing the same process flow. Of course, onClick has the potential to crash your application on runtime, rather than onOptionsItemSelected not handling a menu item (simply causing it to do nothing).
When you create a button, you can assign a listener to capture user actions like clicks.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do some stuff
}
});
}
My question is why menu items don't need a listener? All I need is just write
getMenuInflater().inflate(R.menu.main, menu);
inside of
public boolean onCreateOptionsMenu(Menu menu).
and use
public boolean onOptionsItemSelected(MenuItem item)
to define behaviour when an Item is clicked. I didn't see any setListener method. Besides, the main activity class does not implement any Listener. How does a menuItem work?
How does a menuItem work?
So OptionsMenu is build-in widget in Android OS
It works simply said like when you'll click on an item then is immediately called "OnItemSelectedListener" (you don't need to implement it, it's automatic called whenever you'll click on the item). This listener is already implemented. You don't have to implement it and i think you shouldn't and can't.
An onOptionsItemSelected method is called by listener that is called when you'll click on the item.
And what docs exactly say:
This hook is called whenever an item in your options menu is selected.
The default implementation simply returns false to have the normal
processing happen (calling the item's Runnable or sending a message to
its Handler as appropriate). You can use this method for any items for
which you would like to do processing without those other facilities.
Derived classes should call through to the base class for it to
perform the default menu handling.
I see that it's possible to handle a tap on a icon menù item or by implementing
onOptionsItemSelected
inside the acivity, or by using
onMenuItemClickListener
like onclick listener on a button. When is better to use the fist one method, and when the second one?
Because for my opinion, using an external listener makes more modular the code, but create a new class, but using the first way don't create new class, but makes code less modular...
There are use cases other than the ones outlined below, but I'm putting in the general cases that come up regularly.
onOptionsItemSelected
If you're using Fragments, you may want to use onOptionsItemSelected and consider adding menu items to the Action Bar the way that is described in Adding items to the Action Bar.
What this describes is implementing onCreateOptionsMenu inside your Fragment. To make this happen, you must call setHasOptionsMenu in onCreate.
protected void onCreate(Bundle savedInstanceState) {
this.setHasOptionsMenu(true);
}
Setting this will actually make the Activity call onCreateOptionsMenu which allows you to add the menu items.
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
// add items corresponding to this Fragment
menu.add(...);
return true;
}
The reason I recommend this is that it allows you to put more of the menu handling code into your Fragment instead of the Activity to figure out which Fragment to call, etc.
In this case, clicking the menu item will call onOptionsItemSelected inside of your Fragment which I suggest.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_id1:
dothing1();
return true;
case R.id.my_id2:
dotghing2();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
More of a long winded answer, but this is the way to handle menu clicks inside your Fragment.
onMenuItemClickListener
In the case of onMenuItemClickListener, this is used when you DON'T want to use the pre-ready method above and implement your own.
What I mean by that is you implement OnMenuItemClickListener and generate the methods in the interface. You then assign the menu to call the Activity that implemented this where as the above option assumes what Activity to use based on the pre-ready implementation of the Activity to Fragment relationship.
If you are targeting API 14 or greater (ICS or above) you could implement an ActionProvider. If that's not an option then you could implement a base activity that will always populate the menu and handle any menu clicks using onOptionsItemSelected. This is a good approach to implement "About" or "Settings" menu items through all your activities.
I have a Save MenuItem. This has a onOptionsItemSelected(MenuItem item) function. It's triggered when I click on the Save menu. However, I want to call this function explicitely when the user tries to navidate to another activity without saving.
So basically how can I call this onOptionsItemSelected(MenuItem item) from another function?
Do one thing all the code you written in this method within for save just copy and paste in you created method for e.g.
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.save:
saveMe();
break;
}
}
private void saveMe(){
// write your save code here
}
now you can call this method when user navigate to another activity
Have a function named showSaveMenu() and inside it show the save menu if user has not saved already. Call this function from each possible exit point from the activity i.e. onBackPressed() or from any where you start another activity and also from onOptionsItemselected()....
just call invalidateOptionsMenu().
I have an Options menu up and running in my Android application and I've overridden the onCreateOptionsMenu, onOptionsItemSelected and onPrepareOptionsMenu methods to customize the menu a little.
My question is related to keeping the Options menu open after the user clicks on a menu item. Basically, I'd like to be able to hide the menu until the user clicks on the device menu key. Once the user clicks on this key, I'd like to be able to hold the menu in place regardless of how many times the user clicks on menu items. If the user wants to hide the Options menu, they'd just need to click on the device menu key again.
Is this type of interaction supported (or even advisable). If this interaction is not supported, any alternative suggestions are more than welcome.
Cheers!
Sean
This will not be possible with onCreateOptionsMenu and the other methods. They always act that way.
But you can do it another way. But there you have to program the whole menu yourself. Basically add the Menu in your layout.xml and let it be hidden (visibility = gone). Then you overwrite the methods onKeyDown. There you check if it is the Menu key. if the menu is not yet open yes, then you show the menu. If it is open already, you hide it.
should not be too difficult. Good thing as well is, that you can make the menu look exactly the way you want and as well let it react the way you want.
For anybody like me, who found this question in google:
To keep menu open after selecting item, you need this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(!item.isChecked());
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
Important to return false in onOptionsItemSelected and methods of OnActionExpandListener
This solution from #MayurGajra. More details here: How to hold the overflow menu after I click it?