Updated 15 December 2016
Searching in android is a very important feature and mostly present in every application nowadays. There are two ways to provide the search one is search widget and the other is or a search dailog.
We can also show the suggestions to user in the search view. The suggestions can be the recent search items or the custom suggestions you added in your application.
Here we will implement a search dialog with the recently searched terms as sugessions. Firstly we have to implement a searchable configuration. Here is your searchable.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_label" android:hint="@string/search_hint" android:searchMode="queryRewriteFromText" android:searchSuggestThreshold="2" android:searchSuggestAuthority="<YOUR PACKAGE NAME>.MySuggestionProvider" android:searchSuggestSelection = " ?" android:searchSuggestIntentAction="android.intent.action.SEARCH" > </searchable> |
Place this xml at res/xml folder of your application.
Now make a search activity that will be called internally when a search is performed. The activity should be single top. Lets call it SearchActivity
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 |
public class SearchActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleSearch(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); handleSearch(); } public void handleSearch(){ if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { searchQuery = getIntent().getStringExtra(SearchManager.QUERY).trim(); SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE); suggestions.saveRecentQuery(searchQuery, null); } callServerToGetResult(); } public void callServerToGetResult(){ //your server request and handlimg response } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.search); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) MenuItem.getActionView(searchItem); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); } } } |
To make the system know which one of your activity is search activity. So define your search activity within th manifest as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<activity android:name=".SearchActivity" android:label="@string/app_name" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> <provider android:name=".MySuggestionProvider" android:authorities="<Your package name>.MySuggestionProvider" /> |
Now to store your recent searches for suggestion make a authority provider as we have already defined in Searchable.xml i.e MySuggestionProvider
1 2 3 4 5 6 7 8 |
public class MySuggestionProvider extends SearchRecentSuggestionsProvider { public final static String AUTHORITY = MySuggestionProvider.class.getName(); public final static int MODE = DATABASE_MODE_QUERIES; public MySuggestionProvider() { setupSuggestions(AUTHORITY, MODE); } } |
You can also use DATYABASE_MODE_2LINES, it is helpful in showing a descriptive text below your suggestion text. The search is matched with both the suggestion text as well as descriptive text.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.