18

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;
        }
    });
j.elmer
  • 1,441
  • 2
  • 17
  • 36

7 Answers7

23

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
}
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
Glory
  • 1,007
  • 9
  • 14
  • 3
    What does that "true" stand for? – Chris Nov 24 '17 at 20:40
  • Sorry, the "return" expression is mistakenly removed from code during the last edit. I've fixed it now. Thanks. @Chris – Glory Nov 26 '17 at 14:34
  • when i set return true this show errors message, only false keep error clean.? what is that.. – Ye Htun Z Jan 04 '18 at 15:37
  • 2
    It's not working with the `return true`, you have to add `else -> true` to the when block. Otherwise you will get the error "When expression must be exhaustive, add necessary else branch" – mbo Jan 12 '18 at 08:01
  • @Glory how to set one default menuitem among the items? – Eswar Jul 25 '18 at 14:25
  • @Chris return true to display the item as the selected item. https://developer.android.com/reference/android/support/design/widget/NavigationView.OnNavigationItemSelectedListener – Johnny Five Apr 17 '19 at 08:43
9

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
        }
Thanh vũ
  • 169
  • 3
  • 1
7

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
    }
Ali Hasan
  • 653
  • 9
  • 8
  • wrong! the when is executed only once when the listener is set, so it's not equivalent to the java code in the question – AndrewBloom Nov 05 '19 at 15:21
4
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.

AndrewBloom
  • 2,171
  • 20
  • 30
4

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
        }
Mr. Disability
  • 799
  • 1
  • 10
  • 19
2

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

    }
}
Jafar Sadiq SH
  • 715
  • 9
  • 22
0

kotlin: use setOnItemSelectedListener

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