Search code examples
androidsearchview

How to display list result - searchView


I want to implement the best way to implement a search. In my Activity I have two tabs with a list results and I have added a searchView in the toolbar where the user can type what he is looking for. is there a way to display a list result above my activity whit the results as in whatsapp?

enter image description here

Below is the code I have but its basically a searchView.

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_subs, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return true;
    }

Solution

  • I have implemented it using this dependence MaterialSearchView

    Step 1

    Add the dependencies to your gradle

    compile 'com.miguelcatalan:materialsearchview:1.4.0'
    

    Step 2

    Add MaterialSearchView to your layout file along with the Toolbar

    <!— Must be last for right layering display —>
        <FrameLayout
            android:id="@+id/toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/theme_primary" />
    
            <com.miguelcatalan.materialsearchview.MaterialSearchView
                android:id="@+id/search_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </FrameLayout>
    

    Step 3

    Add the search item into the menu file:

    <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_action_action_search"
            android:orderInCategory="100"
            android:title="@string/abc_search_hint"
            app:showAsAction="always" />
    

    Step 4

    Add define it in the onCreateOptionsMenu:

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
    
            MenuItem item = menu.findItem(R.id.action_search);
            searchView.setMenuItem(item);
    
            return true;
        }
    

    Step 5

    Set OnItemClickListener

     MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
    searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //Do some magic 
                }
            });