Skip to content
Advertisement

SearchView icon shows twice

I’ve implemented SearchView inside my toolbar, following Android’s official instructions.

SearchView works well itself, but when I tap the search icon, it is shifted to the left instead of showing the Search Hint and Close button, although if I click this “second Search” icon, finally I get the typical search view.

Here is my xml of the toolbar:

<item android:id="@+id/search"
        android:title="@string/search_view"
    app:showAsAction="collapseActionView|ifRoom"
    android:iconTint="@android:color/white"
        android:icon="@drawable/ic_search_white_24dp"
    app:actionViewClass="android.widget.SearchView" />

And my onCreateOptionsMenu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

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

        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return true;
    }

And my manifest.xml

<activity
            android:name=".Activities.MainActivity"
            android:label="@string/addWordLabel_Act"
            android:launchMode="singleTop"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

Finally, here you have some screenshots of the problem: SearchView before tapping

SearchView after first tap, Here’s the problem

SearchView after second tap, “the normal SearchView”

And that’s all.

Note: I haven’t implemented an Activity/Fragment to handle the Search yet.

Advertisement

Answer

Finally, I got the solution. It was easy, but I’ve spent two days trying to figure it out.

Just, for compatibility issues, use

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

instead of app:actionViewClass="android.widget.SearchView"

and, of course, change

SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
       SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();

by

SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        android.support.v7.widget.SearchView searchView =
                (android.support.v7.widget.SearchView) menu.findItem(R.id.search).getActionView();
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement