Change another MenuItem onClick of one - java

My problem is that I want to change the icon from a MenuItem from Visible to not being Visible, but this ofcourse needs to go via the onOptionsItemSelected. If I call on menu, it gets the MenuItem where is clicked on, while another one needs to be hidden. And I also checked on defining the MenuItem and findViewById, which didn't work because it's no view.
Let me show you a part of my code to make it more clear:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
(Somehow point to R.id.menu_refresh).setVisible(false);
}
return super.onOptionsItemSelected(menu);
}
Any ideas?

What I read from the docs, there is a special method for this case: onPrepareOptionsMenu(Menu menu).
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
So that makes it more like this:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
showRefresh = false;
//Stuff
}
return super.onOptionsItemSelected(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_refresh).setVisible(showRefresh);
return super.onPrepareOptionsMenu(menu);
}

If you're doing a 2.3 style menu, you don't change it in onOptionsItemSelected. You do it in onPrepareOptionsMenu the next time the menu is launched (by saving whatever data is appropriate to save).
If you have an actionbar, you'll need to call invalidateOptionsMenu on the activity to reload the menu, and do the disabling in the create function.

Related

update onOptionsItemSelected() without setting a new a listener

So I have a couple of settings fragments, and I want to do diffrent things when the 'save' icon is pressed, I can do this with toolbar.setOnMenuItemClickListener, but that would imply setting another listener for every other fragment, and that doesn't feel right, what's the right way to do this?
If I understand the question correctly, try the following:
In your fragments, override the method onOptionsItemSelected(). If you've correctly assigned the options menu save button an item id, you may write the following code to achieve what you want.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.save_button: // Be sure to replace this id with yours!
// <This is where you should put the code for the button's action>
return true;
}
return super.onOptionsItemSelected(item);
}

Android 4.2 setOnMenuItemClickListener is not called

I have an issue with the action bar on Android 4.2 with ActionBarSherlock. I have added an action bar menu item and it uses setOnMenuItemClickListener to set the buttons action.
MenuItem mapButton = menu.findItem(R.id.show_map);
mapButton.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//Button implementation goes here
}
});
This works fine on Android 2.2 up to Android 4.1 however on Android 4.2 onMenuItemClick does not get called at all.
I have tried adding this
//Handles the map and route button in the options bar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.show_map:
return false;
case R.id.show_route:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
Which does get called on button press but appears to have no effect. I could move the code the button implements into this function but it would require me to make drastic alterations to other parts of the program. Is there some way to get setOnMenuItemClickListener working on Android 4.2?

Blocking EditText contextMenu

I want to create an EditText with the following changes:
Clicking on it will not show the keyboard
After SINGLE, SHORT click it will get into selection mode (When I say selection mode I mean the mode where you can select a section of the text (with two pointers). You can get to this mode by long clicking on the text.)
After text is selected the copy/paste/cut toolbar will not be shown
For the first, I guess I can create an OnTouchListener and return true immediately, but then it will block me from doing the second thing (which I have no idea how to do).
I looked for a command that gets the EditText into selection mode, but all I could find was a way to get the selected text from it...
Thanks!
EDIT: I successfully made 1 and 2, but the toolbar still shows (tried unregisterForContextMenu)
you can use edittex.setCustomSelectionActionModeCallback
setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
this will be block to open contextmenu for edittex

Android option menu

In my project i have activity with options menu. I override onOptionsItemSelected method and add my handlers to menu items (switch-case block). But in one handler i need access to another menu item, how i can do that? findViewById doesn't work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.visit:
//how to access another MenuItem from here?
return true;
}
...
When creating your menu items, you could put the MenuItem you want to check in an attribute (i.e. one of the private fields of your class). This way, when you go into your method, you'll be able toacces the other menu item.

From Command to Android

I'm used to working in J2ME. I wanted to do something on Android, but I just can't get how to replace the command class in the events Android scenario.
Do you want to add commands and want them appear in a built-in menu? When user selects one of them you want a predefined function to be called? If I understand your question right and your problem is the same as above here is the solution:
Android provides a built-in menu and let you create your own menu options. Just like in J2ME as you create some menu options(commands in J2ME) they appear in menu automatically.(The menu that appears when the user hits the menu key on the handset)
To add menu to your Activity override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ITEM_ID, 0, "First Menu Option");
return true;
}
Then when user selects an item in the menu onMenuItemSelected(int featureId, MenuItem item) callback is invoked. So you can catch your menu option as follows:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case MENU_ITEM_ID:
yourFunction(); //your function for this particular item
return true;
}
return super.onMenuItemSelected(featureId, item);
}
If there were more items(options) in menu, you would catch them in switch by their ids.
hope that helps

Categories

Resources