0

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

Dimoreno
  • 227
  • 3
  • 15
  • may want to include that as well, since that could be a source of a problem. I am deleting this answer since you have already confirmed that you have done this. Please update your original post. – JoxTraex Jul 26 '22 at 22:14

1 Answers1

-1

Finally I resolve this problem, steps:

  1. set as public static the BottomNavigationView layout in main activity:

    public static BottomNavigationView bnv;

  2. after click button set the state cheched like this:

    MainActivity.bnv.getMenu().getItem(0).setChecked(true);

for me that was ok;

Dimoreno
  • 227
  • 3
  • 15
  • Thats great that you were able to solve it, but a view should probably not be static. That sounds like a good way to cause a memory leak .. -1 as this is not a good solution. Perhaps instead of this you could get the content of the layout the view you inflated via setContentView() or LayoutInflater. Normally when you inflate the view you keep a non-static refence to it after you've inflated it. Or if you'are already attached it to your activity you can get it via https://stackoverflow.com/questions/4486034/get-root-view-from-current-activity . Hope that helps Had to repost for update. – JoxTraex Jul 27 '22 at 17:23
  • 1
    Thanks I will try to implement your response :D – Dimoreno Jul 27 '22 at 17:49