Bhargav Thanki
Bhargav Thanki

Reputation: 4954

I can't Hide my keyboard after search on search view

    final android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setFocusable(false);

    searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchView.getWindowToken(),
                    InputMethodManager.RESULT_UNCHANGED_SHOWN);


            if (query.length() >= 3) {

                String urlfilter = "http://frenzinsoftwares.in/alert/apis/search.php" +
                        "?key=" + query.toString();

                if (isNetworkAvailable()) {
                    new GetMyAppliancesfilter().execute(urlfilter);
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }
            }

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {


            if (isSearch) {
                if (isNetworkAvailable()) {

                    if (newText.length() == 0) {

                        String url2 = "http://frenzinsoftwares.in/alert/apis/filter_by_category.php" +
                                "?category=" + selcategory.toString() +
                                "&city=" + 1;

                        new Filtercategory().execute(url2);
                    }
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }

            } else {

                isSearch = true;

            }

            if (newText.length() >= 3) {

                String urlfilter = "http://frenzinsoftwares.in/alert/apis/search.php" +
                        "?key=" + newText.toString();

                if (isNetworkAvailable()) {
                    new GetMyAppliancesfilter().execute(urlfilter);
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }
            }
            return false;


        }
    });

Upvotes: 1

Views: 287

Answers (3)

Divyesh Patel
Divyesh Patel

Reputation: 2576

try this in onQueryTextSubmit:

searchView.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InputMethodManager imm = (InputMethodManager) searchView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                        imm.hideSoftInputFromWindow(searchView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                                    }
                                }, 50);  

try below:

searchView.clearFocus();
                if (getCurrentFocus()!=null){
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }

Upvotes: 1

Satan Pandeya
Satan Pandeya

Reputation: 3815

Create a method to hide a keyboard and call it :

private void hideKeyboard() {
    View view = this.getActivity().getCurrentFocus();
    if (view != null) {
        view.clearFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

Upvotes: 0

mohitum
mohitum

Reputation: 936

try to use a genral way to hide the keyboard:

View activeView = activityContext.getCurrentFocus();
if (activeView != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(activeView.getWindowToken(), 0);
}

Upvotes: 0

Related Questions