Expand and give focus to SearchView automatically

后端 未结 17 2359
囚心锁ツ
囚心锁ツ 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:26

    If you're using it in layout, you can call

    mSearchView.onActionViewExpanded()

    0 讨论(0)
  • 2020-12-02 18:26

     <item
            android:id="@+id/search"
            android:icon="@drawable/ic_search"
            android:title="Search"
            app:actionViewClass="androidx.appcompat.widget.SearchView"
            app:showAsAction="always"/>
    
     public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.search_menu, menu);
            // Associate searchable configuration with the SearchView
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView = (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setQueryHint("Search the customer...");
            searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
            searchView.setMaxWidth(Integer.MAX_VALUE);
            searchView.setIconifiedByDefault(false);
            searchView.requestFocus();
    
        }

    <pre>
    
     <item
            android:id="@+id/search"
            android:icon="@drawable/ic_search"
            android:title="Search"
            app:actionViewClass="androidx.appcompat.widget.SearchView"
            app:showAsAction="always"/>
    
     public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.search_menu, menu);
            // Associate searchable configuration with the SearchView
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView = (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setQueryHint("Search the customer...");
            searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
            searchView.setMaxWidth(Integer.MAX_VALUE);
            searchView.setIconifiedByDefault(false);
            searchView.requestFocus();
    
        }
    
    </pre>

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-02 18:31

    If you are using inside an activity you need to use

    view.onActionViewExpanded();
    

    if you are using inside menu options you need to use

    MenuItem.expandActionView();
    

    Note: it works only for SearchView

    these two situations are worked for me.

    0 讨论(0)
  • 2020-12-02 18:31

    You can use the SearchView#setIconified() method on a SearchView handle in your Java code. More here:

    http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)

    You can also make use of SearchView#setIconifiedByDefault(). More info here:

    http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)

    0 讨论(0)
  • 2020-12-02 18:32

    Call expandActionView() on the menuItem.

    menuItem.expandActionView()
    
    0 讨论(0)
提交回复
热议问题