Basics of SearchView in Android : Part 2

Introduction

In the previous article we learned how to make a SearchView element in the activity or on the search bar located above at the top of the activity. Let us use the same example as we encountered in the previous article of mine.

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 types a word again into that space then the previous searched keyword is shown below the typing space as in the form of a list.

Using the Search Dialog

The Search Dialog provides a better way to create a search box at the top of the activity as previously said. The search dialog can do the same task as a SearchView was doing in our previous article we saw and analyzed the properties of the SearchView.

The search dialog is always hidden by default, until the user activates it. By invoking the method onSearchRequestd() we can activate our application. However this method works only after we enable the search dialog to the activity. Generally the way we follow to enable the search dialog in the activity it must receive the search queries and save them.

To declare the searchable activity for an activity search dialog add a <meta-data> element inside the respective element. We must create a class having name and attribute "android:name" and attribute value "android.app.default_searchable".

Let us create an activity.

Code

  1. <application ... >  
  2.    <!-- this is the searchable activity; it performs searches -->  
  3.    <activity android:name=".SearchableActivity" >  
  4.       <intent-filter>  
  5.          <action android:name="android.intent.action.SEARCH" />  
  6.       </intent-filter>  
  7.       <meta-data android:name="android.app.searchable"  
  8.       android:resource="@xml/searchable"/>  
  9.    </activity>  
  10.   
  11. <!-- this activity enables the search dialog to initiate searches  
  12. in the SearchableActivity -->  
  13.       <activity android:name=".OtherActivity" ... >  
  14.    <!-- enable the search dialog to send searches to SearchableActivity -->  
  15.       <meta-data android:name="android.app.default_searchable"  
  16.       android:value=".SearchableActivity" />  
  17.    </activity>  
  18. ...  
  19. </application>  
Analysis

The OtherActivity now includes a <meta-data> element to declare which searchable activity to use for searches, the activity has enabled the search dialog. While the user is in this activity, the onSearchRequested() method activates the search dialog. When the user executes the search, the system starts SearchableActivity and delivers it the ACTION_SEARCH intent. However if we want that every activity then something.

Invoking the search dialog


Generally some device has been providing  a dedicated search button and the behavior of the button may vary between devices and many devices do not provide a search Button at all. A button must exist and when clicked the method onSearchRequested() is executed. It can also enable "type-to-search" functionality that must activate the search dialog when the user starts typing on the keyboard, the keystrokes are inserted into the search dialog. You can enable type-to-search in your activity by calling setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL) meanwhile the activity's onCreate() method does something.

Impact on Activity lifecycle

The search dialog floats at the top of the screen. It does not cause any change in the activity. No life cycle methods are called. The activity might loose the focus of the inputis being given by the user. In some cases it happens when the activity looses the focus that the input is activated by just overriding the onSearchRequest().

For example
  1. @Override  
  2. public boolean onSearchRequested() {  
  3.     pauseSomeStuff();  
  4.     return super.onSearchRequested();  
  5. }  
However a user skips the search by pressing the back button. In this case the activity regains input by closing the search dialog. Basically many things could be used, just like setOnDissListener() must be called for notifying the search dialog.

Let us set the android: launchmode attribute to "singleTop", then the searchable activity receives the Action_Search intent with a call to onNewIntent(intent);
  1. package com.example.valen;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7.   
  8.   
  9. public class RecentActivity extends Activity {  
  10.   
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.search);  
  15.         handleIntent(getIntent());  
  16.     }  
  17.   
  18.     @Override  
  19.     protected void onNewIntent(Intent intent) {  
  20.         setIntent(intent);  
  21.         handleIntent(intent);  
  22.     }  
  23.   
  24.     private void handleIntent(Intent intent) {  
  25.         if (Intent.ACTION_SEARCH.equals(intent.getAction())) {  
  26.           String query = intent.getStringExtra(SearchManager.QUERY);  
  27.           doMySearch(query);  
  28.         }  
  29.     }  
  30.   
  31.   
  32.     @Override  
  33.     public boolean onCreateOptionsMenu(Menu menu) {  
  34.         // Inflate the menu; this adds items to the action bar if it is present.  
  35.         getMenuInflater().inflate(R.menu.recent, menu);  
  36.         return true;  
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onOptionsItemSelected(MenuItem item) {  
  41.         // Handle action bar item clicks here. The action bar will  
  42.         // automatically handle clicks on the Home/Up button, so long  
  43.         // as you specify a parent activity in AndroidManifest.xml.  
  44.         int id = item.getItemId();  
  45.         if (id == R.id.action_settings) {  
  46.             return true;  
  47.         }  
  48.         return super.onOptionsItemSelected(item);  
  49.     }  
  50. }  
When our system calls for the onNewIntent(Intent), the activity has not been restarted yet, then the getIntent() method returns the same intent that was received with onCreate(). That is a very good reason to create this intent here. This is why it should call setIntent(Intent) inside onNewIntent(Intent).

ing Search Context Data

In some cases there are refined searches done by the user so in these scenarios the necessary and extra data could be ed in the intents. It can be ed in the APP_DATA Bundle that is included in the ACTION_SEARCH intent.

To this kind of data to your searchable activity, override the onSearchRequested() method for the activity from which the user can perform a search, create a Bundle with the additional data and call startSearch() to activate the search dialog.

For example
  1. @Override   
  2. public boolean onSearchRequested() {   
  3.    Bundle appData = new Bundle();   
  4.    appData.putBoolean(SearchableActivity.JARGON, true);   
  5.    startSearch(nullfalse, appData, false);   
  6.    return true;   
  7. }   
  8. //However while doing all the things don't forget to add the following method.   
  9.    Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);   
  10.    if (appData != null) {   
  11.       boolean jargon = appData.getBoolean(SearchableActivity.JARGON);   
  12. }   
Summary

This articles explains how to make a SearchView using the search dialog as said above. This article is a continuation of the previous articles however the basics of the search system has already been discussed so far. 

Up Next
    Ebook Download
    View all
    Learn
    View all