22

I'm trying to clear and close SearchView after entering a value. I found a solution but it closes the search and won't perform any action if I try to search again.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);

    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(searchListener);

    return super.onCreateOptionsMenu(menu);
}

SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
    @Override
    public boolean onQueryTextChange(String arg0) {
        return false;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
        try {
            searchView.onActionViewCollapsed();
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.out.println(ex);
        }
        return true;
    }
};

I can search only for the first time. Every next time it only closes my input keyboard and does nothing. How can it be performed in a right way?

Edit. Suggested change looks like this:

        try {
            searchView.setIconified(true);
        }

activity_main_actions.xml:

   <!-- Search Widget -->
   <item android:id="@+id/action_search"
       android:icon="@drawable/ic_action_search"
       android:title="@string/action_search"
       android:showAsAction="always"
       android:actionViewClass="android.widget.SearchView"/>

Edit2:

I changed showAsAction="always|collapseActionView" but it closes my app when I click the search.

I also put the listener inside OnCreateOptionsMenu to see if it changes anything:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    searchMenuItem = menu.findItem(R.id.action_search); 
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener(){

            @Override
            public boolean onQueryTextChange(String arg0) {
                return false;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                if (searchMenuItem != null) {
                    boolean closed = searchMenuItem.collapseActionView();
                    Toast.makeText(getApplicationContext(), "closing: " + closed, Toast.LENGTH_SHORT).show();
                }
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }
    });    
    return super.onCreateOptionsMenu(menu);
}

And the closed boolean is false - I don't know why. I read the doc but it tells me nothing.

Edit3:

I've read too much Internet on this subject and it is not clear for me yet. Search widget is cleared, keyboard is hidden and action is performed. The only issue is the search isn't collapsed so it covers the rest of action buttons on the ActionBar.

            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.setIconified(true);
                searchView.clearFocus();
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }

Here I read that collapseActionView() will not work, because my Search is not a view but a widget.

Community
  • 1
  • 1
Kamil
  • 1,456
  • 4
  • 32
  • 50
  • Why aren't you using setIconified() http://developer.android.com/reference/android/widget/SearchView.html#setIconified%28boolean%29 ? You shouldn't call onActionViewCollapsed(). – user May 29 '14 at 07:56
  • @Luksprog it works only in part. It erases typed query and performs the action but input keyboard is not hidden and search field is not closed. – Kamil May 29 '14 at 08:19
  • How exactly do you search again? Nothing happens? I've checked both onActionViewCollapsed() and setIconfied() and both clear the text, close the SearchView and remove the keyboard. Next searches are ok. – user May 29 '14 at 10:48
  • Next search works ok - there was Internet connection problem. setIconified() clears the field but focus isn't lost and cursos is blinking in the search field and keyboard is still on. onActionViewCollapsed() won't allow to search again and it hides the field without erasing the value in first search. In next searches it simply closes the keyboard. I included part of activity_main_actions.xml maybe it would help – Kamil May 29 '14 at 21:52
  • In my case, setIconified(true); must be called twice sequentially before it actually collapsed. – Chlind Jul 20 '16 at 13:25
  • Check the answer for similar question : https://stackoverflow.com/a/46930606/1308990 – oiyio Oct 25 '17 at 11:00

7 Answers7

49

Use:

searchMenuItem.collapseActionView();

Instead of:

searchView.onActionViewCollapsed();

If you are using AppCompat library, then use:

MenuItemCompat.collapseActionView(searchMenuItem);

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Newinjava
  • 972
  • 1
  • 12
  • 19
13

In my case calling invalidateOptionsMenu(); closes SearchView

Rafael
  • 6,091
  • 5
  • 54
  • 79
4

Solution mentioned here is simple and works perfectly.

Basically, Call setQuery("", false) and setIconified(true) on SearchView.

Community
  • 1
  • 1
Darshan Dorai
  • 659
  • 8
  • 10
3

In menu_main.xml add :

<item android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search"
    app:showAsAction="collapseActionView|ifRoom"
    android:orderInCategory="1"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:menuCategory="secondary"
    />

and in onCreateOptionsMenu

final  MenuItem miSearch = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) miSearch.getActionView();
    searchView.setQueryHint("Searh For");
    searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Toast.makeText(context,query, Toast.LENGTH_LONG).show();
            // Here Put Your Code.
            //searchView.onActionViewCollapsed();
            miSearch.collapseActionView();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
Khaled Jas
  • 31
  • 1
1

It sounds like what you are looking for is a way to close the search bar programmatically. Unfortunately, there is not a method or an equivalent workaround for this. You may have already seen another post that had some suggestions, but no real way to do this.

Your first edit above that calls setIconified(true) is the best alternative. The docs suggest calling setIconified(true) should collapse the search widget, clear it, and remove it from focus if the attribute iconifiedByDefault is true (which it is by default so I don't see any problem with your activity_main_actions.xml).

Community
  • 1
  • 1
strwils
  • 687
  • 4
  • 12
1

Hi i faced a similar scenario and so i think this changed to code should do the trick.
Hope this helps...:)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
Menu mMenuItem = menu.findItem(R.id.action_search_loc);  // take a reference with menue item
searchView = (SearchView) mMenuItem.getActionView(); //use that to find searchview
searchView.setOnQueryTextListener(searchListener);

return super.onCreateOptionsMenu(menu);
}

SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String arg0) {
    return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
    new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query +    "&lang=pl").execute();
    try {
        mMenuItem.collapseActionView(); //this will collapse your search view
    }
    catch(Exception ex){
        ex.printStackTrace();
        System.out.println(ex);
    }
    return true;
}
};
Max
  • 510
  • 5
  • 16
1

Activity:

if (!searchView.isIconified())
        search.collapseActionView();

Fragment:

@Override
public void onPause() {
    super.onPause();
    if (!searchView.isIconified())
        search.collapseActionView();
}