开发者

How do I invoke the search dialog using onSearchRequested()

开发者 https://www.devze.com 2023-01-31 13:02 出处:网络
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

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" />
      开发者_StackOverflow社区  </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?


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.


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.


You are missing tag in your activity tag in manifest. replace your activity declaration by following.

<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>

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

    </activity>


I've done all that but still cannot see the search dialog on API 17. It works however after adding

android:actionViewClass="android.widget.SearchView"

in the fragment menu

<item
    android:id="@+id/menu_item_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom"
    android:title="@string/search"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消