Home » Android » How to setOnNavigationItemListener on BottomNavigationView in android using Kotlin?

How to setOnNavigationItemListener on BottomNavigationView in android using Kotlin?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I use kotlin-android-extension and I can call bottomNavigationView id from layout file to kotlin file. I can use bottomNavigationView.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener {}), but whats next?

As far as I know in Java, there is another function called onNavigationItemSelected, but I can’t find it in kotlin.

this is the example code I want to use in Java but cannot write it in kotlin.

bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_favorites:

                case R.id.action_schedules:

                case R.id.action_music:

            }
            return true;
        }
    });
How to&Answers:

You can use this format of code:

bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.action_favorites -> {
        }
        R.id.action_schedules -> {
        }
        R.id.action_music -> {
        }
    }
    true
}

Answer:

You can use below code

bottom_navigation.setOnNavigationItemSelectedListener {
            var selectedFragment: Fragment = A()
            when (it.itemId) {
                R.id.action_item1 -> selectedFragment = A()
                R.id.action_item2 -> selectedFragment = B()
                R.id.action_item3 -> selectedFragment = C()
            }
            val transaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.frame_layout, selectedFragment)
            transaction.commit()
            [email protected] true
        }

Answer:

use must add annotation for return the lambda only

 bottomNavigation.setOnNavigationItemSelectedListener { item ->
        when(item.itemId){
            R.id.home -> {}

            R.id.group -> {}

            R.id.profile -> {}
        }
        return true
    }

Answer:

bottomNavigationView.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.action_favorites -> { }
                R.id.action_schedules -> { }
                R.id.action_music -> { }
            }
            true
        }

note that last line seems to miss the return keyword, but

The last expression in a lambda is considered the return value:

from https://kotlinlang.org/docs/reference/lambdas.html

Moreover, @setOnNavigationItemSelectedListener creates a

local final fun <anonymous> (it: Menuitem) : Boolean

wrapping what follows, so in some answers this will have the effect of executing the when block only when the listener is set (only one time), and the callback will be just a return true statement.

Answer:

This is my code using the new Navigation component.
Let me know if you need help with nav ui.

bottom_nav.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.home -> {
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.mainFragment)
                }
                R.id.search -> {
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.searchFragment)
                }
                R.id.inapppurchases -> {
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.inappPurchasesFragment)
                }
                R.id.settings -> {
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.settingsFragment)
                }
            }
            true
        }

Answer:

return boolean in each block because setOnNavigationItemSelectedListener expects return type

fun initViews() {
    bottomNavigationView.setOnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.action_menu_media -> {
                true
            }
            R.id.action_menu_tag -> {
                true
            }
            R.id.action_menu_home -> {
                true
            }
            else -> {
                true
            }
        }

    }
}