16

I have the next result :

enter image description here

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.

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

3 Answers3

34

MenuItemCompat's SearchView has a property named maxWidth. The following does the trick:

searchView.setMaxWidth( Integer.MAX_VALUE );
Raymundus
  • 2,173
  • 1
  • 20
  • 35
2

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"/>
rk9992
  • 21
  • 1
0

Try removing this from your menu definition:

app:actionViewClass="android.support.v7.widget.SearchView"

In my case, it disabled loading custom-defined SearchView within search_view_layout.

OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
daneejela
  • 13,081
  • 7
  • 38
  • 50