I have a sidebar. It is done as follows:

MainActivity.java

// Methods, variables, etc... Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.addDrawerListener(new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){ @Override public void onDrawerSlide(View drawerView, float slideOffset) { mainActivityPresenter.loadMenuItems(MainActivity.this, MainActivity.this.menu); } }); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); navigationView.setOnCreateContextMenuListener(this); //etc... 

The class also has a listener

 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ this.menu = menu; } 

MainActivityPresenter.java

 public void loadMenuItems(MainActivity mainActivity, ContextMenu menu){ if(Что-то там){doSmth();} } 

Well, the markup file itself:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> 

I need to change the menu after certain actions in the application. I decided to implement this by overriding the onDrawerSlide method, which, as I understand it, is called when the sidebar menu moves. This method calls another method of the class MainActivityPresenter, which should be a menu. How to do it right? I had a few ideas, but they all turned out to be unsuccessful. (Correctly, I understand that onCreateContextMenu is called when creating a menu in this case?

    2 answers 2

    The menu (including the contextual one) can be changed dynamically at the time of its creation, that is, your onCreateContextMenu() handler, for example:

     @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } 

    Where R.menu.context_menu resource where your menu is located.

    You can do the same thing programmatically, for example, remove menu.removeItem() or add menu.addItem()

    If we are talking about the Options Menu, then there is a little more complicated, it is necessary to modify the onPrepareOptionsMenu() - otherwise everything is the same ...

    • Thank. The problem has already been solved, but I think you can use your option too) - timuruktus

    MainActivity.java

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.addDrawerListener(new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){ @Override public void onDrawerOpened(View drawerView) { mainActivityPresenter.loadMenuItems(MainActivity.this.navigationView.getMenu()); } }); 

    MainActivityPresenter.java

     public void loadMenuItems(Menu menu){ if(FirebaseAuth.getInstance().getCurrentUser() != null){ menu.removeItem(R.id.registration_menu); } } 

    It seems to be working!

    • @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ this.menu = menu; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ this.menu = menu; } I generally removed - timuruktus