I have a toolbar as well as a navigation drawer. When I start my app, the toolbar and navigation drawer are created. When I click items in the navigation drawer, it starts n
This is how to create menu dynamically: http://www.101apps.co.za/index.php/articles/using-toolbars-in-your-apps.html
Edited:
Toolbar actionBarToolBar = (Toolbar) findViewById(R.id.my_toobar);
setSupportActionBar(actionBarToolBar);
actionBarToolBar.setNavigationIcon(R.drawable.icon);
actionBarToolBar.setNavigationContextDescription(getResources().getString(R.string.desc);
actionBarToolBar.setLogo(R.drawable.other_icon);
actionBarToolBar.setLogoDescription(getResources().getString(R.string.other_desc);
actionBarToolBar.inflateMenu(R.menu.fragment_menu);
The best way to do it :
1. Find toolbar in Activity and set it as supportActionBar.
Toolbar actionBarToolBar = (Toolbar) findViewById(R.id.my_toobar);
setSupportActionBar(actionBarToolBar);
2. Then It will be a piece of cake to handle option menus for different fragments over a same Activity. Do following things in each fragment as you want:
In OnCreateView Method call
setHasOptionsMenu(true);
And Lastly,
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
And to manage menu items click, we have onOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search :
Log.i("item id ", item.getItemId() + "");
default:
return super.onOptionsItemSelected(item);
}
}
Below are the steps to show different menu options with different fragments.
Step1: Call setHasOptionsMenu(true)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setHasOptionsMenu(true)
super.onViewCreated(view, savedInstanceState)
}
Step2: Inflate your fragment related option menu items.
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
// To clear previously added menu items
menu?.clear()
inflater?.inflate(R.menu.your_fragment_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
The simplest option is to do
toolbar.inflateMenu(R.menu.fragment_menu);
and for handle click on menu item
toolbar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.nav_example -> doThat()
}
true
}
I had the same issue and I wanted to replace the toolbar menu accordingly to the fragment displayed.
The problem I'm now facing is that the menu is added to the previous one.
In order to show only the menu for the fragment, I suggest:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear(); //Empty the old menu
inflater.inflate(R.menu.menu_fragment1, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Hope it'll help.
Using new MaterialToolbar it's very easy to create an overflow menu and use to:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_menu"
app:menu="@menu/main_menu"/>
and then in code to set OnCLick Listeners just add this:
toolbar.setOnMenuItemClickListener {
// .. DO SOMETHING HERE
false
}