Search code examples
androidandroid-studiokotlinmenuitemandroid-bottomnav

ItemID using ChipNavigationBar Android -> kotlin


I am Using this inside onCreate(). I created four fragments for home, like, search and profile. I got this ChipNavgation from https://github.com/ismaeldivita/chip-navigation-bar

lateinit var btm_nav : ChipNavigationBar
    btm_nav = findViewById(R.id.btm_nav)
            val homeFragment = HomeFragment()
            val favoriteFragment = FavoriteFragment()
            val searchFragment = SearchFragment()
            val profileFragment = ProfileFragment()
    
            setCurrentFragment(homeFragment)
            btm_nav.setOnItemSelectedListener({
                when (it.itemId) {
                    R.id.home -> setCurrentFragment(homeFragment)
                    R.id.fav -> setCurrentFragment(favoriteFragment)
                    R.id.search -> setCurrentFragment(searchFragment)
                    R.id.profile -> setCurrentFragment(profileFragment)
    
                }
    
            })

But this code isn't running, could not access the ItemID. error -> Unresolved reference: itemId enter image description here

see the image


Solution

  • setOnItemSelectedListener method callback is itself returning the id of the menu item. it is the id of the menu item. Change it.itemId with it

    btm_nav.setOnItemSelectedListener({
                when (it) {
                    R.id.home -> setCurrentFragment(homeFragment)
                    R.id.fav -> setCurrentFragment(favoriteFragment)
                    R.id.search -> setCurrentFragment(searchFragment)
                    R.id.profile -> setCurrentFragment(profileFragment)
    
                }
    
    })