Expand and give focus to SearchView automatically

后端 未结 17 2425
囚心锁ツ
囚心锁ツ 2020-12-02 18:21

I\'m developing an application where the user presses the \"Search\" icon in the ActionBar and a SearchView is made visible at the top of the scree

17条回答
  •  囚心锁ツ
    2020-12-02 18:29

    If you want to have it iconifiedByDefault, this worked for me. setFocusable and setIconified are needed.

        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setIconifiedByDefault(true);
        searchView.setFocusable(true);
        searchView.setIconified(false);
        searchView.requestFocusFromTouch();
    

    Update: If you are Using android.support.v7.widget.SearchView the behaviour us very different. clearFocus is needed if you don't want the keyboard pop-up all the time. For some reason the menu is recreated all the time, when using appcompat.

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setIconified(false);
    searchView.clearFocus();
    

提交回复
热议问题