j.elmer
j.elmer

Reputation: 1451

How to setOnNavigationItemListener on BottomNavigationView in android using Kotlin?

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;
        }
    });

Upvotes: 18

Views: 29343

Answers (7)

razi
razi

Reputation: 97

kotlin: use setOnItemSelectedListener

bottomNavigationView.setOnItemSelectedListener { item: MenuItem ->
        when (item.itemId) {
             R.id. ... -> {
                Add your code
             true
        }
        
        else ->
            true
    }

Upvotes: 0

Glory
Glory

Reputation: 1077

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
}

Upvotes: 23

Mr. Disability
Mr. Disability

Reputation: 838

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
        }

Upvotes: 4

Ali Hasan
Ali Hasan

Reputation: 673

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
    }

Upvotes: 7

AndrewBloom
AndrewBloom

Reputation: 2438

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.

Upvotes: 4

Thanh vũ
Thanh vũ

Reputation: 169

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()
            return@setOnNavigationItemSelectedListener true
        }

Upvotes: 9

Jafar Sadiq SH
Jafar Sadiq SH

Reputation: 734

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
            }
        }

    }
}

Upvotes: 2

Related Questions