Reputation: 2664
I've implemented a classic search feature in my action bar but when I click on the search menu button, the search editText appears but I have to click on it to enter in edit mode whereas most of application directly enter in edit mode after displaying the editText.
How to fix it?
My code is just:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Place an action bar item for searching.
MenuItem item = menu.add("Search");
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView = new Fragment_siteManager.MySearchView(getActivity());
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setQueryHint(getString(R.string.label_tvGivePosition));
item.setActionView(mSearchView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
EDIT
I tried the following code but it doesn't work:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
View view = mSearchView.findFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
return super.onOptionsItemSelected(item);
}
Regards,
Upvotes: 3
Views: 578
Reputation: 204
According to the Android docs, SearchView has Edittext behavior so you shouldn't need to force the softkey. It seems your issue is that it is in the onOptionItemSelected() when it should be in the onCreateOptionMenu(). If you MUST, the code to force is below. Take a careful long look at the links at the bottom of this post.
SHOW_FORCED
Done!
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mSearchView, InputMethodManager.SHOW_FORCED);
Look at the class to get more info. Also look at the Android Docs on SearchView.
Upvotes: 1