SearchView mSearchView;
MenuItem mMenuItem;
mMenuItem = menu.findItem(R.id.action_search);
mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
// mSearchView = (SearchView) MenuItemCompat.getActionView(mMenuItem);
// <-- MenuItemCompat.getActionView is deprecated
mSearchView = (SearchView) mMenuItem.getActionView();
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
//Log.i(TAG, "Query Text Change:" + newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
mSearchView.setQuery("", false); // Clear query on TextView
mSearchView.clearFocus();
//mSearchView.requestFocus();
// Hide search text
mSearchView.setIconifiedByDefault(true);
if (mSearchView != null) {
mMenuItem.collapseActionView();
}
SearchActivity.this.setTitle(String.format("%s : %s", mTitle, query));
// TO-DO
return false;
}
});
Leave a comment