-1

Option Menu:

    <item  
        android:id="@+id/home"
        android:icon="@drawable/ic_home"
        android:title="Home"/>

    <item  
        android:id="@+id/companies"
        android:icon="@drawable/ic_companies"
        android:title="Companies"/>

    <item  
        android:id="@+id/contact"
        android:icon="@drawable/ic_contacts"
        android:title="Contact"/>

I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked.

The last thing I tried was this code in my Fragment onCreateView method:

Menu menu = bottomNavigationView.getMenu();
menu.findItem(R.id.ic_home).setIcon(R.drawable.ic_home_fill);

but its not work for me.

what i tried in selectFragment(MenuItem item)

switch (item.getItemId()) {
                case R.id.home:
                 item.setIcon(R.id.ic_home_fill);
                break;
            }

I want to change the icon of the bottom navigation of selected position. if user clicked one item then the icon change to another one and when i select another one then 1st icon can set as a default.

refer this link:Android: Bottom Navigation View - change icon of selected item but it's not work for me

plz give me a other solution.

Thank you

Ali
  • 3,346
  • 4
  • 21
  • 56

2 Answers2

1

Try this, It worked for me

BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    invalidateOptionsMenu();
                    switch (item.getItemId()) {
                        case R.id.click_to_use:
                           addHomeFragment(MainActivity.this);
                            mTitle.setText("Home");
                            item.setIcon(R.drawable.device);
                            break;
                        case R.id.history:
                           addNotifyFragment(MainActivity.this);
                            mTitle.setText("History");
                            item.setIcon(R.drawable.ios_icon);

                            break;
                        case R.id.settings:
                          addSettingFragment(MainActivity.this);
                            mTitle.setText("Settings");
                            break;
                    }

                    return true;

                }
            });
  • it's not work for me error show : `Unable to start activity ComponentInfo{com.example.crm/com.example.crm.dashBoard}: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference` – Ali Feb 13 '18 at 11:45
  • If you are using still this code Menu menu = bottomNavigationView.getMenu(); menu.findItem(R.id.ic_home).setIcon(R.drawable.ic_home_fill); then in this you are using wrong ID .findItem(R.id.ic_home) you have to pass view id .findItem(R.id.home) –  Feb 17 '18 at 05:37
  • I have also edited my code you can also use mbottomnavigationview directly insteadof using menu –  Feb 17 '18 at 05:45
0

after any modification in the menu, you would have to make an "Invalidateoptionsmenu ()" in the action. I invite you to want to test this feature . see here