I want to hide an item in Action Bar Sherlock. I try it:
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 2131165381:
item.setVisible(false);
supportInvalidateOptionsMenu();
CopyOfAsyncLoadTasks.run(this);
item.setVisible(true);
break;
What I am doing wrong?
I suggest you to use rather resource id of <item> instead of your "ambicious" number.
switch (item.getItemId()) {
case R.id.myItem:
menu.findItem(R.id.myItem).setVisible(false);
break;
...
}
From first look at your code i don't know exactly what number 2131165381 is? If you'll work in team it'll be not very human-readable especially for another person.
Note: To get more control over menu i recommend you to create second menu variable e.q:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
...
}
Related
I have an Actionbar displayed on my maps fragment to which I have added a zoom-in button. When selected I would like to replace it with a zoom-out button.
When zoom-in is selected the it is obviously pases in to onOptionsItemSelected as the menu item, so it is easy to set it's attributes like this: viewZoomIn.setVisibility(viewZoomIn.GONE); My problem is, how do I get a reference to the zoom-out button on the action bar to set it as as I would like to viewZoomOut.setVisibility(viewZoomOut.VISIBLE);?
I thought I may be able to store the zoom-in and zoom-out views as instance variables and capture them when I inflate the Actionbar, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
viewZoomIn = findViewById(R.id.zoom_in);
viewZoomOut = findViewById(R.id.zoom_out);
return super.onCreateOptionsMenu(menu);
This does not work.
Any help on getting hold of thes buttons, or advice on a better way of toggling my zoom-in/zoom-out buttons would be appreciated.
It probably shows, but I am pretty new to Java, so it would be preferable is any assistance were communicated in a simple manner.
Thank you.
You have to apply your visibility logic in onPrepareOptionsMenu(Menu) not in onCreateOptionsMenu(Menu) if you want to toggle MenuItem state.
private boolean currentlyZoomedIn;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// called the first time and after each "invalidateOptionsMenu()"
// if tha Activity is in the "zoomedIn" state, the zoomOut button will be visible
menu.findItem(R.id.zoom_in).setVisible(!currentlyZoomedIn);
menu.findItem(R.id.zoom_out).setVisible(currentlyZoomedIn);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.zoom_in:
// do your logic when zoom_in is clicked
currentlyZoomedIn = true;
break;
case R.id.zoom_out:
// do your logic when zoom_out is clicked
currentlyZoomedIn = false;
break;
}
// force the redraw of the menu
invalidateOptionsMenu();
return super.onOptionsItemSelected(item);
}
By the way, I suggest you to use only one button and move your logic only in onOptionsItemSelected(MenuItem) to avoid an instance variable and the call to invalidateOptionsMenu() in a way like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// zoom_out_in_id is the id for the common button
if (item.getItemId() == R.id.zoom_out_in_id) {
// get the curren title
final CharSequence title = item.getTitle();
// if the current title is the one of zoom_in button, you have to change its infos to zoom_out ones
if (title.equals("zoom_in_title")) {
// do your logic when zoom_in is clicked
item.setIcon(R.drawable.zoom_out_icon);
item.setTitle("zoom_out_title");
} else if (title.equals("zoom_out_title")) {
// do your logic when zoom_out is clicked
item.setIcon(R.drawable.zoom_in_icon);
item.setTitle("zoom_in_title");
}
}
return super.onOptionsItemSelected(item);
}
This question already has answers here:
change MenuItem visibility when clicked
(2 answers)
Closed 7 years ago.
I have a search item, with 2 other items on the toolbar.
On clicking on the search item, I wish for the other 2 items to disappear and reappear when the search bar is closed.
Here is what i have tried thus far:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.actions_one_fragment, menu);
final MenuItem item = menu.findItem(R.id.action_search);
this.menu = menu;
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentActivity activity = getActivity();
switch (item.getItemId()) {
case R.id.action_search:
menu.findItem(R.id.action_item_one).setVisible(false);
menu.findItem(R.id.action_item_two).setVisible(false);
default:
return super.onOptionsItemSelected(item);
}
This doesn't do anything.
You should hide/remove menu item like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentActivity activity = getActivity();
switch (item.getItemId()) {
case R.id.action_search:
menu.removeItem(R.id.action_item_one);
menu.removeItem(R.id.action_item_two);
default:
return super.onOptionsItemSelected(item);
}
If you want to make changes to a Menu and its items before it's show, Android says you should use the onPrepareOptionsMenu callback. This is called by Android just before the menu is shown, so you can make changes to it (and its items) there that will immediately take effect.
In your sample, you are only setting the menu option to be invisible in the case where the search option is selected. That's probably not sufficient to do what you're asking.
I have created an action bar Search in the action bar menu.
Well, I am trying to make it go to its normal state if I click a go button or a click anywhere on the screen. Here is my code :
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
final EditText editText = (EditText) menu.findItem(
R.id.menuSearch).getActionView();
editText.addTextChangedListener(textWatcher);
menuItem = menu.findItem(R.id.menuSearch);
menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menuItem.collapseActionView();
// Do something when collapsed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//editText.clearFocus();
return true; // Return true to expand action view
}
});
return true;
}
It works well. But, It doesn't go to its normal state. I used this function
menuItem.collapseActionView();
But, no use. Thanks in advance
try this code
menuItem.collapseActionView();
inside
public boolean onOptionsItemSelected(MenuItem item) {
The framework calls onMenuItemCollapsed() to notify your code that the search view has been collapsed. A call to "menuItem.collapseActionView()" does no good in that method body. Your calls to collapseActionView() should be in the code where you want to force the view to be collapsed, such as a keystroke handler for the Go button.
I can get all the items from the actionbar with getItem() but I need to compare their id's with a particular id. How can I do this?
You can get actionBar menu items id by calling pMenu.getItem();
You can use changing methods of these menu items by overriding the method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_stats:
break;
}
}
on android phones there is a button at the bottom left. this usually opens up a menu. what is the code for this. Its okay to lead me to another post instead of posting a long answer here. sorry Im asking. I understand if there are other posts like this one. I just couldnt seem to find one. ill delete this one when done. Thanks.
Check out the developer guide on menu :) http://developer.android.com/guide/topics/ui/menus.html
To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide their own onCreateOptionsMenu() callback). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
To handle click events:
#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);
}
}