I\'m currently using ActionBarSherlock 4.2 and it\'s SearchView widget in my app.
I wanted to make it submit query even though it\'s empty. I tried to set imeOptions and
I switched to put SearchView via ActionMode instead. I'll accept this as an answer for now until someone has a better solution for this question.
private void showActionMode(){
MainActivity a = (MainActivity)getActivity();
a.startActionMode(new SearchActionMode());
}
private class SearchActionMode implements ActionMode.Callback{
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onCreateActionMode creating inflater");
MenuInflater inflater = new MenuInflater(getActivity());
Log.d(TAG, "onCreateActionMode inflating");
inflater.inflate(R.menu.interactive_map, menu);
Log.d(TAG, "onCreateActionMode finding SearchView");
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
Log.d(TAG, "mSearchView: " + mSearchView);
setupSearchView();
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onPrepareActionMode");
//TODO add searchView once working
mSearchView.requestFocus();
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.d(TAG, "onActionItemClicked");
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
Log.d(TAG, "onDestroyActionMode");
closeTopTray();
closeListView();
}
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setId(ID_SEARCH_VIEW);
mSearchView.setOnSearchClickListener(this);
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
// To automatically display keyboard when display SearchView
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(TAG, "mSearchView focus changed: " + hasFocus);
if (hasFocus) {
showInputMethod(v.findFocus());
}
}
});
mSearchView.setQueryHint("Type keyword");
mSearchView.setQuery((TextUtils.isEmpty(searchQuery)? "": searchQuery), false);
}
I had the same problem, the problem lies onSubmitQuery() in SearchView.java
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
Empty query's are not supported so I had to download and use ActionBarSherlock and then modify this method.
This is how my onSubmitQuery() looks like now
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
Hope this helps.