0

I'm trying to disable the MenuItem's in my navigationdrawer from my fragment, but it just wont work...

Fragment code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.waiting_for_terminal, container, false);

    setHasOptionsMenu(true);
    return rootView;
}

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

        menu.findItem(R.id.nav_amount).setEnabled(false);
        menu.findItem(R.id.nav_return).setEnabled(false);
        menu.findItem(R.id.nav_about).setEnabled(false);
        menu.findItem(R.id.nav_settings).setEnabled(false);

        super.onCreateOptionsMenu(menu, inflater);
    }

I can call getTitle() for the MenuItems, and it will return correct value. But for some reason setEnabled(), setTitle(), setVisible() etc. does not work, the value stays the same...

Christer
  • 2,746
  • 3
  • 31
  • 50

4 Answers4

4

to disable menuitem in fragment use it with fragment:

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

Remove onCreateOptionsMenu() inside the Activity,and use inside a fragment as:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
    }
Androider
  • 3,833
  • 2
  • 14
  • 24
  • check it http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean) – Androider Nov 30 '15 at 13:09
  • you have to remove it from activity, and i'll give you links related to you problem to get them work for you – Androider Nov 30 '15 at 13:31
  • check some problems which is being faced by developer: http://stackoverflow.com/questions/21498534/hide-menuitem-in-some-fragments , http://stackoverflow.com/questions/17825297/closeoptionsmenu-doesnt-work thanks – Androider Nov 30 '15 at 13:32
2
Try This:

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_act_add_section, menu);
    MenuItem item = menu.findItem(R.id.action_preview);
    item.setIcon(null);
    item.setTitle("");
    super.onCreateOptionsMenu(menu, inflater);
}
Suhas Bachewar
  • 1,230
  • 7
  • 21
0

I think you are trying to disable that Home Button on the AppBar which toggles the NavigationDrawer.

The best way i can think of doing that is:

In Method onOptionItemSelected:

protected onOptionItemSelected(MenuItem item)
{
   if(item.getItemId() == android.R.id.home)
    {
            // do anything you want here
    }
}

This will help you override that Home Button.

More over if you want to replace that Hamburger Icon with an Default Arrow Icon, you can use

mNavigationDrawer.setDrawerIndicatorEnabled(false);

Additionally if you want to disable the drawer Swipe Functionality also, you can use

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

Hope it Helps. :)

Sahil Sharma
  • 117
  • 1
  • 4
0

Omg, my bad...

It was MenuItems in the NavigationView I wanted to disable...

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().findItem(R.id.nav_amount).setEnabled(false);

Thanks for the help though :)

Christer
  • 2,746
  • 3
  • 31
  • 50