Expand and give focus to SearchView automatically

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

    This worked for me:

    menu.expandActionView();
    
    0 讨论(0)
  • 2020-12-02 18:49

    To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..)). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

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

    I am using android.widget Searchview and iconified by default.Below code in xml helped me make it expand and autofocus on search view,when clicked:

    <SearchView
                    android:id="@+id/searchView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:iconifiedByDefault="true"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:queryHint="Search"/>
    
    0 讨论(0)
  • 2020-12-02 18:50

    You can also call to expandActionView() method in order to force it:

    @Override
    public boolean onCreateOptionsMenu( Menu menu )
    {
        super.onCreateOptionsMenu( menu );
    
        MenuItem searchMenuItem = menu.findItem( R.id.mi_search ); // get my MenuItem with placeholder submenu
        searchMenuItem.expandActionView(); // Expand the search menu item in order to show by default the query
    
        return true;
    }
    

    Search item in the Action Bar layout:

    <item
            android:id="@+id/mi_search"
            android:icon="@drawable/abs__ic_search_api_holo_light"
            android:title="@string/search"
            android:showAsAction="ifRoom|collapseActionView"
            android:actionViewClass="com.actionbarsherlock.widget.SearchView"
            />
    
    0 讨论(0)
  • 2020-12-02 18:53

    MenuItemCompat's SearchView has a property named maxWidth.

    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setMaxWidth(xxx);
    

    use screen width instead of xxx offcourse

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