0

I have a searchview in toolbar in a fragment but when I click on the search item I want the other menu items to hide and searchview to take the full width. But the menu items don't hide when the searchview is clicked which is very inconvenient. I tried to use searchView.setMaxWidth(Integer.MAX_VALUE); but it didn't work.

toolbar.inflateMenu(R.menu.files_menu);

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                SearchView searchView = (SearchView) item.getActionView();
                searchView.setQueryHint("Search");
                searchView.setMaxWidth(Integer.MAX_VALUE);

                searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus){
                            toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.grey));
                        } else {
                            toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.light_red));
                            toolbar.collapseActionView();
                        }

This the files_menu

<?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/search"
        android:title="search"
        android:icon="@drawable/ic_search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        />

    <item
        android:id="@+id/go"
        android:icon="@drawable/ic_baseline_go_24"
        app:showAsAction="ifRoom|collapseActionView"
        android:title="go"/>

    <item
        android:icon="@drawable/ic_baseline_more_vert_white"
        android:id="@+id/menu_overflow"
        app:showAsAction="ifRoom|collapseActionView"
        android:title="TODO">
        <menu>
    <item
        android:title="Find"
        android:id="@+id/find"
        />

        </menu>
    </item>

</menu>

I also tried to define the action layout in menu xml but that did not work as well.

 <item android:id="@+id/search"
            android:title="search"
            android:icon="@drawable/ic_search"
            android:actionLayout="@layout/searchview"
            app:showAsAction="ifRoom|collapseActionView"
            />

layout/searchview.xml is

<androidx.appcompat.widget.SearchView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxWidth="10000dp" />
a awasi
  • 71
  • 6

1 Answers1

0

I think it is duplicated question, you can check the following questions and the answers Expand Search View to use entire Action Bar (hide other things)