onItemSelectedListener for bottomnavigationbar

2.9k Views Asked by At

I am new to android and Kotlin, developing a bottom navigation bar using onitemselectedListener, since setOnNavigationItemSelectedListener is deprecated and I couldn't find any youtube tutorial that explains how to used onitemselectedlistener for navigationbar. navigation shows up on the emulator, but fragments are not showing up when i click on navigation Icons. here are my codes.

adding image of activity_main and emulator error image

fragmentWord image, that is connected to the first icon of "A" but doesn't show up

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.aryanvedh.vocabapp2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val wordFragment = WordFragment()
        val memorisedFragment = MemorisedFragment()
        setCurrentFragment(wordFragment)

        binding.bottomNavigationView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.words -> setCurrentFragment(wordFragment)
                R.id.memorised -> setCurrentFragment(memorisedFragment)
            }
            true
        }

    }

    private fun setCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.flFragment, fragment)
            commit()
    }
}```


any help? thanks 
3

There are 3 best solutions below

1
Saikiran Kopparthi On

You need to use the OnNavigationItemSelectedListener Method to capture once the item is selected in Bottom Navigation.

Here is the sample code attached from Documentation.

BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when(item.itemId) {
        R.id.item1 -> {
            // Respond to navigation item 1 click
            true
        }
        R.id.item2 -> {
            // Respond to navigation item 2 click
            true
        }
        else -> false
    }
}

Please refer to the Official Documentation for more information about BottomNavigationView.

0
jotaro-gogogo On

Use the function like this:

    val navBar = findViewById<NavigationBarView>(R.id.navBar)

    navBar.setOnItemSelectedListener {
        when(it.itemId) {
            R.id.item01 -> {
                setCurrentFragment(Fragment01())
                true
            }
            R.id.item02 -> {
                setCurrentFragment(Fragment02())
                true
            }
            R.id.item03 -> {
                setCurrentFragment(Fragment03())
                true
            }
            else -> {
                false
            }
        }
    }

It now should raise the fragments when each navigation bar item is selected.

0
Anubhav Singh On

I got a similar issue but the main problem was not with the lambda function onItemSelectedListner. The problem is with the view binding, just use the traditional findViewById method and you are good to go.