Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView doesn't expand full width

Tags:

android

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.

like image 878
Sergey Shustikov Avatar asked Jul 16 '15 13:07

Sergey Shustikov


People also ask

How do I expand SearchView?

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.

How do I use search view?

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.


2 Answers

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

searchView.setMaxWidth( Integer.MAX_VALUE );
like image 151
Raymundus Avatar answered Oct 22 '22 07:10

Raymundus


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"/>
like image 40
rk9992 Avatar answered Oct 22 '22 07:10

rk9992