In my project, I try to remove the extra menu when the search view is clicked and return the menus when canceling the search view. Please find the screenshot below
I use this below code to hide and show the menu, but it is not working as expected. Please find the screenshot below of my implementation.
if (searchMenu is MenuItem) {
searchMenu.setOnActionExpandListener(object :
MenuItem.OnActionExpandListener {
override fun onMenuItemActionCollapse(p0: MenuItem): Boolean {
activity?.invalidateOptionsMenu()
return true
}
override fun onMenuItemActionExpand(p0: MenuItem): Boolean {
toolbar?.menu?.findItem(R.id.menu_add_recipients)?.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
return true
}
})
}
Also, I have tried using setOnSearchClickListener and setOnCloseListener for hiding and showing the menu, but that also not working. Could anyone please help me with this




