Is it possible to change the option menu items programmatically? Can anyone provide me with an example please?
Also, I want to disable certain items, so that they do
using the following lines i have done to add the values in menu
getActivity().invalidateOptionsMenu();
try this work like a charm to me.
To disable certain items:
MenuItem item = menu.findItem(R.id.ID_ASSING_TO_THE_ITEM_IN_MENU_XML);
item.setEnabled(false);
For anyone needs to change the options of the menu dynamically:
private Menu menu;
// ...
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.options, menu);
return true;
}
// ...
private void hideOption(int id)
{
MenuItem item = menu.findItem(id);
item.setVisible(false);
}
private void showOption(int id)
{
MenuItem item = menu.findItem(id);
item.setVisible(true);
}
private void setOptionTitle(int id, String title)
{
MenuItem item = menu.findItem(id);
item.setTitle(title);
}
private void setOptionIcon(int id, int iconRes)
{
MenuItem item = menu.findItem(id);
item.setIcon(iconRes);
}
you can accomplish your task simply by implementing as below:
private Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drive_menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
this.menu = menu;
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_toggle_grid) {
handleMenuOption(id);
return true;
} else if(id == R.id.action_toggle_list){
handleMenuOption(id);
return true;
}
return super.onOptionsItemSelected(item);
}
private void handleMenuOption(int id) {
MenuItem item = menu.findItem(id);
if (id == R.id.action_toggle_grid){
item.setVisible(false);
menu.findItem(R.id.action_toggle_list).setVisible(true);
}else if (id == R.id.action_toggle_list){
item.setVisible(false);
menu.findItem(R.id.action_toggle_grid).setVisible(true);
}
}
Try this code:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
this.menu=menu;
updateMenuItems(menu);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.document_list_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
if (item.getItemId() == android.R.id.home) {
onHomeButtonPresssed();
}else if (item.getItemId() == R.id.action_delete) {
useCheckBoxAdapter=false;
deleteDocuments();
} else if (item.getItemId() == R.id.share) {
useCheckBoxAdapter=false;
shareDocuments();
} else if (item.getItemId() == R.id.action_tick) {
useCheckBoxAdapter=true;
onShowCheckboxes();
}
updateMenuItems(menu);
return true;
}
private void updateMenuItems(Menu menu){
if (useCheckBoxAdapter && menu != null) {
menu.findItem(R.id.action_delete).setVisible(true);
menu.findItem(R.id.share).setVisible(true);
menu.findItem(R.id.action_tick).setVisible(false);
} else {
menu.findItem(R.id.action_delete).setVisible(false);
menu.findItem(R.id.share).setVisible(false);
menu.findItem(R.id.action_tick).setVisible(true);
}
invalidateOptionsMenu();
}
If I have to change the contents of my options menu I perform it during the onMenuOpened(). This allows me to check the running state at the very moment that the user is accessing the menu.
public boolean onMenuOpened(int featureid, Menu menu)
{
menu.clear();
if (!editable)
{
MenuItem itemAdd = menu.add(0, REASSIGN, Menu.NONE, context.getString(R.string.reassign));
MenuItem itemMod = menu.add(1, EDIT, Menu.NONE, context.getString(R.string.modify));
MenuItem itemDel = menu.add(2, DELETE, Menu.NONE, context.getString(R.string.delete));
itemAdd.setShortcut('0', 'a');
itemMod.setShortcut('1', 'm');
itemDel.setShortcut('2', 'd');
}
else
{
MenuItem itemSave = menu.add(3, SAVE, Menu.NONE, context.getString(R.string.savechanges));
itemSave.setShortcut('0', 'S');
}
return true;
}