I have the problem that my menu items on my toolbar are not shown. Specifically, I have one menu item called 'Settings' that encapsulates all other menu items which I want to show when the 'Settings' icon is clicked. But when that icon is clicked, the list of other menu items are not shown to the user.
Here is my my_menu.xml
:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
app:showAsAction="always"
android:icon="@drawable/ic_settings"
android:title="@string/settings">
<menu>
<item
android:id="@+id/loginFragment"
android:menuCategory="secondary"
android:icon="@drawable/ic_login"
app:showAsAction="ifRoom"
android:title="@string/loginOrLogout"/>
<item
android:id="@+id/firstFragment"
android:icon="@drawable/ic_first_fragment_icon"
android:menuCategory="secondary"
app:showAsAction="ifRoom"
android:title="@string/first_fragment_text"/>
<item
android:id="@+id/secondFragment"
android:icon="@drawable/ic_second_fragment"
android:menuCategory="secondary"
app:showAsAction="ifRoom"
android:title="@string/second_fragment_text"/>
</menu>
</item>
</menu>
Here, is my MainActivity.kt
class:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val navController by lazy{ findNavController(R.id.myNavHostFragment)}
private val appBarConfiguration by lazy {
AppBarConfiguration(setOf(R.id.firstFragment), binding.drawerLayout)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
setSupportActionBar(binding.toolbar)
setupNavigationComponents(navController)
setupNavControllerListener()
}
private fun setupNavigationComponents(navController: NavController) {
binding.apply {
toolbar.setupWithNavController(navController, appBarConfiguration)
navView?.setupWithNavController(navController)
bottomNavView?.setupWithNavController(navController)
}
}
private fun setupNavControllerListener() {
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id in arrayOf(
R.id.firstFragment)){
toolbar.visibility = View.GONE
bottom_nav_view?.visibility = View.GONE
nav_view?.visibility = View.GONE
}else{
toolbar.visibility = View.VISIBLE
bottom_nav_view?.visibility = View.VISIBLE
nav_view?.visibility = View.VISIBLE
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.my_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean =
/*
* Have the NavigationUI look for an action or destination matching the menu
* item id and navigate there if found.
* Otherwise, bubble up to the parent.
* */
item.onNavDestinationSelected(findNavController(R.id.myNavHostFragment))
|| super.onOptionsItemSelected(item)
override fun onSupportNavigateUp(): Boolean {
// Allows NavigationUI to support proper up navigation or the drawer layout
// drawer menu, depending on the situation
return findNavController(R.id.myNavHostFragment).navigateUp(appBarConfiguration)
}
}
In the following, I show a screenshot of the right top of my screen where you can see the 'Settings' icon:
When I click on that icon, I get this result:
As you can see, no menu item is shown to the user. The weird thing is that when I click somewhere inside the 'opened white box', then the app navigates to the corresponding fragment. But the items are not shown, why ?
What am I doing wrong here ?