Search code examples
androidinserticonstoolbarsearchview

how to insert search view in toolbar?


A few lines of the logcat

11-26 01:14:04.752 16902-16902/com.androidbelieve.drawerwithswipetabs W/dalvikvm: VFY: unable to resolve         virtual method 3268: Landroid/support/v4/app/Fragment;.performOptionsMenuClosed     (Landroid/view/Menu;)V
11-26 01:14:04.752 16902-16902/com.androidbelieve.drawerwithswipetabs D/dalvikvm: VFY: replacing opcode 0x6f at 0x04ac
11-26 01:14:07.315 16902-16902/com.androidbelieve.drawerwithswipetabs D/AbsListView: onDetachedFromWindow
11-26 01:14:08.896 16902-16902/com.androidbelieve.drawerwithswipetabs I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
11-26 01:14:08.936 16902-16902/com.androidbelieve.drawerwithswipetabs D/AbsListView: Get MotionRecognitionManager
11-26 01:14:08.936 16902-16902/com.androidbelieve.drawerwithswipetabs I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.

2-Screenshot of the app on my phone.

screenshot of the app on my phoen


Solution

  • XML

    I - Create a searchable configuration in a searchable.xml saved in res/xml directory.

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

    II - Declare your searchable activity in the AndroidManifest.xml file.

    <activity android:name=".SearchActivity">  
       <intent-filter>
           <action android:name="android.intent.action.SEARCH" />
       </intent-filter>
       <meta-data android:name="android.app.searchable"
                  android:resource="@xml/searchable"/>
    </activity>
    

    III- Declare your SearchView inside any layout.xml

    <android.support.v7.widget.SearchView  
       android:id="@+id/search"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
    

    STYLING

    I. Declare custom styles in your styles.xml file.

    <style name="SearchViewTheme" >  
       <item name="colorControlActivated">@color/amber500</item>
       <item name="colorControlNormal">@color/green500</item>
    </style>
    

    II. Apply this style

    <android.support.v7.widget.SearchView  
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:searchIcon="@drawable/ic_library"
        app:theme="@style/SearchViewTheme"/>
    

    JAVA

    I- Setting up the SearchView in the OnCreate method

    SearchView searchView = (SearchView) findViewById(R.id.search);  
    // Sets searchable configuration defined in searchable.xml for this SearchView
    SearchManager searchManager =  
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    

    II- Receiving search query

    SearchView searchView = (SearchView) findViewById(R.id.search);  
    // Sets searchable configuration defined in searchable.xml for this SearchView
    SearchManager searchManager =  
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    

    III- Listening to the user inputs (getting inputs)

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
    @Override
    public boolean onQueryTextSubmit(String query) {
        searchFor(query);
        return true;
        }
    
    @Override
    public boolean onQueryTextChange(String query) {
        filterSearchFor(query);
        return true;
       }
    });