Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBarSherlock, SearchView `setOnCloseListener` is not working

I am using SearchView and it is working fine but only setOnCloseListener is not working; Here is my code

import com.actionbarsherlock.widget.SearchView.OnCloseListener;

and

searchView.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {

                Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

**EDIT****

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar
        //Create the search view
        final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search");
        searchView.setIconifiedByDefault(true);

    //search button
    menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
        .setIcon(R.drawable.abs__ic_search_api_holo_light)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        MenuItem sView =  menu.findItem(1);

        sView.setOnActionExpandListener(this);

return true;

}

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        adopter.getFilter().filter(null);
        Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
        return true; // Return true to collapse action view
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
        return true; // Return true to expand action view
    }
like image 311
Androider Avatar asked Mar 17 '13 10:03

Androider


1 Answers

Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu

//        searchView.setOnCloseListener(new OnCloseListener() { 
//          @Override
//          public boolean onClose() {
//              adapter.getFilter().filter("");
//              Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show(); 
//              return false;
//          }
//      });

        MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {   
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }
        });
like image 157
Androider Avatar answered Oct 15 '22 04:10

Androider