How do I invoke the search dialog using onSearchRequested()

前端 未结 4 1386
执笔经年
执笔经年 2021-02-15 17:51

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 sho

相关标签:
4条回答
  • 2021-02-15 18:18

    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>
    
    0 讨论(0)
  • 2021-02-15 18:20

    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 讨论(0)
  • 2021-02-15 18:22

    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.

    0 讨论(0)
  • 2021-02-15 18:29

    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.

    0 讨论(0)
提交回复
热议问题