In android, we can provide suggestions to user on serach. The custom suggestions are the suggestions that you provide the application rather than the suggestions based on user history.
So the data for the suggestions can be either from data of your application or from network. Here we are going to add the data from our network based on the input text in the search box. We are performing search using search dailog in action bar. Set up search in menu of your activity as:
1 2 3 4 5 6 7 8 9 10 |
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/search" app:actionViewClass="android.support.v7.widget.SearchView" android:icon="@drawable/ic_search" app:showAsAction="collapseActionView|always" android:title="@string/Search"/> </menu> |
Now set the searchable.xml file which we will store in res/xml folder
1 2 3 4 5 6 7 8 9 10 11 |
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_name" android:searchMode="queryRewriteFromText" android:searchSettingsDescription="string resource" android:searchSuggestThreshold="2" android:textColorHint="@color/white" android:searchSuggestSelection = " ?" android:searchSuggestIntentAction="android.intent.action.SEARCH"> </searchable> |
Now inflate the menu and add suggestions to your search dailog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.home, menu); MenuItem searchItem = menu.findItem(R.id.search); final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); final List<String> suggestions = new ArrayList<>(); final CursorAdapter suggestionAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, new String[]{SearchManager.SUGGEST_COLUMN_TEXT_1}, new int[]{android.R.id.text1}, 0); 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), true); searchView.clearFocus(); return true; } }); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; //false if you want implicit call to searchable activity // or true if you want to handle submit yourself } @Override public boolean onQueryTextChange(String newText) { // Hit the network and take all the suggestions and store them in List 'suggestions' String[] columns = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_INTENT_DATA, }; MatrixCursor cursor = new MatrixCursor(columns); for (int i = 0; i < suggestions.size(); i++) { String[] tmp = {Integer.toString(i),suggestions.get(i),suggestions.get(i)}; cursor.addRow(tmp); } suggestionAdapter.swapCursor(cursor); return true; } }); return super.onCreateOptionsMenu(menu); } |
And by this you can get your search suggestion from network.