Search code examples
androidsearchview

SearchView needs to be clicked twice to open


I'm following this Android tutorial to implement the Search View. After fixing some issues, I got it to work. However, the search view needs to be clicked twice to open the editText. Any idea of what's going on?

Filter class:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.toolbar_menu_filter, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    searchView.setQueryHint(getResources().getString(R.string.search_hint));

    return super.onCreateOptionsMenu(menu);
}

menu.xml

<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"
tools:context=".FilterUI">

<item android:id="@+id/menu_item_search"
    android:title="Search"
    android:icon="@drawable/ic_search_white"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>

<item android:id="@+id/menu_item_options"
    android:icon="@drawable/ic_clear_all_white"
    app:showAsAction="ifRoom"
    android:title="@string/filter_default"/>

Manifest file

<activity android:name=".Activities.FilterUI"
        android:screenOrientation="portrait"
        android:configChanges="orientation"
        android:windowSoftInputMode="adjustNothing">

        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

    </activity>

    <activity android:name=".Activities.SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

Solution

  • Might be too late to answer but, let's get this done. Like I said, it's better to use:

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

    For more compatibility and etc. But the point is, in new AndroidX, we can't use android.support since we migrate to AndroidX and there will be only:

    app:actionViewClass="android.widget.SearchView"
    

    Available. But, let's see what documentation says for the newest APIs (AndroidX) and etc:

    Note: This class is included in the support library for compatibility with API level 7 and higher. If you're developing your app for API level 11 and higher only, you should instead use the framework android.widget.SearchView class.

    So in AndroidX, I have found:

    app:actionViewClass="androidx.appcompat.widget.SearchView"
    

    To use but you still can use: android.widget.SearchView.