Using search bar in toolbar

6.2k Views Asked by At

I want to use search view in my android app. For the purpose, I add searchview in my app_bar :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColorDark">
    <SearchView
        android:id="@+id/mySearchView"
        android:icon="@drawable/ic_magnify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:iconifiedByDefault="true"
        android:queryHint="Search from Qur'an"/>

</android.support.v7.widget.Toolbar>

Now, the methods which implements this is :

 search = (SearchView) findViewById(R.id.mySearchView);
        search.setQueryHint("SearchView");

        //*** setOnQueryTextFocusChangeListener ***
        search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub

                Toast.makeText(getBaseContext(), String.valueOf(hasFocus) +" I am in has focus",
                        Toast.LENGTH_SHORT).show();
            }
        });

        //*** setOnQueryTextListener ***
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // TODO Auto-generated method stub

                startActivity(new Intent(getApplicationContext(),SearchResultsActivity.class));

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // TODO Auto-generated method stub

                    Toast.makeText(getBaseContext(), newText,
                Toast.LENGTH_SHORT).show();
                return false;
            }
        });

Now, I want add dropdown Like this :

drop down

Now, on clicking any of these items, a new activity should be started. How can I achieve this. Also, I am new to android development, so please suggest me any other method to do it.

Edit 1:

Using setOnSuggestionListener doesn't work here:

search.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int i) {
                return true;
            }

            @Override
            public boolean onSuggestionClick(int i) {
                //your intent
                startActivity(new Intent(getBaseContext(),SearchResultsActivity.class));
                return true;
            }
        });
1

There are 1 best solutions below

3
On

just implement on suggestionListener
the below code is the short example may this will help you

 searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
                    @Override
                    public boolean onSuggestionSelect(int i) {
                        return true;
                    }

                    @Override
                    public boolean onSuggestionClick(int i) {
                       //your intent
                        return true;
                    }
                });