SearchView in OptionsMenu not full width

只愿长相守 提交于 2019-11-27 07:25:23
jjung

I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons). I solved it by adding in item:

android:actionLayout="@layout/my_search_view"

and in layout/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" />

Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);
    ....

For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.

Also, just to add, I'm using the support library (v7), and so my layout file looks like this:

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxWidth="10000dp" />

And in my item, I have:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item
        ...
        myapp:actionLayout="@layout/searchview" />
...

Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).

Maybe I am late but thought it might be helpful for someone who faced the same issue as me.

final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);//set search menu as full width

in onCreateOptionsMenu of your class may solve this problem but one thing we need to understand. If you just want to show search menu (on clicking of search button) and not any other menu items in actionbar then you need to keep the search item as a first item in menu file. Here is an example

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

<!--keeping searach as first item-->
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/search_ab"
            android:title="@string/search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="ifRoom" />

        <item
            android:id="@+id/itemFilter"
            android:icon="@drawable/filter"
            android:title="@string/filter"
            app:showAsAction="ifRoom" />
    </menu>

And it will be displayed as

on clicking search it will be shown as full width

And if you keep search as second or last item in menu then it will be show like this.

and on clicking search

As crazy as it might seem, in my case problem was this line: app:actionViewClass="android.widget.SearchView" in menu.xml:

 <item
        android:id="@+id/action_search"
        app:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_search"
        android:title="@string/title"
        app:actionLayout="@layout/layout_search"
        app:showAsAction="always" />

When I removed app:actionViewClass, it fully expanded as defined in my layout_search file.

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="10000dp"
android:layout_centerInParent="true"
android:background="@color/colorAccent"
android:queryHint="Search me" />

There are so many ways to solve this issue - I'll just add the one I've used in several apps.

I have my custom SearchView (with some other customization) which extends android.support.v7.widget.SearchView and in order to stretch to full width:

@Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    super.setLayoutParams(params);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!