app/src/main/java/com/smedic/tubtub/MainActivity.java
@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_main, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }

        //suggestions
        final CursorAdapter suggestionAdapter = new SimpleCursorAdapter(this,
                R.layout.dropdown_menu,
                null,
                new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1},
                new int[]{android.R.id.text1},
                0);
        final List<String> suggestions = new ArrayList<>();

        searchView.setSuggestionsAdapter(suggestionAdapter);

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

            @Override
            public boolean onSuggestionClick(int position) {
                searchView.setQuery(suggestions.get(position), false);
                searchView.clearFocus();

                Intent suggestionIntent = new Intent(Intent.ACTION_SEARCH);
                suggestionIntent.putExtra(SearchManager.QUERY, suggestions.get(position));
                handleIntent(suggestionIntent);

                return true;
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false; //if true, no new intent is started
            }

            @Override
            public boolean onQueryTextChange(String suggestion) {
                // check network connection. If not available, do not query.
                // this also disables onSuggestionClick triggering
                if (suggestion.length() > 2) { //make suggestions after 3rd letter

                    if (networkConf.isNetworkAvailable()) {

                        new JsonAsyncTask(new JsonAsyncTask.AsyncResponse() {
                            @Override
                            public void processFinish(ArrayList<String> result) {
                                suggestions.clear();
                                suggestions.addAll(result);
                                String[] columns = {
                                        BaseColumns._ID,
                                        SearchManager.SUGGEST_COLUMN_TEXT_1
                                };
                                MatrixCursor cursor = new MatrixCursor(columns);

                                for (int i = 0; i < result.size(); i++) {
                                    String[] tmp = {Integer.toString(i), result.get(i)};
                                    cursor.addRow(tmp);
                                }
                                suggestionAdapter.swapCursor(cursor);

                            }
                        }).execute(suggestion);
                        return true;
                    }
                }
                return false;
            }
        });

        return true;
    }
/**
     * Options menu in action bar
     *
     * @param menu
     * @return
     */
Options menu in action bar
@param menu
@return
Options menu in action bar @param menu @return
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }
        final CursorAdapter suggestionAdapter = new SimpleCursorAdapter(this,
                R.layout.dropdown_menu,
                null,
                new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1},
                new int[]{android.R.id.text1},
                0);
        final List<String> suggestions = new ArrayList<>();
        searchView.setSuggestionsAdapter(suggestionAdapter);
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }
            @Override
            public boolean onSuggestionClick(int position) {
                searchView.setQuery(suggestions.get(position), false);
                searchView.clearFocus();
                Intent suggestionIntent = new Intent(Intent.ACTION_SEARCH);
                suggestionIntent.putExtra(SearchManager.QUERY, suggestions.get(position));
                handleIntent(suggestionIntent);
                return true;
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false; 
            }
            @Override
            public boolean onQueryTextChange(String suggestion) {
                if (suggestion.length() > 2) { 
                    if (networkConf.isNetworkAvailable()) {
                        new JsonAsyncTask(new JsonAsyncTask.AsyncResponse() {
                            @Override
                            public void processFinish(ArrayList<String> result) {
                                suggestions.clear();
                                suggestions.addAll(result);
                                String[] columns = {
                                        BaseColumns._ID,
                                        SearchManager.SUGGEST_COLUMN_TEXT_1
                                };
                                MatrixCursor cursor = new MatrixCursor(columns);
                                for (int i = 0; i < result.size(); i++) {
                                    String[] tmp = {Integer.toString(i), result.get(i)};
                                    cursor.addRow(tmp);
                                }
                                suggestionAdapter.swapCursor(cursor);
                            }
                        }).execute(suggestion);
                        return true;
                    }
                }
                return false;
            }
        });
        return true;
    }
@ Override public boolean onCreateOptionsMenu ( Menu menu ) { getMenuInflater ( ) . inflate ( R . menu . menu_main , menu ) ; MenuItem searchItem = menu . findItem ( R . id . action_search ) ; SearchManager searchManager = ( SearchManager ) getSystemService ( Context . SEARCH_SERVICE ) ; final SearchView searchView = ( SearchView ) MenuItemCompat . getActionView ( searchItem ) ; if ( searchView != null ) { searchView . setSearchableInfo ( searchManager . getSearchableInfo ( getComponentName ( ) ) ) ; } final CursorAdapter suggestionAdapter = new SimpleCursorAdapter ( this , R . layout . dropdown_menu , null , new String [ ] { SearchManager . SUGGEST_COLUMN_TEXT_1 } , new int [ ] { android . R . id . text1 } , 0 ) ; final List < String > suggestions = new ArrayList < > ( ) ; searchView . setSuggestionsAdapter ( suggestionAdapter ) ; searchView . setOnSuggestionListener ( new SearchView . OnSuggestionListener ( ) { @ Override public boolean onSuggestionSelect ( int position ) { return false ; } @ Override public boolean onSuggestionClick ( int position ) { searchView . setQuery ( suggestions . get ( position ) , false ) ; searchView . clearFocus ( ) ; Intent suggestionIntent = new Intent ( Intent . ACTION_SEARCH ) ; suggestionIntent . putExtra ( SearchManager . QUERY , suggestions . get ( position ) ) ; handleIntent ( suggestionIntent ) ; return true ; } } ) ; searchView . setOnQueryTextListener ( new SearchView . OnQueryTextListener ( ) { @ Override public boolean onQueryTextSubmit ( String s ) { return false ; } @ Override public boolean onQueryTextChange ( String suggestion ) { if ( suggestion . length ( ) > 2 ) { if ( networkConf . isNetworkAvailable ( ) ) { new JsonAsyncTask ( new JsonAsyncTask . AsyncResponse ( ) { @ Override public void processFinish ( ArrayList < String > result ) { suggestions . clear ( ) ; suggestions . addAll ( result ) ; String [ ] columns = { BaseColumns . _ID , SearchManager . SUGGEST_COLUMN_TEXT_1 } ; MatrixCursor cursor = new MatrixCursor ( columns ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { String [ ] tmp = { Integer . toString ( i ) , result . get ( i ) } ; cursor . addRow ( tmp ) ; } suggestionAdapter . swapCursor ( cursor ) ; } } ) . execute ( suggestion ) ; return true ; } } return false ; } } ) ; return true ; }
Options menu in action bar
 @param menu
 @return
Options menu in action bar @param menu @return
// Inflate the menu; this adds items to the action bar if it is present. //suggestions //if true, no new intent is started // check network connection. If not available, do not query. // this also disables onSuggestionClick triggering //make suggestions after 3rd letter