close

[Solved] setOnNavigationItemSelectedListener is deprecated

Hello Guys, How are you all? Hope You all Are Fine. In my app, I am used setOnNavigationItemSelectedListener() but now I am facing issue that ‘setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)’ is deprecated in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How setOnNavigationItemSelectedListener is deprecated Error Occurs ?

In my app, I am used setOnNavigationItemSelectedListener() but now I am facing an issue that.

'setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)' is deprecated 

How To Solve setOnNavigationItemSelectedListener is deprecated Error ?

  1. How To Solve setOnNavigationItemSelectedListener is deprecated Error?

    To Solve setOnNavigationItemSelectedListener is deprecated Error Here you can try setonItemSelectedListener. It is working the same as setOnNavigationItemSelectedListener()

  2. setOnNavigationItemSelectedListener is deprecated

    To Solve setOnNavigationItemSelectedListener is deprecated Error Here you can try setonItemSelectedListener. It is working the same as setOnNavigationItemSelectedListener()

Solution 1: Use setonItemSelectedListener

Here you can try setonItemSelectedListener. It is working the same as setOnNavigationItemSelectedListener()

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

You Can use in kotlin like

bnv.setOnItemSelectedListener { item ->
            when (item.itemId) {
            }
            true
        }

Solution 2: Use NavigationBarView.setOnItemSelectedListener from its base class

You can 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;
  }

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Please Rate Our Solution to Motivate Us For Give Our Best

Leave a Comment