Q:

Android Toolbar Change Menu action items dynamically

I have a Toolbar (android.support.v7.widget.Toolbar) and one Action Menu Item on it. I want to programmatically populate submenu for the menu item.
I could not find a way to do it until I set the toolbar as ActionBar by setSupportActionBar(toolbar). In this case I can modify menu items in onPrepareOptionsMenu(). Is there a way to change menu items programmatically without setSupportActionBar(toolbar)?
The only action item is inflated by toolbar.inflateMenu(R.menu.menu_main)
menu_main.xml:
<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=".MainActivity">
<item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="1"
    app:showAsAction="always">
 </item>
</menu>

A:

You can get the menu of the Toolbar with the Toolbar's getMenu() method. This method returns a Menu object to which you can add or modify menu items, or submenus.
Here you can check out the documentation of the getMenu() method.

