Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2015 14:26
Show Gist options
  • Save StefMa/86964e3a11ce52662be9 to your computer and use it in GitHub Desktop.
Save StefMa/86964e3a11ce52662be9 to your computer and use it in GitHub Desktop.
SearchView suggestions over network

SearchView suggestions over network

What is it?

This is a simple how to make a network request with the SearchView implemented in a Toolbar.


The documentation for a Search Interface says that you can't make a a request over the network. You can only make a SearchView via recent searches or over a ContentProvider. I want to demonstrate how to make network request with the searchview.

How to use?

First thing is to setup the SearchView despripted here: Creating a Search Interface

Your onCreateOptionsMenu(Menu menu) should look like this:

SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
            (SearchView) menu.findItem(;
ComponentName componentName = new ComponentName(this, SearchableActivity.class);

To setup suggestions you must put an adapter to the SearchView:

final CursorAdapter suggestionAdapter = new SimpleCursorAdapter(
            new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1},
            new int[]{},

For suggestions based on the input you must set a onQueryTextListener.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        public boolean onQueryTextSubmit(String query) {
            return false;

        public boolean onQueryTextChange(String newText) {
            // Put your magic here
            return false;

Now it's time for magic. On your onQueryTextChange(String newText) you can call your AsyncTask* and set the result (from the network) to your adapter (which was set to your SearchView).

new Task(new Task.Listener() {
                public void onReady(List<String> results) {
                    String[] columns = {

                    MatrixCursor cursor = new MatrixCursor(columns);

                    for (int i = 0; i < results.size(); i++) {
                        String[] tmp = {Integer.toString(i),  results.get(i), "COLUMNT_INTENT_DATA"};


*To make your network request you need an AsyncTask (which is described here: AsyncTask)

Copy link

Sooooo helpful!!! Many thanks @StefMa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment