0

I have a "menu" resource "menu_test" with the following code:

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TestActivity" >

<item android:id="@+id/hideshow"
    android:title="Body visbility"
    android:orderInCategory="100"
    android:icon="@drawable/showicon"
    app:showAsAction="always" />

</menu>

In my activity.java I inflate the toolbar in the "onCreate" function and add a click listener:

private Toolbar toolbar;
...
toolbar = (Toolbar)findViewById(R.id.toolbarId);
toolbar.inflateMenu(R.menu.menu_test);
toolbar.setOnMenuItemClickListener(this);

I would like to get the ID of "hideshow" and change the icon:

testItem = (MenuItem) toolbar.findViewById(R.id.hideshow);
testItem.setIcon(R.drawable.hideicon);

However to app crashes when getting the ID in the first line. In the click listener function it works fine, since the ID is provided internally with the click:

public boolean onMenuItemClick(MenuItem item)
    item.setIcon(R.drawable.hideicon);

So how can I change the icon within the onCreate function? ("findItem" does not work, since toolbar is not a menu)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
void74
  • 3
  • 1
  • 3
  • Have you tried `toolbar.findItem()`? No need to cast `(MenuItem)` since it already returns as it. – klauskpm Oct 14 '16 at 21:14
  • Saw your edit. You could use this solution then: [How do I hide a menu item in the actionbar?](http://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar). You would need to call `invalidateOptionsMenu()` and then inside `onCreateOptionsMenu(Menu)` you find the item with `findItem` and set the icon. – klauskpm Oct 14 '16 at 21:21
  • Menu API is level 11, `onCreateOptionsMenu` should be there. As in [**Google Section about Menu**](https://developer.android.com/guide/topics/ui/menus.html?hl=pt-br) and this another exemple of [**Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu**](http://stackoverflow.com/questions/7066657/android-how-to-dynamically-change-menu-item-text-outside-of-onoptionsitemssele) are both using it. Is it giving you an error? – klauskpm Oct 14 '16 at 21:29
  • 1
    `toolbar.getMenu().findItem()` worked for me – Spikatrix Jul 09 '18 at 07:55

2 Answers2

1

I may be easier to add the menu items programmatically so you can maintain a reference to them:

public class MainActivity extends AppCompatActivity {

    private static final int MENU_SETTINGS = Menu.FIRST;
    MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menuItem = menu.add(0, MENU_SETTINGS, 0, R.string.action_settings).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_SETTINGS:
                // DO SOMETHING
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
Abtin Gramian
  • 1,630
  • 14
  • 13
0

As the other answers and comments indicate, one needs a reference from onCreateOptionsMenu or onPrepareOptionsMenu.

In my case, this would require a lot of code rewrite, as this is an old project, where everything is based on using toolbars instead of menus.

However, I found an easy solution: Instead of finding the ID and changing the icon, I created several instances of menu_test[1 .. n].xml with different icons and simply use inflateMenu(R.menu.menu_test[1 .. n]) depending on the icon I need.

If you write new code, use the solution from Abtin Gramian, so I mark that as the proper answer.

void74
  • 3
  • 1
  • 3