I have the next result :
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:actionLayout="@layout/my_search_view"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
</menu>
my_search_view.xml
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp"/>
Activity
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint("Поиск");
searchView.setOnQueryTextListener(this);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchView.setIconified(false);
MenuItemCompat.expandActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
I want that X(close action) will located on right of the screen.
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.
Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.
MenuItemCompat's SearchView has a property named maxWidth. The following does the trick:
searchView.setMaxWidth( Integer.MAX_VALUE );
I was having the same problem. Searchview was not fully expanding. Here is how i solved the problem
in my styles.xml i defined a style for SearchView
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="queryBackground">@color/light_gray_3</item>
<item name="submitBackground">@color/gray</item>
<item name="closeIcon">@android:drawable/ic_menu_close_clear_cancel</item>
<item name="goIcon">@drawable/abc_ic_go_search_api_mtrl_alpha</item>
<item name="searchHintIcon">@null</item>
<item name="android:maxWidth">1000dp</item>
</style>
then in my style for toolbar i added a line to stylize the searchview
<style name="ThemeToolbar" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/gray</item>
<item name="colorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@color/gray</item>
<item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
Here is my searchview layout defined in search_view_layout.xml
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/rk.chkout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchHintIcon="@null"
android:maxWidth="10000dp" />
and the search item declaration in menu.xml
<item
android:orderInCategory="2"
android:id="@+id/action_search"
android:icon="@drawable/search_icon"
android:title="@string/search"
app:actionLayout="@layout/search_view_layout"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With