Android SearchView not removed on menu clear - android

I am using app compat activity. I have a fragment where I added a search view in the MenuItemCompat. Now when I am opening another Fragment within the same container I can still see the Search View when clicking on the action bar Menu item. I have added the following
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main, menu);
// MenuItemCompat.setActionView(menu.getItem(1), null);
//SearchView searchView =View.findViewById(R.id.searchViewId);
// MenuItemCompat.collapseActionView(menu.getItem(0));
// MenuItem item = menu.findItem(R.id.action_add_recipient);
//
// item.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
in the new fragment but still when clicking on the action bar item the search view is displayed. Any help please.

Related

Android: edit Menu Item when finished loading menu

I want know if I can change the menu item icon after the menu it's initialized, this is my code:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.right_menu, menu);
this.menu = menu;
updateMenuButton();
}
public void updateMenuButton() {
if (menu != null) {
if (verificato) {
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_clear_white_24dp);
} else {
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_done_white_24dp);
}
}
}
I call updateMenuButton at the end of onCreateOptionsMenu, but when try to access to this:
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_clear_white_24dp);
I get this error:
java.lang.IndexOutOfBoundsException: Invalid index 2131624115, size is 1
so I think that the item menu is not added yet? if i remove the updateMenuButton() call at the end of onCreateOptionsMenu I can see the Menu Item.
How can I do?
Thanks
That because "getItem(int index)" gets the menu item at the given index.
I think you have to write menu.getItem(0);

Remove Action Button From Action Bar

I have implemented ActionBar Navigation using Fragment. In my App i have one Activity and rest is in Fragments. In my MainActivity i am implementing menu like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Two Fragments uses Navigation Drawer and in their respected fragments i am inflating menu buttons to sort items.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sort_button_shops, menu);
}
Now the Problem is if i open the Fragment 1 it works perfectly. When i open fragment 2, it shows 2 button to sort, one from Fragment 1 and the second one from Fragment 2.
I have tried to hide the button but it didn't worked.
Any Help will be Appreciated.
Thanks
When you inflate a new menu you are adding new items to the old Menu object, which is probably not what you intended.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.removeItem(R.id.your_menu_item);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try using this in onResume() of fragments.
MenuItem item = (MenuItem) findViewById(R.menu.activity_main);
item.setVisible(false);
this.invalidateOptionsMenu();

Hide menu which is created in acitivity?

I have one activity with 3 fragments. I want to hide menu which is created in activity and show the fragment 2 alone.
I know how to hide menu which is created by other fragments. Is it possible to hide the Activity menu on fragments?
When you override onCreateOptionsMenu you can clear the menu from there.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

Set drop down menu of Action bar invisible

I am using Android native action bar in my app. I created the drop down menu of action bar by:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
return true;
}
For certain page, I would like to hide the action bar drop down menu. How to achieve this? Seems there is no direct method to set visibility to false for this drop down menu...
I managed to resolve this problem myself by set each menu item in drop down menu of action bar to be invisible:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
menu.getItem(2).setVisible(false);
menu.getItem(3).setVisible(false);
return true;
}
you should try to change the navigation mode of action bar when you create the activity...
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

Menu Ordering differences due to Fragment and Activity hierarchy. (onCreateOptionsMenu)

Background
Developing for Android 3.0, I have a HostActivity that is the superclass of NotebooksActivity and NoteActivity. NotebooksActivity includes a fragment, NotebooksFragment.
In HostActivity, I include a menu that I want to appear at the rightmost end of the options menu in the ActionBar, i.e. all menu items in subclasses of HostActivity should appear to the left of the menu items added in HostActivity.
Menu inflation in HostActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.host_menu, menu);
return super.onCreateOptionsMenu(menu);
}
The Problem
When I add menu items in NoteActivity, I achieve the desired order as expected:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.notebook_menu, menu);
return super.onCreateOptionsMenu(menu);
}
However, when I add menu items in NotebooksFragment, because of how Fragments work, onCreateOptionsMenu is called after the same method in HostActivity, resulting in HostActivity's menu items appearing before NotebooksFragment's.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.notebooks_menu, menu);
SearchView sv = new SearchView(getActivity());
sv.setOnQueryTextListener(this);
menu.findItem(R.id.search_notebooks).setActionView(sv);
super.onCreateOptionsMenu(menu, inflater);
}
How can I achieve my desired menu ordering?
You can try using android:menuCategory and android:orderInCategory to more manually specify your ordering.
Or, use onPrepareOptionsMenu() in HostActivity, as that is called after onCreateOptionsMenu(). Downside is that it is called on every MENU button press, not just the first time.

Categories

Resources