Thursday, March 31, 2016

Android Implementing searching

Android provides either a search Dialog that appears on top of the activity window or a search widget that one can insert in the layout. Both search dialog and search widget can deliver the user’s search query to a specific activity in the application. Some of the features readily available in the search dialog and widget include Voice search, Search suggestions and recent queries, search suggestions that matches actual result in the application data. 

Search dialog UI appears on top of the activity upon activating it. this component can also deliver search suggestions. 
Search widget is an instance of SearchView that can be placed anywhere in the app layout. 

When user executes a search from the search dialog or search widget, the system creates an Intent and stores the user query in it. The system then starts the activity that application has declared to handle the searches and delivers the Intent. Below are the items required to set up application as assisted search. 

A searchable configuration: An XML file that configures some settings for the search dialog or widget. It includes settings for features such as voice search, search suggestion and hint text for the search box. 

A searchable Activity : The Activity that receives search query. Searches the data and displays the search results. 

A search interface, provided by either 
- The search dialog -> Search dialog is hidden by default, and it appears when calling onSearchRequested. 
- A SearchView widget -> allows to put a search view anywhere in the activity. 

For creating search configuration, app needs to have searchable.xml under res/xml folder. Below is the XML for searchable configuration

?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" 
/searchable

label is the only required attribute, which should be attribute name. The label is shown only when user activate the searchable suggestions. 

A searchable Activity can be created by declaring searchable activity in the manifest file. After doing this, whenever user searches within a dialog or search widget, the system starts the application searchable activity and delivers the search query in an Intnet with ACTION_SEARCH. The intent has Query extra using which the data can be retrieved. Below is how we can declare the activity as a searchable one

application ... 
activity android:name=".SearchableActivity" 
intent-filter
action android:name="android.intent.action.SEARCH"
/intent-filter
meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"
/activity
...
/application


references:

No comments:

Post a Comment