Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 556
Dani M Avatar asked Feb 25 '16 19:02

Dani M


People also ask

How to display a searchview and listview using its different attributes?

In this step we open an xml file and add the code for displaying a SearchView and ListView by using its different attributes. In this step we open MainActivity and add the code to initiate SearchView and ListView. In this we create an Animal name list and then set the adapter to fill the data in ListView.

How to make the searchview always visible in action bar?

Important Note: When a SearchView is used in an Action Bar as an action view for collapsible menu item then it needs to be set to iconified by default using setIconfiedByDefault (true) function. If you want the search field to always be visible, then call setIconifiedByDefault (false). true is the default value for this function.

How to add searchview to Android Studio project?

Below you can download complete SearchView Android Studio project code, see final output and step by step explanation of example: Step 2: Open res -> layout ->activity_main. xml (or) main.xml and add following code: In this step we open an xml file and add the code for displaying a SearchView and ListView by using its different attributes.

How to set the default state of the search field in searchview?

3. iconifiedByDefault: This attribute of searchview is used to set the default or resting state of the search field. You can set a Boolean value for this attribute and default value is true. True value indicates you can iconifies or expands the search view. Below we set the false value for this attribute.


1 Answers

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.

like image 90
ʍѳђઽ૯ท Avatar answered Sep 19 '22 14:09

ʍѳђઽ૯ท