Remove extra menu when search view is click

108 views Asked by At

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

enter image description here enter image description here enter image description here

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.

enter image description here

enter image description here enter image description here

  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

2

There are 2 answers

0
ltp On
//hide
toolbar?.menu?.findItem(R.id.menu_add_recipients)?.isVisible = false

//show
toolbar?.menu?.findItem(R.id.menu_add_recipients)?.isVisible = true
0
AnumShafiq On

Sorry about the misunderstanding. Please share your Xml file or compose code as it seems to have some extra padding or margins added to your view