Get the ID of a MenuItem in a toolbar
<p>I have a "menu" resource "menu_test" with the following code:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;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" &gt;

&lt;item android:id="@+id/hideshow"
    android:title="Body visbility"
    android:orderInCategory="100"
    android:icon="@drawable/showicon"
    app:showAsAction="always" /&gt;

&lt;/menu&gt;
</code></pre>

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

<pre><code>private Toolbar toolbar;
...
toolbar = (Toolbar)findViewById(R.id.toolbarId);
toolbar.inflateMenu(R.menu.menu_test);
toolbar.setOnMenuItemClickListener(this);
</code></pre>

<p>I would like to get the ID of "hideshow" and change the icon:</p>

<pre><code>testItem = (MenuItem) toolbar.findViewById(R.id.hideshow);
testItem.setIcon(R.drawable.hideicon);
</code></pre>

<p>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:</p>

<pre><code>public boolean onMenuItemClick(MenuItem item)
    item.setIcon(R.drawable.hideicon);
</code></pre>

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