Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the soft keyboard for SearchView on ActionBar

We've got a SearchView on the ActionBar which is set to be non-iconified. As we don't have any content in the view until the user's entered something to search for, it would make sense to give the SearchView initial focus, and make sure the soft keyboard is showing ready for the user to enter text — otherwise they'll always have to first tap in the SearchView.

I can give the SearchView focus by just calling

searchView.requestFocus();

but I can't get the soft keyboard to appear. In another one of our Fragments I have an EditText which we want to be focused I can get the soft keyboard to appear there by calling

InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

but this just doesn't work on the SearchView. It must surely be possible to get this to work.

like image 582
Jonathan Caryl Avatar asked Mar 05 '13 12:03

Jonathan Caryl


People also ask

How to create SearchView in android studio?

Go to app -> java -> right click on package-> New -> Java Class and create ListViewAdapter. java and add following code. Here we extends BaseAdapter in ListViewAdapter class and then set the data in the ListView by using Modal class. Step 5: Now Create new a new layout Activity.

How to make search button in android?

Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.

How to add search Bar in android application?

To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.


4 Answers

Further rummaging around StackOverflow and I found this question:

Forcing the Soft Keyboard open

which contains a solution that worked for me:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
    toggleSoftInput(InputMethodManager.SHOW_FORCED,
                    InputMethodManager.HIDE_IMPLICIT_ONLY);
like image 190
Jonathan Caryl Avatar answered Oct 10 '22 06:10

Jonathan Caryl


I have a similar problem where none of the proposed solutions here worked. Some just didn't make the keyboard appear at all and some show a keyboard but the key presses there just do not work.

The only thing that worked was:

                    // hack for making the keyboard appear
                    searchView.setIconified(true);
                    searchView.setIconified(false);
like image 36
Ulrich Scheller Avatar answered Oct 22 '22 08:10

Ulrich Scheller


Further rummaging around StackOverflow and I found this question:

Forcing the Soft Keyboard open

which contains a solution that worked for me:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
    toggleSoftInput(InputMethodManager.SHOW_FORCED,
                    InputMethodManager.HIDE_IMPLICIT_ONLY);
like image 22
Jonathan Caryl Avatar answered Oct 22 '22 09:10

Jonathan Caryl


I am using a SearchView with setIconifiedByDefault(false). Testing with Android 4.4.2, the only way I could get the keyboard to actually show was to look at the source code for SearchView and mimic how it requested the keyboard to be shown. I've tried literally every other method I could find/think of and this is the only way I could get the keyboard to show reliably. Unfortunately, my method requires some reflection.

In onCreateOptionsMenu(Menu):

searchView.requestFocus();
searchView.post(new Runnable() {
    @Override
    public void run() {
        showSoftInputUnchecked();
    }
});

And then create a method to call the hidden method "showSoftInputUnchecked" in InputMethodManager:

private void showSoftInputUnchecked() {
    InputMethodManager imm = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm != null) {
        Method showSoftInputUnchecked = null;
        try {
            showSoftInputUnchecked = imm.getClass()
                    .getMethod("showSoftInputUnchecked", int.class, ResultReceiver.class);
        } catch (NoSuchMethodException e) {
            // Log something
        }

        if (showSoftInputUnchecked != null) {
            try {
                showSoftInputUnchecked.invoke(imm, 0, null);
            } catch (IllegalAccessException e) {
                // Log something
            } catch (InvocationTargetException e) {
                // Log something
            }
        }
    }
}

As with all solutions that access methods not in the public API, I can't promise that this won't break with new versions of Android.

like image 6
Jamie Avatar answered Oct 22 '22 07:10

Jamie