Home Forum
Welcome, Guest
Username Password: Remember me

Searching in android
(1 viewing) (1) Guest
Discussion related to creating Android software
  • Page:
  • 1

TOPIC: Searching in android

Searching in android 2 years, 9 months ago #167

Adding the search capability to applications using the following as instructions.
developer.android.com/reference/android/app/SearchManager.html

The following was done to allow searching

Added a button that when clicked calls "onSearchRequested"
Also added "setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);"
so that handsets with keyboards can just start typing to search
Added a search result activity with the following code
(doSearchQuery not included to avoid verbosity)
 
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) { doSearchWithIntent(queryIntent);
} }
 
private void doSearchWithIntent(final Intent queryIntent) {
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
doSearchWithQuery(queryString); }
 


Add a searchable.xml file in the xml folder (This controls aspects of the search + UI)
 
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchIconAsBadge"
android:searchButtonText="@string/search" android:inputType="text">
</searchable>
 

Add "Glue" xml to point the entire application to the search activity
<meta-data android:name="android.app.default_searchable" android:value=".search.SearchResults" />

The define the search activity in the manifest include the search intent and link to the searchable xml file
 
<activity android:name=".search.SearchResults"
android:launchMode="singleTop"
android:label="Search">
 
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
</activity>
 
  • mmoore
  • OFFLINE
  • Examiner
  • Posts: 22
  • Karma: 1
Last Edit: 2 years, 9 months ago by mmoore. Reason: Trying to tidy up
  • Page:
  • 1
Time to create page: 0.34 seconds
Copyright © 2012 Android Academy. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
 
Glossary
We have 27 guests online