'Change lambda expression return type to Unit navigation bar
i am very beginner. sorry if its too easy or im stupid. i want to make bottom navigation menu with material design 3 kotlin. but this error comes. i want to change visibility of scroll view. what is problem?
again, im using this: m3.material.io
NavigationBarView.OnItemSelectedListener { item ->
when(item.itemId) {
R.id.main -> {
likes.visibility = View.GONE
mainmenu.visibility = View.VISIBLE
}
R.id.starred -> {
mainmenu.visibility = View.GONE
likes.visibility = View.VISIBLE
}
else -> false
}
}
Solution 1:[1]
Going off memory, but I think you missed the word set, as in setOnItemSelectedListener, and the listener needs to return a Boolean for all cases. You only returned it for the else case. Very unlikely you need to worry about returning false (for fall-through behavior), so I'd just return treu after the when statement.
NavigationBarView.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.main -> {
likes.visibility = View.GONE
mainmenu.visibility = View.VISIBLE
}
R.id.starred -> {
mainmenu.visibility = View.GONE
likes.visibility = View.VISIBLE
}
else -> { }
}
true
}
If you're possibly going to be adding more tabs, I suggest doing your logic like this so it's easier to maintain all the possible cases without having to repeat similar lines of code. The way you're doing it now, you have to write a line of code per tab for each view, and make sure you get the visibility right for each case. You can use the isVisible extension property to hide/show views with Boolean logic.
NavigationBarView.setOnItemSelectedListener { item ->
mainmenu.isVisible = item == R.id.main
likes.isVisible = item == R.id.starred
true
}
Solution 2:[2]
I think instead of the onItemSelected, It should be setNavigationItemSelectedListener. I had the same issue with the material top nav bar so, maybe try this out too. Also, try adding log statements and verify if the click statements are working. here's a sample code for reference -
binding.navigationViewMainScreen.setNavigationItemSelectedListener {
Log.d(TAG, "navigationViewMainScreen working")
when (it.itemId) {
R.id.log_in -> {
loginPrompt()
true
}
R.id.log_out -> {
Toast.makeText(requireContext(), "Logged Out", Toast.LENGTH_SHORT)
.show()
CoroutineScope(Dispatchers.IO).launch {
accountDataStore.logOut(requireContext())
}
true
}
R.id.sign_up -> {
findNavController()
.navigate(
MainScreenFragmentDirections.actionMainScreenFragmentToSignUpFragment()
)
true
}
R.id.remove_account -> {
Toast.makeText(requireContext(), "Account Removed", Toast.LENGTH_SHORT)
.show()
CoroutineScope(Dispatchers.IO).launch {
accountDataStore.resetAccounts(requireContext())
}
true
}
R.id.switch_account -> {
switchAccountPrompt()
true
}
R.id.contract_interface -> {
if (userLoggedIn) {
findNavController()
.navigate(
MainScreenFragmentDirections
.actionMainScreenFragmentToContractScreenFragment()
)
} else {
Toast.makeText(
requireContext(), "Log In Necessary To Continue", Toast.LENGTH_SHORT
).show()
loginPrompt()
}
true
}
R.id.about -> {
findNavController()
.navigate(
MainScreenFragmentDirections
.actionMainScreenFragmentToAboutFragment()
)
Log.d(TAG, "about working")
true
}
R.id.exit -> {
Log.d(TAG, "exit working")
logOutAndExit()
true
}
else -> {
throw IllegalArgumentException("side menu item not registered.")
}
}
}
just focus on the content inside. like this one -
R.id.log_in -> {
loginPrompt()
true
}
here we can see an action is performed and it returns true to verify that. Just don't forget the true at the ends. that is used to verify an action has been performed after the button was clicked. Hope it helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Vaishnav Kanhirathingal |
