Search code examples
androidsearchdrop-down-menusearch-box

Open a search suggestions to quick search box


When I open the search field the search suggestions box is not opened. It stay closed as long as the search field is empty. When I start typing, only then, the search suggestions box is opened. How can I open a search suggestions box immodestly when I expose the search field?

YouTube example: YouTube example


Solution

  • searchView.setSuggestionsAdapter() is the answer
    

    for example for List < String> items:

    searchView.setSuggestionsAdapter(new SearchableCursorAdapter(this, getCursorByQuery(items, ""), items));
    
    /**
     * CursorAdapter with the search able list
     *   compiler bug in javac 1.5.0_07-164, we need to implement Filter able to make compilation work
     */
    public class SearchableCursorAdapter extends CursorAdapter implements Filterable {
        List<String> mItems;
    
        public SearchableCursorAdapter(Context context, Cursor cursor, List<String> items) {
            super(context, cursor);
            mItems = items;
        }
    
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.searchable_item, parent, false);                 
            TextView textView = (TextView) view.findViewById(R.id.searchable_optianl_text_view);           
            textView.setText(cursor.getString(COLUMN_DISPLAY_NAME));
            return view;
        }
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView textView = (TextView) view.findViewById(R.id.searchable_optianl_text_view);
            textView.setText(cursor.getString(COLUMN_DISPLAY_NAME));
        }
    
        @Override
        public String convertToString(Cursor cursor) {
            return cursor.getString(COLUMN_DISPLAY_NAME);
        }
    
        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            FilterQueryProvider filter = getFilterQueryProvider();
            if (filter != null) {
                return filter.runQuery(constraint);
            }
    
            return getCursorByQuery(mItems, (String)constraint);
        }
    
        private static final int COLUMN_DISPLAY_NAME = 1;
    }
    /**
     * create a cursor with the list to display
     */
    Cursor getCursorByQuery(List<String> fullItems, String constraint) {
        List<String> items = new ArrayList<String>();
        for (String item : fullItems) {
            if (item.toLowerCase(Locale.US).startsWith(((String) constraint).toLowerCase(Locale.US))) {
                items.add(item);
            }
        }
        // Load data from list to cursor
        String[] columns = new String[] { "_id", "text" };
        Object[] temp = new Object[] { 0, "default" };
    
        MatrixCursor cursor = new MatrixCursor(columns);
    
        for(int i = 0; i < items.size(); i++) {
            temp[0] = i;
            temp[1] = items.get(i);
    
            cursor.addRow(temp);
        }
        return cursor;
    }
    

    don't forget at AndroidMenifest:

    <!-- enable the search dialog to send searches to SearchableActivity -->
    <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>           
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    

    searchable.xml

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

    menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@+id/menu_search"
        android:title="@string/Search"
        android:icon="@drawable/ic_action_search" 
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />
    
    </menu>