title: How to add submenu items to ActionBar action in code?
body: <p>Via xml I can add submenu items to my action in the <code>ActionBar</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/zzn5Y.png" alt="enter image description here"></p>
  
  <p>main_menu.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:id="@+id/menu_new_form"
            android:icon="@drawable/ic_new_form"
            android:title="@string/menu_new_form"
            android:showAsAction="ifRoom|withText"&gt;
          &lt;menu&gt;
              &lt;item android:id="@+id/form1"
                    android:icon="@drawable/attachment"
                    android:title="Form 1"
                    android:onClick="onSort" /&gt;
              &lt;item android:id="@+id/form2"
                    android:icon="@drawable/attachment"
                    android:title="Form 2"
                    android:onClick="onSort" /&gt;
          &lt;/menu&gt;
      &lt;/item&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p><strong>But how can I add these sub items via Java code</strong>? It doesn't work as below, the sub items are getting added to the wrong action (and also the drawable isn't shown), the very right button, not my 'New Form' button:</p>
  
  <p><img src="https://i.stack.imgur.com/JEL0w.png" alt="enter image description here"></p>
  
  <p>main_menu.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:id="@+id/menu_new_form"
            android:icon="@drawable/ic_new_form"
            android:title="@string/menu_new_form"
            android:showAsAction="ifRoom|withText"&gt;
      &lt;/item&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>Java Code:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
  
      Log.d("MainMenu", ",menu title0: " + menu.getItem(0).getTitle()); 
      // returns "New Form"
  
      menu.addSubMenu(0, Menu.NONE, 1, "Form 1").setIcon(R.drawable.attachment);
      menu.addSubMenu(0, Menu.NONE, 2, "Form 2").setIcon(R.drawable.attachment);
      return true;
  }
  </code></pre>
  
  <p>Is there a way to achieve this, adding sub menu items via Java Code instead of XML, <strong>without</strong> using a <code>PopupMenu</code> (<a href="http://developer.android.com/guide/topics/ui/menus.html#PopupMenu" rel="noreferrer">http://developer.android.com/guide/topics/ui/menus.html#PopupMenu</a>)?</p>
  
  <p><strong>Update (Solution):</strong></p>
  
  <p>My final code snippet I ended up with to populate the submenu dynamically, following adamp's reply:</p>
  
  <pre><code>// menu options
  private static final int MENU_PREFERENCES = Menu.FIRST;
  private static final int MENU_LOGOUT = 2;
  
  @Override
  public boolean onCreateOptionsMenu(final Menu menu) {
      super.onCreateOptionsMenu(menu);
  
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
      menu.add(0, MENU_PREFERENCES, 0, getString(R.string.general_preferences)).setIcon(
              android.R.drawable.ic_menu_preferences);
  
      // load all available form templates
      Cursor c = managedQuery(FormsProviderAPI.FormsColumns.CONTENT_URI, null, null, null, null);
      try {
          int ixDisplayName = c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME);
          int ixId = c.getColumnIndex(FormsProviderAPI.FormsColumns._ID);
          int cnt = 0;
          while (c.moveToNext()) {
              cnt++;
              Log.d("ID: ", "ID: "+ c.getInt(ixId));  // misusing the group id for the form id
              menu.getItem(1).getSubMenu().addSubMenu(c.getInt(ixId), Menu.NONE, cnt, c.getString(ixDisplayName)).setIcon(R.drawable.attachment_dark);
          }
      } catch (Exception e) {
          Log.e(TAG, "Error init form templates list.", e);
      }
  
      return true;
  }
  </code></pre>