When a item is selected from the menu it should be highlighted to make sure it is selected.
That works until I implement the OnNavigationItemSelectedListener
When I do this the bar continues to work but the highlighting does not work anymore, the same element is always highlighted.
My class
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomBar = findViewById(R.id.navigation);
bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.action_home:
getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new HomeFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
break;
case R.id.action_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new ProfileFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
break;
case R.id.action_search:
getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new SearchFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
break;
}
return false;
}
});
}
}
XML
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
app:menu="@menu/my_navigation_items"
/>