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);
Related
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.
Hi this is my first question. i am learning android. here i am trying to setup menu icon top menubar.
I have added sets of item in menu. i want to manage icon from activity.
I am trying to show hide menu icon.
Menu return null in onCreate.
Is there any other way to manage menu icon dynamically ?
please help.
This is the activity class code snippet where i am trying to manage menu.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMenu = (Menu) findViewById(R.id.menuBar);//here Menu return null
mMenuItem = mMenu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
}
will appreciate your help. thanks.
First inflate menu to get MenuItem in activity method onCreateOptionsMenu and then try to get menu.getItemlike this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_activity, menu);
MenuItem item=menu.getItem(2);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setVisible(true);
return true;
}
Dont put it on your onCreate because your menu is initialized in onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenuItem = menu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
return true;
}
Just follow the bellow instruction:
Step 1:
You need to inflate menu item from onCreateOptionsMenu(Menu menu)
Step 2:
You need a MenuInflater object that you can get using getMenuInflater()
API. Like: MenuInflater inflater = getMenuInflater();
Step 3:
Inflate you menu xml file like: inflater.inflate(R.menu.menu_bottom_nav,
menu);
Step 4:
You have to get menu object for specific item like : MenuItem menuItem =
menu.getItem(index). Here Index is the number depending on which menu item's
object you want to get.
Full code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bottom_nav, menu);
MenuItem menuItem = menu.getItem(0);
return true;
}
I need to remove the inflated menu after I return to Fragment 1 from Fragment 2 after some action.
I clear the menu after I make the popBackStack, but nevertheless the menu item can't be deleted.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
if (savedItemsExist())
inflater.inflate(R.menu.menu_saved_filters, menu);
else
{
Log.i(TAG + " onCreateOptionsMenu", " going to delete");
menu.clear();
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
if (savedItemsExist())
getActivity().getMenuInflater().inflate(R.menu.menu_saved_filters, menu);
else{
menu.clear();
}
super.onPrepareOptionsMenu(menu);
}
If you have menu items tied to a Fragment's lifecycle, you should instead use setHasOptionsMenu(true) on your Fragment and then override onCreateOptionsMenu() in the Fragment, inflating your fragment's menu.
Maybe that is hack solution, but issue solved by replacing the Fragment 2 by Fragment 1 instead of popBackStack()
I'm tring to change a menuitem icon, but the icon is not being changed.
Here is how I find the MenuItem (works fine):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_location_actions, menu);
mActionLocation = menu.findItem(R.id.action_location);
return super.onCreateOptionsMenu(menu);
}
Method UpdateIcon: I took a screenshot so you can see (in the red frame) that the images have been found by the system.
The mActionLocation is a MenuItem which is initialized before this Method is called and is not null.
Anyone has an idea?
UPDATE (solution by help from #vinitius)
The UpdateIcon method:
private void UpdateIcon(boolean locationOn) {
mActionLocation = locationOn; // mActionLocation is a global boolean
invalidateOptionsMenu();
}
The onPrepareOptionsMenu override:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Toggle location icon
if (mActionLocation) {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_on_white_24dp);
} else {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_off_white_24dp);
}
return super.onPrepareOptionsMenu(menu);
}
You need to use onPrepareOptionsMenu(Menu menu):
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.
Inside this method, retrieve your item as you do in onCreateOtionsMenu and check whether your gps in os or not to modify your item icon.
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.