I have 3 tabs with BottomNavigationView, each tab has:
tab 1: recyclerview list (home icon)
tab 2: recyclerview list (cart icon)
tab 3: Button which navigates to tab 1 (search icon)
all is good, when I navigate between BottomNavigationView (the icon changes good), BUT when I click the button, fragment changes good but the icon does not change (always is search icon and should be the home icon)
this is my code:
Button click:
Fragment fragment = new Home();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
BottomNavigationView structure:
bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.btn_home:
setFragment(Home);
setTitle("Home");
return true;
case R.id.btn_cart:
setFragment(Cart);
setTitle("Cart");
return true;
case R.id.btn_search:
setFragment(Search);
setTitle("Search");
return true;
default:
return false;
}
}
});
my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blanco">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="@+id/nav_menu">
<FrameLayout
android:id="@+id/nav_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/general"/>
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_menu"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="@style/BottomNavigationView"
app:menu="@menu/menu"
app:labelVisibilityMode="unlabeled"
android:background="@color/color_texto">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
Menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/btn_home"
android:icon="@drawable/tab_menu_home"
android:title="Home" />
<item
android:id="@+id/btn_cart"
android:icon="@drawable/tab_menu_cart"
android:title="Cart" />
<item
android:id="@+id/btn_search"
android:icon="@drawable/tab_menu_search"
android:title="Search" />
</menu>
How can I change the BottomNavigationView icon When I click button in tab 3?
thanks in advance