Question

I have a collapsible search box. I want to capture the text change event and perform a search on a list. But I am not able to capture the onTextChanged event.

Menu Resource:

<item android:id="@+id/action_search"
      android:icon="@drawable/abs__ic_search"
      android:title="Search"
      android:showAsAction="always"
      android:iconifiedByDefault="false"
      android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>

Activity:

import com.actionbarsherlock.view.MenuItem;
    public class FromPageActivity extends SherlockListActivity {
        EditText search;
    //more code here
    .
    .
    .
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                search(item);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    public void search(MenuItem item){
            search = (EditText) item.getActionView();
            search.addTextChangedListener(filterTextWatcher);
            search.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
        private TextWatcher filterTextWatcher = new TextWatcher(){
            public void afterTextChanged(Editable s)
            {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                Toast.makeText(FromPageActivity.this, "changed", Toast.LENGTH_LONG).show();
            }
        };
    }

What am I missing out?

Was it helpful?

Solution

Try to use com.actionbarsherlock.view.MenuItem instead of MenuItem as parameter in search and onOptionMenuItemSelected methods

Also you can try to use SearchView.OnQueryTextListener to detect text changing, for example:

public boolean onCreateOptionsMenu(Menu menu) 
{
        getSupportMenuInflater().inflate(R.menu.menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        if (null != searchView )
        {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);   
        }

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            public boolean onQueryTextChange(String newText) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(newText);
                return true;
            }

            public boolean onQueryTextSubmit(String query) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top