Reputation: 2035
I have been trying to find the answer but maybe I am not asking google correctly so here it goes: I have a search view in my android app that has the top menu item as a search input, etc. I click the magnifier, and then textfield becomes editable for search term. How can I skip this step and make it that, when onCreate() method of my activity, the search field gets the focus automatically without the need for clicking the magnifier icon?
Here is my code:
MenuInflater inflater = new MenuInflater(SearchMedicineActivity.this);
inflater.inflate(R.menu.search_view, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnQueryTextListener(this);
mSearchView.setIconifiedByDefault(false);
return true;
Upvotes: 1
Views: 1339
Reputation: 538
Calling setIconifiedByDefault(false)
inside the public boolean onPrepareOptionsMenu(Menu menu)
should get you the result you want. If you want the keyboard to be visible, try calling requestFocus()
as well.
Upvotes: 2
Reputation: 28063
From http://developer.android.com/reference/android/widget/SearchView.html
When the SearchView is used in an ActionBar as an action view for a collapsible menu item, it needs to be set to iconified by default using
setIconifiedByDefault(true)
. This is the default, so nothing needs to be done.If you want the search field to always be visible, then call
setIconifiedByDefault(false)
.
Upvotes: 0