How to use SearchView in Toolbar Android How to use SearchView in Toolbar Android android android

How to use SearchView in Toolbar Android


You have to use Appcompat library for that. Which is used like below:

dashboard.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">    <item        android:id="@+id/action_search"        android:icon="@android:drawable/ic_menu_search"        app:showAsAction="always|collapseActionView"        app:actionViewClass="androidx.appcompat.widget.SearchView"        android:title="Search"/></menu>

Activity file (in Java):

public boolean onCreateOptionsMenu(Menu menu) {    MenuInflater menuInflater = getMenuInflater();    menuInflater.inflate(R.menu.dashboard, menu);     MenuItem searchItem = menu.findItem(R.id.action_search);    SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);    SearchView searchView = null;    if (searchItem != null) {        searchView = (SearchView) searchItem.getActionView();    }    if (searchView != null) {        searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));    }        return super.onCreateOptionsMenu(menu);}

Activity file (in Kotlin):

override fun onCreateOptionsMenu(menu: Menu?): Boolean {    menuInflater.inflate(R.menu.menu_search, menu)    val searchItem: MenuItem? = menu?.findItem(R.id.action_search)    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager    val searchView: SearchView? = searchItem?.actionView as SearchView    searchView?.setSearchableInfo(searchManager.getSearchableInfo(componentName))    return super.onCreateOptionsMenu(menu)}

manifest file:

<meta-data       android:name="android.app.default_searchable"       android:value="com.apkgetter.SearchResultsActivity" />         <activity            android:name="com.apkgetter.SearchResultsActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />            </intent-filter>            <meta-data                android:name="android.app.searchable"                android:resource="@xml/searchable" />        </activity>

searchable xml file:

<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"    android:hint="@string/search_hint"    android:label="@string/app_name" />

And at last, your SearchResultsActivity class code. for showing result of your search.


If you would like to setup the search facility inside your Fragment, just add these few lines:

Step 1 - Add the search field to you toolbar:

<item    android:id="@+id/action_search"    android:icon="@android:drawable/ic_menu_search"    app:showAsAction="always|collapseActionView"    app:actionViewClass="android.support.v7.widget.SearchView"    android:title="Search"/>

Step 2 - Add the logic to your onCreateOptionsMenu()

import android.support.v7.widget.SearchView; // not the default !@Overridepublic boolean onCreateOptionsMenu( Menu menu) {    getMenuInflater().inflate( R.menu.main, menu);    MenuItem myActionMenuItem = menu.findItem( R.id.action_search);    searchView = (SearchView) myActionMenuItem.getActionView();    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {        @Override        public boolean onQueryTextSubmit(String query) {            // Toast like print            UserFeedback.show( "SearchOnQueryTextSubmit: " + query);            if( ! searchView.isIconified()) {                searchView.setIconified(true);            }            myActionMenuItem.collapseActionView();            return false;        }        @Override        public boolean onQueryTextChange(String s) {            // UserFeedback.show( "SearchOnQueryTextChanged: " + s);            return false;        }    });    return true;}


If you want to add it directly in the toolbar.

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.AppBarLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <android.support.v7.widget.Toolbar        android:id="@+id/app_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <SearchView            android:id="@+id/searchView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:iconifiedByDefault="false"            android:queryHint="Search"            android:layout_centerHorizontal="true" />    </android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout>