Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to expand SearchView without showing the keyboard

I have SearchView in action bar. It expands when user clicks it and then keyboard appears. This is expected behavior. However, in my app there's one case when I expand SearchView programmatically.

MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.expandActionView(searchMenuItem);

I don't want to show keyboard in this case. I expand SearchView programmatically just to show user what was the last search query.

So is it possible to open search view without showing the keyboard?

like image 643
Egis Avatar asked Sep 13 '25 14:09

Egis


2 Answers

I resolve my problem using this:

@Override
    public void onPrepareOptionsMenu(Menu menu) {
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
        if(!TextUtils.isEmpty(mSearch)){
            searchView.setQuery(mSearch, false);
            searchView.setIconified(false);
            searchView.clearFocus();
        }
    }

Let me know if this code resolve your problem.

like image 183
gersonmdesouza Avatar answered Sep 16 '25 03:09

gersonmdesouza


I used below code to expand searchview without opening keyboard:

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();

Hope this helps someone.

like image 21
Lakhan Sharma Avatar answered Sep 16 '25 03:09

Lakhan Sharma