Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove item from menu programmatically?

Tags:

java

android

I've created bottom bar navigation with menu. I want to remove menu items by checking some conditions, so i want it remove using java code. I have tried it using onCreateOptionsMenu(), but somehow it did not work. I don't know what am i doing wrong, a little help would be appreciated.

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.member_activities);        
            BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomViewBar);
            BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
            menu = bottomNavigationView.getMenu();
            MenuItem menuItem = menu.getItem(0);
            menuItem.setChecked(true);
}
  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            this.menu = menu;
            menu.removeItem(R.id.district_tab);
            return true;
        }

bottom_navigation_menu.xml:

<item
    android:id="@+id/member_tab"
    android:icon="@drawable/ic_person_outline"
    android:title="Member"/>
<item
    android:id="@+id/club_tab"
    android:icon="@drawable/ic_person_outline"
    android:title="Club"/>

<item
    android:id="@+id/district_tab"
    android:icon="@drawable/ic_person_outline"
    android:title="District"/>

<item
    android:id="@+id/ag_tab"
    android:icon="@drawable/ic_person_outline"
    android:title="AG"/>

<item
    android:id="@+id/dg_tab"
    android:icon="@drawable/ic_person_outline"
    android:title="DG"/>

like image 220
Upendra Joshi Avatar asked Feb 05 '18 11:02

Upendra Joshi


1 Answers

onCreateOptionsMenu its for the menu in the toolbar.

For the menu in your BottomNavigationView you need to use bottomNavigationView.getMenu().removeItem(R.id.district_tab); in onCreate method.

like image 143
extmkv Avatar answered Sep 20 '22 02:09

extmkv