I have a BottomNavigationView
.
It works and shows the text title under the item that the user clicked.
However, adding a OnNavigationItemSelectedListener
stops the tapped item to show it's title underneath it's icon. It still gets the ripple effect and it runs any code attached to that item listener. Adding a Log.d() and printing a message to the console on item click shows that the listener runs. But for some reason the text title for that item won't show up under. So you don't know what button you last pressed.
I've tried moving it from onStart() to onCreate() but the same problem occurs.
Commenting it out fixes the problem, but I want to trigger events on the item clicks.
Listener
bnvMain.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
// open fragment 1
break;
case R.id.navigation_dashboard:
// open fragment 2
break;
case R.id.navigation_notifications:
// open fragment 3
break;
case R.id.navigation_profile:
// open fragment 4
Log.d("myTag", "This seems to be running...");
break;
}
return false;
}
});
XML
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnvMain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@color/colorPrimary"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorNavText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />