Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - collapse searchview after submit

I am using searchview in my application ( without action bar). How can i collapse searchview after query text submit ?

I have these listeners ;

        @Override
        public boolean onQueryTextSubmit(String query) {

            InputMethodManager imm = (InputMethodManager)thisFr.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(globalSearch.getWindowToken(), 0);

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            return false;
        }

I don't use ActionBar so i don't have a function like collapseActionView() .

Waiting for help

Thanks

like image 803
dracula Avatar asked May 21 '13 09:05

dracula


4 Answers

You can do it this way in your activity, tested with actionbarsherlock (it even hides the keyboard, make sure to return false in onQueryTextSubmit):

private MenuItem searchMenuItem;

public MenuItem getSearchMenuItem() {
    return searchMenuItem;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // ...
    searchMenuItem = menu.findItem(R.id.menu_search);
    // ...
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            MenuItem searchMenuItem = getSearchMenuItem();
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
            }
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            // ...
            return true;
        }
    });
    // ...
    return super.onCreateOptionsMenu(menu);
}
like image 142
arne.jans Avatar answered Nov 07 '22 22:11

arne.jans


you need to call setIconified(true) twice to actually collapse your search view, with first call text is cleared with second call keyboard and search view get closed.

like image 17
nosaiba darwish Avatar answered Nov 07 '22 21:11

nosaiba darwish


If you are using the SearchView in the OptionsMenu, you ca call invalidateOptionsMenu()

like image 4
Elyess Abouda Avatar answered Nov 07 '22 21:11

Elyess Abouda


//close suggestion list on query text submit
searchView.setIconified(true);
like image 2
moh Avatar answered Nov 07 '22 21:11

moh