Search code examples
androidsearchandroid-intentactionbarsherlock

how add search in actionbar sherlock


I am using the actionbar sherlock. Now I want to add a search option to actionbar and intent Edittext search to other activity.

can anyone help me?


Solution

  • Do something like this

    @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");
    
    
        menu.add(Menu.NONE,Menu.NONE,1,"Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    
        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() > 0) {
                    // Search
    
                } else {
                    // Do something when there's no input
                }
                return false;
            }
            @Override
            public boolean onQueryTextSubmit(String query) { 
    
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
    
                Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
                setSupportProgressBarIndeterminateVisibility(true);
    
                Handler handler = new Handler(); 
                handler.postDelayed(new Runnable() { 
                     public void run() { 
                         setSupportProgressBarIndeterminateVisibility(false);
                     } 
                }, 2000);
    
                return false; }
        });
    
        return true;
    }