RobGThai
RobGThai

Reputation: 5969

SearchView imeOptions and onQueryTextSubmit support

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 OnKeyListener but both were ignored without a reason.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d(TAG, "onCreateOptionsMenu");
    inflater.inflate(R.menu.interactive_map, menu);
    mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    mSearchView.setImeOptions(EditorInfo.IME_ACTION_GO);
    mSearchView.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            Log.d(TAG, "keyCode: " + keyCode); 
            Log.d(TAG, "Action: " + event.getAction());
            if(keyCode == EditorInfo.IME_ACTION_SEARCH){
                onQueryTextSubmit(""+mSearchView.getQuery());
            }

            return false;
        }
    });


    super.onCreateOptionsMenu(menu, inflater);
}

onKey's never get triggered as both Logcat entries never appear in Logcat window. Not sure if this being inside SherlockFragment is the problem.

Nothing special's done to the menu item as well.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_search"
          android:title="@string/app_name"
          android:icon="@android:drawable/ic_menu_search"
          android:showAsAction="always"
          android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>

Is there a way to get around this? Or is there simpler way to enable submitting when query is empty? It doesn't seems to support any configuration in the source code though.

Upvotes: 3

Views: 6318

Answers (2)

M3rd0n
M3rd0n

Reputation: 321

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.

Upvotes: 0

RobGThai
RobGThai

Reputation: 5969

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);
}

Upvotes: 1

Related Questions