fralbo
fralbo

Reputation: 2664

How to automatically enter in edit mode in a search?

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

Answers (1)

Frank B.
Frank B.

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.

  1. Get a the InputMethodManager
  2. Call the method to show the soft keys and pass it the search view and a flag, SHOW_FORCED
  3. 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

Related Questions