SearchView doesn't expand full width

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"/>

Tags:

Android