Android: how do I get SearchView close button to return the setQueryHint()?
Asked Answered
F

1

1

I have a Search icon in my toolbar that searches a RecyclerView list.

Pic 1:

enter image description here

When the icon is clicked, the toolbar shows the SearchView's setQueryHint " Search here...".

Pic 2:

enter image description here

When a character is entered on the SearchView line, in this case the number "4", it is then searched in the RecyclerView list. If the character is not found, a close button "x" appears to the right of the character:

Pic 3:

enter image description here

My problem is that the default behavior when clicking on the close button "x" is to close the SearchView which returns the view back to Pic 1, the initial icon View. I would like to have the View return to the SearchView line "Search here...", which is Pic 2. I try to use the setQueryHint on the SearchView in the mCloseButton.OnClickListener but it doesn't work. See the ** below in the mCloseButton OnClickListener() for the other snippets I tried that did not work. What am I missing here?

menu.xml

<item android:id="@+id/action_search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"  />   

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.cardview_menu, menu);

    final MenuItem searchItem = menu.findItem(R.id.action_search);

    if (adapter == null || adapter.getItemCount() == 0) {
        searchItem.setVisible(false);
    } else {
        searchItem.setVisible(true);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);            
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        final EditText mSearchEditText = (EditText)mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        final ImageView mCloseButton = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        mSearchView.setQueryHint(" Search here...");

        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;  // Return true to expand action view
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {

                adapter.clear();
                adapter.addAll(allList);
                return true;  // Return true to collapse action view
            }
        });

        mCloseButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Clear the query
                mSearchView.setQuery("",false);
                // below lines were all tried and do not work
                **mSearchView.setQueryHint(" Search here...");**
                **mSearchView.setIconified(false);**
                **mSearchView.onActionViewCollapsed);**
                **MenuItemCompat.expandActionView(searchitem);**
                adapter.clear();
                adapter.addAll(allList);
            }
        });

...
return super.onCreateOptionsMenu(menu);
}            
Farming answered 30/4, 2017 at 1:30 Comment(2)
What doesn't work: the click listener or the query clear?Ibex
The query clear. I would like the query clear to return to where the user can enter another search character rather than being returned to the initial state of seeing the Search icon and having to click on the Search icon again.Farming
A
0

Try using mSearchView.setIconified(false).

Alic answered 17/3, 2020 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.