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.
Related
my application provides a list of recipes
at first all recipes are meat based and on the click of a switch in the setting page they become vegetable based
i want my app to change the titles and icons of the menu from meat based to vegetable based
i have put this code in the onPrepareOptionsMenu and it is being called but the titles are not being updated
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_drawer, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT > 11) {
invalidateOptionsMenu();
MenuItem item1 = menu.findItem(R.id.nav_all);
MenuItem item2 = menu.findItem(R.id.nav_beef);
MenuItem item4 = menu.findItem(R.id.nav_chicken);
Log.d("ISVEG", MeApplication.getIsVeg().toString());
if (MeApplication.getIsVeg()) {
item1.setTitle("Omlets");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.eggs));
item2.setTitle("Broccoli");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.broccoli));
item4.setTitle("Tomato");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.tomato));
} else {
item1.setTitle("All meat");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.allmeat));
item2.setTitle("Beef");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.steak));
item4.setTitle("Chicken");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.thanksgiving));
}
}
return super.onPrepareOptionsMenu(menu);
}
Remove invalidateOptionsMenu(); from onPrepareOptionsMenu. Its not required since onPrepareOptionsMenu will prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown
I have an activity in an android app with fragments displayed using a pager.
One of the three fragments shows a menu item (always displayed as action) and an overflow menu, the two others fragments show in the app bar only the first menu item (but not the overflow menu).
My problem is that when I switch from one tab to another, the menu is not updating smoothly.
Is it possible to add animations to menu inflation ?
Has anyone ever encountered such an issue and how did you handle it ?
Here is the app bar :
And here is how I inflate the menu inside the fragment, of course, the other fragment is inflating another XML file.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.home_menu, menu);
}
Please feel free to ask for more details ;-)
Thanks for helping !
In you fragment please write this line in your oncreateview mehod:
setHasOptionsMenu(true);
and implement this mehod:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_referesh, menu);
super.onCreateOptionsMenu(menu,inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
pageNumber=1;
getWebServiceData1();
return false;
}
return super.onOptionsItemSelected(item);
}
Ive inflated a menu in onCreateOptionsMenu and handled functionality in onOptionsItemSelected but I was just wondering, what passes the inflated menu to onOptionsItemSelected. I'm thinking onCreateOptionsMenu but I'm not sure.
The first time a menu is needed by Android for a particular instance of an Activity, it will call onCreateOptionsMenu(). The Menu and all of its MenuItem objects that you create and return there will be remembered for as long as that activity is active, and reused for subsequent calls to onPrepareOptionsMenu() and onOptionsItemSelected(). All of these objects will go away when the activity instance is destroyed, and new ones will be created for new instances of the Activity.
try to do in onCreateOptionsMenu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_home, menu);
}
and in onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
return true;
default:
return false;
}
Using item as menu
I have a ListActivity (SherlockListActivity) and its content can be dynamically changed by the user. When the content changes, the options menu should be replaced.
As I learned, instead of onCreateOptionsMenu I should use onPrepareOptionsMenu that is (supposed to be) called every time the user selects the menu.
This is called right before the menu is shown, every time it is shown.
I have the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.clear();
if (UserOption == UserOption1)
getSupportMenuInflater().inflate(R.menu.menu_option1, menu);
else
getSupportMenuInflater().inflate(R.menu.menu_option2, menu);
return super.onPrepareOptionsMenu(menu);
}
It is called only once during debug, so I always have the same menu.
What should I set to make it work?
Create and prepare the options menu for changing and its item selection method
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if(menuString=="red"){
inflater.inflate(R.menu.red_menu, menu);
}else if(menuString=="green"){
inflater.inflate(R.menu.green_menu, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Like whenever you want to change menu call
String menuString="red or green";
invalidateOptionsMenu();
Like others told, if you want to have static menu use onCreateOptionsMenu, and if you want to change its visibility dynamically use onPrepareOptionsMenu along with onCreateOptionsMenu
I found an example how to use a context menu with actionBar. This example waits for clicks on the phones menu button. But I want to have it appended to the icon or better the activity name. thanks
public class menu extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_fragen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
..
After the logo and title comes an drop down menu indicator.
That has nothing to do with onCreateOptionsMenu() or onOptionsItemSelected(). To set up that Spinner:
Get your ActionBar via getActionBar()
Call setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) on the ActionBar
Call setListNavigationCallbacks() on the ActionBar with your SpinnerAdapter and a listener object to be notified when the user makes a selection change in the Spinner