javaandroiddeprecatedbottomnavigationviewandroid-bottomnavigationview

Latest BottomNavigtionView


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


Solution

  • 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)