Search code examples
androidbuttonsearchview

No submit button for searchView Android app


I use searchview in my Android app and I would like to add a button that user presses to start the search. Based on what I read on the Internet, I can use setSubmitButtonEnabled to invoke a submit button instead of putting a button in the layout file. Here's my code:

public void setSubmitButtonEnabled (boolean enabled) {

}

I put my setSubmitButtonEnabled in the menu inflater as below:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mylist, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    setSubmitButtonEnabled(true);

    return true;
}

Apparently I am not doing it right because when I launch my app, I don't see any submit button on the screen. What's missing or what's wrong in my code? Is the submit button supposed to appear on the keypad or on the screen? Thank you.


Solution

  • You need to call

    searchView.setSubmitButtonEnabled(true)

    Why would you create your own version with no body and expect it to do anything?