简体   繁体   中英

How to add Search Dialog to an Android app

Here is my issue. I have a requirement to add a search dialog to an Android app. I have found the way to add a search view component, but not the search dialog. I know from the Android Developers docs that I am supposed to use onSearchRequested() to make it appear, but I don't quite understand what that means. Is the search dialog supposed to be tied to my own UI component that will invoke this method or am I missing something? I haven't managed to read all the docs on searching, but I was hoping someone may point me where I missed something important.

I am not well versed in Android development, only a few small projects. I am more of a Flutter dev when creating a mobile app, but I don't have the option this time around.

Below is the screenshot of what the Android Developers docs show as search dialog and the comparison to search view. 在此处输入图像描述

Based on the documentation , the steps for invoking a Search Dialog are:

  1. create an Xml file (which you already did for search widget )

  2. Declaring a searchable activity: create an actiivty and notify the Manifest that it gets the search results (you did that too)

3. At the Manifest , decalre some other activity (MainActivity for examlpe) to be able to invoke the search dialog. Add this line inside <activity> tag:

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

( android:value is the name of the Activity that gets the searcg results, which you defined at step 2)

4. At that Activity, create a search button etc and invoke the Serach Dialog like this:

onSearchRequested();

Very simple actually:)

The search words stored in intent.getStringExtra(SearchManager.QUERY) as a String, you get it at onCreate() of SearchableActivity class. From this point you should handle the search query and results logic...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM