Introduction

Search is a very common feature in any type of application whether the application is a web application or console application. Now let us talk about Android and mobile applications. Android applications also have search features of their own.
Android enabled devices to have a very robust feature of search queries within the devices or searching with the web so that it is combined in the device itself. A user can search for something in the Android device and user settings can be saved on that devices and when the user searches in the future again for something and the matched characters would show and the previous search words appear in the dropdown selection list.
Let us see an example in which we have a dictionary application. When we begin the search in the dictionary search provider tab, there are suggestions based on recent searches or after five or four search results we have seen that what happens is when the user type a word again into that space then the previously searched keyword is shown below the typing space as in the form of a list.
searchview

Features of Search Widgets

Creating a Search Interface

In an Android application, a search interface could be created using two methods, namely by search widgets or with a search dialog box. Note: However, the search widget is available only in Android 3.0 (API Level 11) and higher.
However, a user's query must be saved in the locale using an Intent so as to provide an efficient search for it. To enhance this use the following procedure:

Creating a Searchable Configuration

For creating a searchable configuration we must use the code for a search widget or a dialog in the XML layout file. It configures certain UI aspects of the search dialog.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="@string/app_label"
  4. android:hint="@string/search_hint" >
  5. </searchable>
    The android: label attribute is the only required attribute. However, it is pointing towards a string resource. Its attributes must be defined in the string.xml file.
    The <searchable> element accepts several other attributes. However, you don't need most attributes until one must attach the features on the search suggestions and voice search.

    Creating the Searchable Activity

    activity_main.xml
    1. <application ... >
    2. <activity android:name=".SearchableActivity" >
    3. <intent-filter>
    4. 1<action android:name="android.intent.action.SEARCH" />
    5. </intent-filter>
    6. <meta-data android:name="android.app.searchable"
    7. android:resource="@xml/searchable"/>
    8. </activity>
    9. ...
    10. </application>
    Note: The <intent-filter> does not need a <category> with the DEFAULT value because the system delivers the ACTION_SEARCH intent explicitly to searchable activity.

    Performing a search

    This includes the following three steps basically:

    Receiving the query

    When a user executes a search from the search dialog or widgets then the system starts your searchable activity and sends it an ACTION _SEARCH intent. An intent can carry the information. Here it carries the search result.
    1. import android.app.Activity;
    2. import android.os.Bundle;
    3. import android.view.Menu;
    4. import android.view.MenuItem;
    5. public class RecentActivity extends Activity {
    6. @Override
    7. protected void onCreate(Bundle savedInstanceState) {
    8. super.onCreate(savedInstanceState);
    9. setContentView(R.layout.activity_recent);
    10. // Get the intent, verify the action and get the query
    11. Intent intent = getIntent();
    12. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    13. String query = intent.getStringExtra(SearchManager.QUERY);
    14. doMySearch(query);
    15. }
    16. @Override
    17. public boolean onCreateOptionsMenu(Menu menu) {
    18. // Inflate the menu; this adds items to the action bar if it is present.
    19. getMenuInflater().inflate(R.menu.recent, menu);
    20. return true;
    21. }
    22. @Override
    23. public boolean onOptionsItemSelected(MenuItem item) {
    24. // Handle action bar item clicks here. The action bar will
    25. // automatically handle clicks on the Home/Up button, so long
    26. // as you specify a parent activity in AndroidManifest.xml.
    27. int id = item.getItemId();
    28. if (id == R.id.action_settings) {
    29. return true;
    30. }
    31. return super.onOptionsItemSelected(item);
    32. }
    33. }

    Searching Data using Search Widget

    The processing and storing of the data in an Android application using the database SQLite is done uniquely. The SearchView widgets are available in Android 3.0 and higher. If we are developing for Android version 3.0 and higher then we must insert search widgets as an action view in the ActionBar.

    Configuring the search widgets

    After you've created a searchable configuration and a searchable activity as discussed above you need to enable searching. For example, if you're using a SearchView as an action view in the Action Bar, you should enable the widget during the onCreateOptionsMenu() callback:
    1. @Override
    2. public boolean onCreateOptionsMenu(Menu menu) {
    3. // Inflate the options menu from XML
    4. MenuInflater inflater = getMenuInflater();
    5. inflater.inflate(R.menu.options_menu, menu);
    6. // Get the SearchView and set the searchable configuration
    7. SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    8. SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    9. // Assumes current activity is the searchable activity
    10. searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    11. searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
    12. return true;
    13. }

      Other features of Search Widgets

      Summary

      This article illustrates the basics of search widgets and SearchView objects and the various aspects of them. This article has clearly explained how to configure the search widgets and search the data using it.