Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invoke the search dialog using onSearchRequested()

Tags:

android

I'm trying to implement the search dialog and I am unable to display the search from an Activity.

I have my main activity defined in my manifest file, this activity shows the user a list of options they have to choose from. One of the options is a Search option.

    <activity
        android:name=".MenuListActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

My Search activity is defined in my manifest file like so.

<activity android:name=".SearchActivity"  
              android:launchMode="singleTop"  
              android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>  
            <meta-data android:name="android.app.searchable"  
                       android:resource="@xml/searchable"/>  
</activity>

Now my problem is when I call the onSearchRequested() from my MenuListActivity nothing happens.

public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {  
          String strText = items[position];  

          if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_browse))) {  
              startActivity(new Intent(MenuListActivity.this, WSMobileActivity.class));  
          } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_search))) { 

            // If i call it like this it doesn't work, nothing happens  
              onSearchRequested();  

          } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_locator))) {
              startActivity(new Intent(MenuListActivity.this, StoreLocatorActivity.class));  
          }  
      }  

Please help, how do I invoke the search request from my MenuActivity?

like image 890
user459255 Avatar asked Dec 16 '10 21:12

user459255


People also ask

How to make search button in android?

Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.


2 Answers

did you check your res/xml/searchable.xml?

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:hint="@string/search_hint" 
  android:label="@string/search_label">
</searchable>

Search dialog doesn't show up when you have hard coded strings for hint and label. They have to be @string resources.

Also, there is no need to call or override the onSearchRequested() method in your calling activity unless you want to invoke search from one of your UI widgets like a Button or a ListItem.

like image 77
motiver Avatar answered Sep 17 '22 15:09

motiver


Apart from declaring the SearchActivity in the manifest file you need to include the meta-data info.

If you want to invoke the search dialog throughout the application then include

<meta-data android:name="android.app.default_searchable"
               android:value=".SearchActivity" />

inside the application tag.

If you want to invoke the search dialog only in a particular activity then include

<meta-data android:name="android.app.default_searchable"
               android:value=".SearchActivity" />

inside the activity tag.

for more details please refer http://developer.android.com/guide/topics/search/search-dialog.html.

like image 36
Manish Khot Avatar answered Sep 18 '22 15:09

Manish Khot