Android Toolbar Adding Menu Items for Different Fragments

How can I change menu items in top bar in different fragments?

instead of toolbar.setOnMenuItemClickListener you need to use this :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}

For more info please have a look at this answer.

A quick overview you need to inflate different menu resource everytime you need to change your menu. Also you need to set OnOptionsItemSelected for the custom actions you need to perform.

You might also need to add this for fragment setHasOptionsMenu(true); in your onCreate method.

How to add Options Menu to Fragment in Android

Call the super method:

Java:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}

Kotlin:

    override fun void onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater)
}

Put log statements in the code to see if the method is not being called or if the menu is not being amended by your code.

Also ensure you are calling setHasOptionsMenu(boolean) in onCreate(Bundle) to notify the fragment that it should participate in options menu handling.

Add Toolbar inside fragment (AndroidX)

so what was the successfull answer for me:

Inside onCreate method just add setHasOptionsMenu(true);

    @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

Inside my WallFragment.java in onCreateView I added setOnMenuItemClickListener just right after initializing the Toolbar

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflatedView = inflater.inflate(R.layout.fragment_wall, container, false);

TopActivityToolbar = inflatedView.findViewById(R.id.update_wall_page_toolbar);
TopActivityToolbar.setTitle("someTitle");
TopActivityToolbar.inflateMenu(R.menu.menu_app_actionbar_wall);

// and finally set click listener
TopActivityToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Your code inside Toolbar onClick...
return false;
}
});
return inflatedView;
}

Do not forget to to INFLATE the menu from res/menu/...

Cannot inflate option menu with toolbar in fragment

I fixed it by changed android:theme="@style/Theme.Mig33.NoActionBar of whole app to theme that support action bar android:theme="@style/Theme.Mig33"and keep the activity with no action bar

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mml.mig33">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Mig33">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Mig33.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

the called this in fragment

(activity as AppCompatActivity?)!!.setSupportActionBar(binding.toolbar)

Show different menu options on Toolbar for activity and fragment

I'm not sure if you can do this with onCreateOptionsMenu(). I think your better bet will be onPrepareOptionsMenu().

You can force Android to refresh the options menu by simply writing getActivity().invalidateOptionsMenu() in Fragment's onResume().

So your onPrepareOptionsMenu() will look like:

@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear(); //remove all items
getActivity().getMenuInflater().inflate(R.menu.menu_fragment, menu);
}

How can I have different menus in a Toolbar for different fragments?

You should call setSupportActionBar(toolbar) when you setup your Toolbar (probably in onCreate) and inflate your menu as usual in onCreateOptionsMenu(Menu menu) method.
In this method you can handle the different Menus, whether it's by hiding the items or just inflating a different menu based on the fragment.

Then, as you said, call supportInvalidateOptionsMenu(); when you change between fragments to recreate the Menu.



Related Topics



Leave a reply



Submit