Search code examples
androidbottomnavigationview

Android: Changing selected menu item


I am trying to change the selected menu item of a BottomNavigationView from a seperate fragment from the one that I initialize the BottomNavigationView in. I am using

MenuItem item = navigationView.getMenu().findItem(R.id.nav_home);
item.setChecked(true);

The problem is that the navigationView is null because I am in a seperate fragment.

I am using navigationView = view.findViewById(R.id.navigation); to get the id of the navigationView.


Solution

  • here when you use view.findViewById(R.id.navigation);, the view is representing the view of that separate fragment, which doesn't have such a view , hence it will return null.