title: Remove Toolbar items on runtime
body: <p>I have a <strong>Toolbar</strong> within my app which I would like to <strong>modify it contents dynamically</strong>, during the app execution (in other words, on run-time).</p>
  
  <p>For example, the app is capable of taking and previewing photos; once that photos are previewed, the user is able to select some photos and perform a sending action to a server. I want also to make the user able to delete photos once that some of them are selected and for doing that I would like that the "delete" item on the action bar (identifiable via the trash icon) will be visible only when one or more photos are selected.</p>
  
  <p><strong>Is this possible to do?
  If yes, how?</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ik0qY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ik0qY.png" alt="Toolbar"></a></p>
  
  <p>In other words and, more generically, I want to control items (visibility) in the toolbar, setting the code as they will be "visible" only when some conditions are "true" (in the example above, when photos are selected or, in a different example, when user is logged) and invisible when they are "false" (when user isn't logged).</p>
  
  <p>For now I just need to "remove" (or make invisible) items in the Toolbar, but it will be useful also to know if <strong>is possible to add items in the toolbar on run-time</strong>.</p>
  
  <p>I'm adding some code which can help to understand the problem.</p>
  
  <p><strong>app_bar.xml</strong> file in "/res/layout", which graphically defines the Toolbar</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:elevation="4dp"
      android:theme="@style/ToolbarTheme" /&gt;
  </code></pre>
  
  <p><strong>menu_resources.xml</strong> file, which defines the Toolbar items</p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  
  &lt;!-- "User favourite function", should appear as action button if possible --&gt;
  &lt;item
      android:id="@+id/action_app_icon"
      android:icon="@mipmap/ic_launcher"
      android:title="@string/action_bar_app_icon"
      app:showAsAction="always" /&gt;
  
  &lt;!-- Settings, should always be in the overflow --&gt;
  &lt;item
      android:id="@+id/action_delete"
      android:icon="@drawable/trash"
      android:title="@string/action_bar_delete"
      app:showAsAction="always"/&gt;
  
  &lt;!-- Settings, should always be in the overflow --&gt;
  &lt;item
      android:id="@+id/action_settings"
      android:icon="@drawable/settings"
      android:title="@string/action_bar_settings"
      app:showAsAction="never"/&gt;
  
  &lt;!-- About, should always be in the overflow --&gt;
  &lt;item
      android:id="@+id/about"
      android:icon="@android:drawable/ic_dialog_info"
      app:showAsAction="never"
      android:title="@string/action_bar_about"/&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Part of the activity in which the toolbar is</strong></p>
  
  <pre><code>public class myClass extends AppCompatActivity implements View.OnClickListener {
  
  // Instantiating a toolbar
  private Toolbar toolbar;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_class);
  
      // Adding toolbar to the activity
      toolbar = (Toolbar) findViewById(R.id.my_toolbar);
      setSupportActionBar(toolbar);
  
      // Get a support ActionBar corresponding to this toolbar
      ActionBar ab = getSupportActionBar();
      // Enable the Up button
      ab.setDisplayHomeAsUpEnabled(true);
      }
  
      // The method that creates an options menu
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_resource, menu);
          // This make the delete item invisible
          menu.findItem(R.id.action_delete).setVisible(false);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.action_settings:
                  // Perform the settings action
                  return true;
              case R.id.about:
                  // Perform the about
                  return true;
              case R.id.action_delete:
                  deletePhotos();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
  
      public static void manageSelection(Boolean state, int position){
          if (photoArray.getNumberSelected() == 0) {
              // disable the trash icon and its functionality;
          } else {
              // enable the trash icon with its functionality;
          }
      }
  
      // This method allows to deleteItems images to the array
      public void deletePhotos() {
          //code for deleting photos
      }
  }
  </code></pre>
  
  <p>Thanks for your time.</p>