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
This worked for me:
menu.expandActionView();
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.
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"/>
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"
/>
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