0

I am creating an Android app and I stumbled upon the fact that the previous way to create a bottom navigation view has been deprecated.

This is the error I am getting:

'setOnNavigationItemReselectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemReselectedListener)' is deprecated

I am searching everywhere for this, but there is no website that shows the latest & updated way to do this.

What is the latest way to create a proper BottomNavigtionView? Thanks in Advance

  • Possible duplicate of https://stackoverflow.com/questions/68248515/bottomnavigationview-setonnavigationitemreselectedlistener-is-deprecated-with-ma – art Aug 31 '21 at 10:21
  • 1
    Does this answer your question? [BottomNavigationView setOnNavigationItemReselectedListener is deprecated with Material compoment version 1.4.0. What to user now?](https://stackoverflow.com/questions/68248515/bottomnavigationview-setonnavigationitemreselectedlistener-is-deprecated-with-ma) – Stefano Sansone Aug 31 '21 at 10:27
  • Thanks a lot....that was the thing I was looking for. I wonder how I missed that page, I was surfing for almost 30 mins!! –  Aug 31 '21 at 10:28

1 Answers1

0

Yes setOnNavigationItemReselectedListener is deprected now. To perform click operation on Google Bottom navigation you have to use the bottomNavigation.setOnItemSelectedListener(this) and extend NavigationBarView.OnItemSelectedListener in your actvity/fragment

bottomNavigation.setOnItemSelectedListener(this) and then override onNavigationItemSelected(item: MenuItem)

Example Kotlin:

bottomNavigation.setOnItemSelectedListener(this)
override fun onNavigationItemSelected(item: MenuItem): Boolean {

    when(item.itemId){
        R.id.your_menu_id->{
            Toast.makeText(this,"Click",Toast.LENGTH_SHORT).show()
        }
        ///
    }
    return true
}

or

bottomNavigationView.setOnItemSelectedListener{
        when (it.itemId) {
            R.id.your_menu_id-> {
               Toast.makeText(this,"Click",Toast.LENGTH_SHORT).show()
                return@setOnItemSelectedListener true
            }
           ///
        }
        false
    }

And for reselection use bottomNav.setOnItemReselectedListener(this) and extend NavigationBarView.OnItemReselectedListener and override onNavigationItemReselected(item: MenuItem)

Sniffer
  • 1,495
  • 2
  • 14
  • 30