Search code examples
androidandroid-fragmentsmaterial-designandroid-toolbarsearchview

SearchView is collapsable even after "app:showAsAction="always"


I've got a simple fragment toolbar:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbarDriver"
        app:menu="@menu/menu_toolbar_driver_list"
        app:navigationIcon="@drawable/baseline_menu_24"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:collapseIcon="@drawable/baseline_menu_24"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
    android:id="@+id/action_searchDrivers"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search Drivers..."
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="always" />

</menu>

Code:

mtSearchDrivers.setIconified(false);

Which looks like this:

deafult

As you can see, on the right side there is the "x" button which should only be used to remove the searchviewtext but if no text is entered like in this case and the user clicks on "x", the searchview collapses even when app:showAsAction="always" is set.. What can the issue be?

jl


Solution

  • I solved the issue:

    I just put the searchview as a child of my toolbar, rather than an own menuitem.