1page.title=Adding Recent Query Suggestions 2parent.title=Search 3parent.link=index.html 4@jd:body 5 6<div id="qv-wrapper"> 7<div id="qv"> 8<h2>In this document</h2> 9<ol> 10<li><a href="#TheBasics">The Basics</a></li> 11<li><a href="#RecentQueryContentProvider">Creating a Content Provider</a></li> 12<li><a href="#RecentQuerySearchableConfiguration">Modifying the Searchable 13Configuration</a></li> 14<li><a href="#SavingQueries">Saving Queries</a></li> 15<li><a href="#ClearingSuggestionData">Clearing the Suggestion Data</a></li> 16</ol> 17 18<h2>Key classes</h2> 19<ol> 20<li>{@link android.provider.SearchRecentSuggestions}</li> 21<li>{@link android.content.SearchRecentSuggestionsProvider}</li> 22</ol> 23 24<h2>See also</h2> 25<ol> 26<li><a href="searchable-config.html">Searchable Configuration</a></li> 27</ol> 28</div> 29</div> 30 31<p>When using the Android search dialog, you can provide search suggestions based on recent search 32queries. For example, if a user previously searched for "puppies," then that query appears as a 33suggestion once he or she begins typing the same query. Figure 1 shows an example of a search dialog 34with recent query suggestions.</p> 35 36<p>Before you begin, you need to implement the search dialog for basic searches in your application. 37If you haven't, see <a href="search-dialog.html">Using the Android Search Dialog</a>.</p> 38 39 40 41<h2 id="TheBasics">The Basics</h2> 42 43<div class="figure" style="width:250px"> 44<img src="{@docRoot}images/search/search-suggest-recent-queries.png" alt="" height="417" /> 45<p class="img-caption"><strong>Figure 1.</strong> Screenshot of a search dialog with recent query 46suggestions.</p> 47</div> 48 49<p>Recent query suggestions are simply saved searches. When the user selects one of 50the suggestions, your searchable Activity receives a {@link 51android.content.Intent#ACTION_SEARCH} Intent with the suggestion as the search query, which your 52searchable Activity already handles (as described in <a href="search-dialog.html">Using the Android 53Search Dialog</a>).</p> 54 55<p>To provide recent queries suggestions, you need to:</p> 56 57<ul> 58 <li>Implement a searchable Activity, <a 59href="{@docRoot}guide/topics/search/search-dialog.html">using the Android Search Dialog</a>.</li> 60 <li>Create a content provider that extends {@link 61android.content.SearchRecentSuggestionsProvider} and declare it in your application manifest.</li> 62 <li>Modify the searchable configuration with information about the content provider that 63provides search suggestions.</li> 64 <li>Save queries to your content provider each time a search is executed.</li> 65</ul> 66 67<p>Just as the Search Manager displays the search dialog, it also displays the 68search suggestions. All you need to do is provide a source from which the suggestions can be 69retrieved.</p> 70 71<p>When the Search Manager identifies that your Activity is searchable and provides search 72suggestions, the following procedure takes place as soon as the user types into the search 73dialog:</p> 74 75<ol> 76 <li>Search Manager takes the search query text (whatever has been typed so far) and performs a 77query to the content provider that contains your suggestions.</li> 78 <li>Your content provider returns a {@link android.database.Cursor} that points to all 79suggestions that match the search query text.</li> 80 <li>Search Manager displays the list of suggestions provided by the Cursor.</li> 81</ol> 82 83<p>Once the recent query suggestions are displayed, the following might happen:</p> 84 85<ul> 86 <li>If the user types another key, or changes the query in any way, the aforementioned steps are 87repeated and the suggestion list is updated.</li> 88 <li>If the user executes the search, the suggestions are ignored and the search is delivered 89to your searchable Activity using the normal {@link android.content.Intent#ACTION_SEARCH} 90Intent.</li> 91 <li>If the user selects a suggestion, an 92{@link android.content.Intent#ACTION_SEARCH} Intent is delivered to your searchable Activity using 93the suggested text as the query.</li> 94</ul> 95 96<p>The {@link android.content.SearchRecentSuggestionsProvider} class that 97you extend for your content provider automatically does the work described above, so there's 98actually very little code to write.</p> 99 100 101 102<h2 id="RecentQueryContentProvider">Creating a Content Provider</h2> 103 104<p>The content provider that you need for recent query suggestions must be an implementation 105of {@link android.content.SearchRecentSuggestionsProvider}. This class does practically everything 106for you. All you have to do is write a class constructor that executes one line of code.</p> 107 108<p>For example, here's a complete implementation of a content provider for recent query 109suggestions:</p> 110 111<pre> 112public class MySuggestionProvider extends SearchRecentSuggestionsProvider { 113 public final static String AUTHORITY = "com.example.MySuggestionProvider"; 114 public final static int MODE = DATABASE_MODE_QUERIES; 115 116 public MySuggestionProvider() { 117 setupSuggestions(AUTHORITY, MODE); 118 } 119} 120</pre> 121 122<p>The call to {@link android.content.SearchRecentSuggestionsProvider#setupSuggestions(String,int) 123setupSuggestions()} passes the name of the search authority and a 124database mode. The search authority can be any unique string, but the best practice is to use a 125fully qualified name for your content provider 126(package name followed by the provider's class name; for example, 127"com.example.MySuggestionProvider"). The database mode must include {@link 128android.content.SearchRecentSuggestionsProvider#DATABASE_MODE_QUERIES} and can optionally include 129{@link 130android.content.SearchRecentSuggestionsProvider#DATABASE_MODE_2LINES}, which adds another column 131to the suggestions table that allows you to provide a second line of text with each suggestion. For 132example, if you want to provide two lines in each suggestion:</p> 133 134<pre> 135public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES; 136</pre> 137 138<p>Now declare the content provider in your application manifest with the same authority 139string used in your {@link android.content.SearchRecentSuggestionsProvider} class (and in the 140searchable configuration). For example:</p> 141 142<pre> 143<application> 144 <provider android:name=".MySuggestionProvider" 145 android:authorities="com.example.MySuggestionProvider" /> 146 ... 147</application> 148</pre> 149 150 151 152<h2 id="RecentQuerySearchableConfiguration">Modifying the Searchable Configuration</h2> 153 154<p>To configure your search dialog to use your suggestions provider, you need to add 155the {@code android:searchSuggestAuthority} and {@code android:searchSuggestSelection} attributes to 156the {@code <searchable>} element in your searchable configuration file. For example:</p> 157 158<pre> 159<?xml version="1.0" encoding="utf-8"?> 160<searchable xmlns:android="http://schemas.android.com/apk/res/android" 161 android:label="@string/app_label" 162 android:hint="@string/search_hint" 163 <b>android:searchSuggestAuthority="com.example.MySuggestionProvider" 164 android:searchSuggestSelection=" ?"</b> > 165</searchable> 166</pre> 167 168<p>The value for {@code android:searchSuggestAuthority} should be a fully qualified name for 169your content provider that exactly matches the authority used in the content provider (the {@code 170AUTHORITY} string in the above example). 171</p> 172 173<p>The value for {@code android:searchSuggestSelection} must be a single question mark, preceded by 174a space ({@code " ?"}), which is simply a placeholder for the SQLite selection argument (which is 175automatically replaced by the query text entered by the user).</p> 176 177 178 179<h2 id="SavingQueries">Saving Queries</h2> 180 181<p>To populate your collection of recent queries, add each query 182received by your searchable Activity to your {@link 183android.content.SearchRecentSuggestionsProvider}. To do this, create an instance of {@link 184android.provider.SearchRecentSuggestions} and call {@link 185android.provider.SearchRecentSuggestions#saveRecentQuery(String,String) saveRecentQuery()} each time 186your searchable Activity receives a query. For example, here's how you can save the query during 187your Activity's {@link android.app.Activity#onCreate(Bundle) onCreate()} method:</p> 188 189<pre> 190@Override 191public void onCreate(Bundle savedInstanceState) { 192 super.onCreate(savedInstanceState); 193 setContentView(R.layout.main); 194 195 Intent Intent = getIntent(); 196 197 if (Intent.ACTION_SEARCH.equals(Intent .getAction())) { 198 String query = Intent .getStringExtra(SearchManager.QUERY); 199 SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 200 MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE); 201 suggestions.saveRecentQuery(query, null); 202 } 203} 204</pre> 205 206<p>The {@link android.content.SearchRecentSuggestionsProvider} constructor requires the 207same authority and database mode declared by your content provider.</p> 208 209<p>The {@link android.provider.SearchRecentSuggestions#saveRecentQuery(String,String) 210saveRecentQuery()} method takes 211the search query string as the first parameter and, optionally, a second string to include as the 212second line of the suggestion (or null). The second parameter is only used if you've enabled 213two-line mode for the search suggestions with {@link 214android.content.SearchRecentSuggestionsProvider#DATABASE_MODE_2LINES}. If you have enabled 215two-line mode, then the query text is also matched against this second line when the Search Manager 216looks for matching suggestions.</p> 217 218 219 220<h2 id="ClearingSuggestionData">Clearing the Suggestion Data</h2> 221 222<p>To protect the user's privacy, you should always provide a way for the user to clear the recent 223query suggestions. To clear the query history, call {@link 224android.provider.SearchRecentSuggestions#clearHistory()}. For example:</p> 225 226<pre> 227SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 228 HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE); 229suggestions.clearHistory(); 230</pre> 231 232<p>Execute this from your choice of a "Clear Search History" menu item, 233preference item, or button. You should also provide a confirmation dialog to 234verify that the user wants to delete their search history.</p> 235 236