1

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?

sajad aln
  • 13
  • 2
  • 6
  • This will be help full http://stackoverflow.com/questions/11690525/how-to-implement-search-widget-in-action-bar-sherlock – Triode Feb 20 '13 at 17:23
  • This training could not help me. I want search option with the search button when click on the butten intent EditText to another activity . – sajad aln Feb 20 '13 at 18:22

1 Answers1

5

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;
}
Androider
  • 2,884
  • 5
  • 28
  • 47
  • 1
    you can use getBaseContext() instead of context. by the way context is the activity's context (i.e. this). – Androider Feb 22 '14 at 16:16