" /> " /> "/>

How to hide the keyboard without collapsing the SearchView in Android?

13 views Asked by At

I have an Android activity which has an set up for searching:

<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
           android:resource="@xml/searchable_settings"/>
<meta-data android:name="android.app.lib_name"
           android:value="" />

In the activity, I'm setting up a SearchView in the onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setSubmitButtonEnabled(true);
    searchView.setQueryRefinementEnabled(true);
    searchView.setIconified(false);
    
    int searchPlateId = getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        searchPlate.setBackground(null);
        searchPlate.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        searchPlate.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchView.setQuery(textView.getText(), true);
                searchView.clearFocus();
                searchPlate.clearFocus();
                return true;
            }
            return false;
        });
    }
}

my menu layout is

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/app_bar_search"
        android:orderInCategory="100"
        android:title="@string/search1"
        app:showAsAction="always"
        app:actionViewClass="android.widget.SearchView" />

</menu>

My aim is to hide the soft keyboard and removing focus after pressing the search key on the keyboard, without collapsing the SearchView. I tried using clearFocus(), but it didn't prevent the keyboard from showing up again. I also tried searchView.onActionViewCollapsed();, but I don't want the SearchView to collapse.

Question: How can I hide the soft keyboard without collapsing the SearchView after pressing the search key?

please don't answer me

           InputMethodManager imm = (InputMethodManager) textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
            }

because if the

        int searchPlateId = getResources().getIdentifier("android:id/search_src_text", null, null);
        EditText searchPlate = searchView.findViewById(searchPlateId);`

have focus it will right away pop up

0

There are 0 answers