How to Add Options Menu to Fragment in Android

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.

How to create option menu in fragment

in order to have an options menu you need to tell your fragment about it in onCreate after which you override onCreateOptionsMenu.
then to handle any clicks on the items override onOptionsItemSelected()
your full Activity should look like this:

class TestFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_test, menu);

super.onCreateOptionsMenu(menu, inflater)
}

You can also try to use the actionBar widget provided by android incase the appcompat one doesn't work for you

How to add a menu in a fragment?

To add a menu for each fragment, you should go through many steps:

1) First of all, add setHasOptionsMenu(true) in the fragment's onCreateView() like below:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
....
}

2) Override fragment's onCreateOptionsMenu() method as below:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_above_details_fragment, menu);
return;
}

3) Override the activity's onOptionsItemSelected() method like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
Intent i;
switch (item.getItemId()) {
case R.id.action_param:
i = new Intent(this, Settings.class);
startActivity(i);
return true;

case R.id.action_history:
i = new Intent(this, HistoryMenu.class);
startActivity(i);
return true;
}
return onOptionsItemSelected(item);
}

4) Don't override the fragment's onOptionsItemSelected(), nor activity's onCreateOptionsMenu().

How to inflate menu from fragment overlaying an activity

You may have to override the onCreateOptionsMenu in your fragmewnt. Also you may have to setHasOptionsMenu to true in your fragments oncreate to let the activity know that this fragment has its own menu:

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Your menu needs to be added here
super.onCreateOptionsMenu(menu, inflater);
}

Until you do this, your menu will not be inflated and your activity can never hide another fragments menu.. So give it a try!!

How do i attach fragments using on options item selected using menu items

You cannot pass Fragment as app:actionViewClass in menu items. What you can really do is attach the fragment when the menu item is selected inside onOptionItemSelected.

  1. Remove the app:actionViewClass from menu item

    <item android:id="@+id/action_account"
    android:title="Account"
    android:icon="@drawable/ic_profile"
    app:showAsAction="collapseActionView|ifRoom"/>
  2. Add a static method inside AccountManagementFragment to get the instance

    public class AccountManagementFragment extends Fragment {
    ...
    ...

    static AccountManagementFragment newInstance() {
    return AccountManagementFragment();
    }
    ...
    ...
    }
  3. Change the implementation of onOptionsItemSelected to add the fragment

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    if (item.getItemId() == R.id.action_account) {
    // Make sure to add a fragment container view in the layout (preferably a FrameLayout)
    // Here I am assuming the id is 'container'
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.container, AccountManagementFragment.newInstance())
    .commit()
    }

    return super.onOptionsItemSelected(item);
    }

How to properly add options menu on a single fragment with navigation component without broke up behavior

If you're using setSupportActionBar, you must use setupActionBarWithNavController(), not toolbar.setupWithNavController as per the documentation.

Changing option menu item in android fragment programmatically Kotlin

You need to use findItem if you're using the id of item. Because when you do menu[0] it expects index & not id.

So in your case it should be something like this:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
super.onOptionsItemSelected(item)
when(item.itemId) {
R.id.recycler_view_toggle -> {
binding.mfToolbar.menu.findItem(R.id.recycler_view_toggle).isVisible = false
binding.mfToolbar.menu.findItem(R.id.recycler_view_toggle_grid).isVisible = true
}
}
return true
}


Related Topics



Leave a reply



Submit