52

Currently I am working on an app which has a bottom navbar with three menu items. I had used setOnNavigationItemSelectedListener() for items being clicked. but now iam facing issue that the method has been depreciated.

  • App Language: Java
  • Issue: 'setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)' is deprecated

Is there any way to resolve it? is is there any better alternative than setOnNavigationItemSelectedListener() method.

yivi
  • 42,438
  • 18
  • 116
  • 138
Jeyasurya U R
  • 666
  • 1
  • 4
  • 8
  • its not deprecated https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView#setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener) – Dinkar Kumar Jun 17 '21 at 15:04

8 Answers8

72

Its deprecated according to github sources: BottomNavigationView.setOnNavigationItemSelectedListener

In its comment you can read:

@deprecated Use {@link NavigationBarView#setOnItemSelectedListener(OnItemSelectedListener)}
   *     instead.

so use NavigationBarView.setOnItemSelectedListener from its base class:

  /**
   * Set a listener that will be notified when a navigation item is selected. This listener will
   * also be notified when the currently selected item is reselected, unless an {@link
   * OnItemReselectedListener} has also been set.
   *
   * @param listener The listener to notify
   * @see #setOnItemReselectedListener(OnItemReselectedListener)
   */
  public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
    selectedListener = listener;
  }

Also see this commit

as it explains confusion about this change:

The listeners were deprecated in favor of NavigationBarView#OnItemSelectedListener and NavigationBarView#OnItemReselectedListener, but deprecation documentation was never added, so it's unclear what developers should use instead.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I tried it. it works fine, Thanks. but the speed of switching between nav items gets a bit slower than `OnNavigationItemSelectedListener`. any suggestions to improve it? Anyways thanks for the help – Jeyasurya U R Jun 18 '21 at 03:43
  • 1
    maybe add empty listener to setOnItemReselectedListener, this way your listener in setOnItemSelectedListener will not get called when item is re-selected – marcinj Jun 18 '21 at 08:13
49

you can try setonItemSelectedListener. It is working same as setOnNavigationItemSelectedListener()[tested in android 11]

bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch(id){
              //check id
            }
            return true;
        }
    });

Kotlin:

bnv.setOnItemSelectedListener { item ->
            when (item.itemId) {
            }
            true
        }
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
Abu Saeed
  • 1,020
  • 8
  • 21
5

We can use setOnItemSelectedListener instead of setOnNavigationItemSelectedListener and setOnItemReselectedListener instead of setOnNavigationItemReselectedListener

navView.setOnItemSelectedListener {
    // do something
    true
}

// In case the default menu can be the first menu 
// Should set the default selected menu BETWEEN setOnItemSelectedListener and setOnItemReselectedListener. 
// It will make setOnItemSelectedListener fired when you launch app. 

// If you set default menu AFTER setOnItemReselectedListener. 
// Then setOnItemReselectedListener will fired when you launch app
navView.selectedItemId = R.id.navigation_home


navView.setOnItemReselectedListener {
    // do something
}
Linh
  • 57,942
  • 23
  • 262
  • 279
5

you can use in Kotlin

buttmNav.setOnItemSelectedListener { item ->
        when (item.itemId) {
        }
        true
    }
devio
  • 607
  • 9
  • 29
2
public class HomeActivity extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener {

    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        bottomNavigationView = findViewById(R.id.bottom_nav);
        bottomNavigationView.setOnItemSelectedListener(this);
        displayfragment(new FragmentHome());

    }

    private void displayfragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(R.id.content_area, fragment).commit();


    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        switch (item.getItemId()) {

            case R.id.nav_home:
                fragment = new FragmentHome();
                break;
            case R.id.nav_fav:
                fragment = new FavouriteFragment();
                break;
            case R.id.nav_set:
                fragment = new FragmentSetting();
                break;

            default:
                fragment = new FragmentHome();
                break;

        }
        displayfragment(fragment);
        return true;
    }
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Moeenuddin178
  • 39
  • 1
  • 9
1

kotlin: use setOnItemSelectedListener

    bottomNavigationView.setOnItemSelectedListener { item: MenuItem ->
            when (item.itemId) {
                 R.id. ... -> {
                    Add your code
                 true
            }
            
            else ->
                true
        }
razi
  • 77
  • 8
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '22 at 08:30
0

 viewBindingMainActivity.navView.setOnItemSelectedListener { menuItem ->
            if (menuItem.itemId != R.id.navigation_home) {
                Add your code
                false
            } else {
                Add your code
                true
            }
        }
Royal
  • 1
0

Add below line in your code @SuppressWarnings("deprecation")

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 11 '23 at 17:36