Search while typing
I have a working search Activity
that queries a remote database when I send input to ActionBar
android.support.v7.widget.SearchView
(entering "Go" on a soft keyboard). This works great, but ultimately I would like to query the database every time the text SearchView
changes by adding or removing a character. My init code is SearchView
below.
SearchFragment.java (child fragment of the search engine Activity
mentioned above)
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
// Get the searchable.xml data about the search configuration
final SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
// Associate searchable configuration with the SearchView
mSearchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
mSearchView.setSearchableInfo(searchInfo);
mSearchView.requestFocus();
mSearchView.onActionViewExpanded();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mSearchListAdapter.clear();
return false;
}
@Override
public boolean onQueryTextChange(String query) {
mSearchListAdapter.clear();
// Execute search ...
return false;
}
});
}
I am guessing that the job should be done within the method onQueryTextChange(String query)
above, but I'm not sure what to call. I was thinking about calling an instance method SearchManager
startSearch
, but that doesn't seem like best practice. Anyone have experience with the search type and would like to share an effective solution?
UPDATE:
MainActivity.java (search Activity
)
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Handle the search for a particular musical object
final SearchFragment searchFragment = (SearchFragment) getFragmentManager().findFragmentByTag(SearchFragment.TAG);
String query = intent.getStringExtra(SearchManager.QUERY);
mWebService.searchTracks(query, new Callback<Pager>() {
@Override
public void success(Pager results, Response response) {
Log.d(TAG, "Search response received.");
searchFragment.updateItems(results);
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(TAG, "Search response failed: " + retrofitError.toString());
}
});
The aforementioned search interface design is what is recommended by the Android team at Google.
source to share
So far, the only solution I've come across after reading a few pages of documentation is simply dispatching the intent with the action Intent.ACTION_SEARCH
and the current request from SearchView
to start searching Activity
when the SearchView
text changes. Keep in mind that this is probably not best practice in terms of design SearchManager
, but it works. I will change my mind on this approach later and will report back here if I get into anything new.
@Override
public boolean onQueryTextChange(String query) {
mSearchListAdapter.clear();
if (!query.isEmpty()) {
Intent searchIntent = new Intent(getActivity(), MainActivity.class);
searchIntent.setAction(Intent.ACTION_SEARCH);
searchIntent.putExtra(SearchManager.QUERY, query);
startActivity(searchIntent);
}
return false;
}
source to share
The TextWatcher should be what you are looking for. It also offers to execute code before or after text changes.
http://developer.android.com/reference/android/text/TextWatcher.html
When an object of a type is attached to an Editable, its methods will be called when the text is changed.
source to share
It is the perfect solution for the problem you are looking for.
See this helps you ... Android SearchView action bar as autocomplete?
source to share