searchView menuItem appears in the collapsible menu then disappears

Viewed 44

My current situation is as so:

My SearchView appears on the menu.

My SearchView appears on the menu.

Normally the collapsible menu does not have the menu for search.

Normally the collapsible menu does not have the menu for search.

When I press the search menu and open the collapsible menu there is Search in the collapsible menu. Why does this happen?

When I press the search menu and open the collapsible menu there is Search in the collapsible menu.

Also, after this happens, if the collapsible menu is closed and the user exits the search, the search icon disappears. How can I resolve the issue?

Search Icon is no longer there!

My onCreateOptionsMenu:

        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.menu, menu)

        val trendingBtn = menu?.findItem(R.id.trending)
        trendingBtn?.setOnMenuItemClickListener {
            viewModel.trending()
            true
        }


        val searchItem = menu?.findItem(R.id.search)
        val searchView = searchItem?.actionView as SearchView

        searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
            override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
                Toast.makeText(this@MainActivity, "Search is Expanded", Toast.LENGTH_SHORT).show()
                return true
            }

            override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
                Toast.makeText(this@MainActivity, "Search is Collapsed", Toast.LENGTH_SHORT).show()
                return true
            }
        })

        searchView.queryHint = "Search"
        searchView.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextChange(s: String): Boolean {
                return false
            }

            override fun onQueryTextSubmit(s: String?): Boolean {
                val searchString = "Searching " + s
                Toast.makeText(this@MainActivity, searchString, Toast.LENGTH_SHORT).show()

                loadSearchVideoList(s)

                val intent = Intent(applicationContext, MainActivity::class.java)
                intent.putExtra(SearchManager.QUERY, s)
                intent.putExtra(
                    "CUSTOM_MESSAGE",
                    "you can also add custom message when submitting the search action"
                )
                intent.action = Intent.ACTION_SEARCH
                startActivity(intent)
                return false
            }
        })

        return super.onCreateOptionsMenu(menu)
    }

My menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:title="@string/search"
        android:icon="@drawable/ic_search"
        android:id="@+id/search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"/>
    <item
        android:title="@string/trending"
        android:icon="@drawable/ic_baseline_trending_up_24"
        android:id="@+id/trending"/>


</menu>
1 Answers

Take a look on invalidateOptionsManu() method, I guess is what you are looking for.

You should call this method after the search is expanded so it can "re-create" the menu.

From doc :

Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(android.view.Menu) method will be called the next time it needs to be displayed.

Related