[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Set up the search interface\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add search functionality in Compose. \n[Filter a list →](/develop/ui/compose/quick-guides/content/filter-list-while-typing) \n\nWe recommend using the\n[SearchView](/reference/android/widget/SearchView)\nwidget as an item in the app bar to provide search functionality in your app. As\nwith all items in the app bar, you can define the `SearchView` to\nshow at all times or only when there is room. You can also define it as a\ncollapsible action, which displays the `SearchView` as an icon\ninitially and then takes up the entire app bar as a search field when the user\ntaps the icon.\n\nAdd the SearchView to the app bar\n---------------------------------\n\nTo add a `SearchView` widget to the app bar, create a file in your\nproject named `res/menu/options_menu.xml` and add the following code\nto the file. This code defines how to create the search item, such as the icon\nto use and the title of the item. The `collapseActionView` attribute\nlets your `SearchView` expand to take up the whole app bar and\ncollapse back down into a normal app bar item when not in use. Because of the\nlimited app bar space on handset devices, we recommend using the\n`collapsibleActionView` attribute to provide a better user\nexperience. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cmenu xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n\u003citem android:id=\"@+id/search\"\n android:title=\"@string/search_title\"\n android:icon=\"@drawable/ic_search\"\n android:showAsAction=\"collapseActionView|ifRoom\"\n android:actionViewClass=\"androidx.appcompat.widget.SearchView\" /\u003e\n\u003c/menu\u003e\n```\n| **Note:** If you already have an XML file for your menu items, you can add the `\u003citem\u003e` element to that file instead.\n\nIf you want a more accessible search icon, create an\n`ic_search.xml` file in the `/res/drawable` folder and\ninclude the following code in it: \n\n```xml\n\u003cvector\n android:height=\"24dp\"\n android:tint=\"#000000\"\n android:viewportHeight=\"24\"\n android:viewportWidth=\"24\"\n android:width=\"24dp\"\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003cpath android:fillColor=\"@android:color/white\" android:pathData=\"M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z\"/\u003e\n\u003c/vector\u003e\n```\n\nTo display the `SearchView` in the app bar, inflate the XML menu\nresource `res/menu/options_menu.xml` in the\n[onCreateOptionsMenu()](/reference/android/app/Activity#onCreateOptionsMenu(android.view.Menu))\nmethod of your activity: \n\n### Kotlin\n\n```kotlin\noverride fun onCreateOptionsMenu(menu: Menu): Boolean {\n menuInflater.inflate(R.menu.options_menu, menu)\n\n return true\n}\n```\n\nRunning the app generates something like this:\n**Figure 1.** A search icon in the app top bar.\n\nThe `SearchView` appears in your app's app bar, but it isn't\nfunctional. If you tap the search icon, you get something like this:\n**Figure 2.** `SearchView` in action.\n\nTo make the `SearchView` functional, you must define how the\n`SearchView` behaves.\n\nCreate a search configuration\n-----------------------------\n\nA [search\nconfiguration](/guide/topics/search/searchable-config) specifies how the `SearchView` behaves and is\ndefined in a `res/xml/searchable.xml` file. A search configuration\nmust contain, at minimum, an `android:label` attribute that has the\nsame value as the `android:label` attribute of the\n[\\\u003capplication\\\u003e](/guide/topics/manifest/application-element)\nor [\\\u003cactivity\\\u003e](/guide/topics/manifest/activity-element)\nelement in your Android manifest. However, we also recommend adding an\n`android:hint` attribute to give the user an idea of what to enter\ninto the search box. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\n\u003csearchable xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:label=\"@string/app_name\"\n android:hint=\"@string/search_hint\" /\u003e\n```\n\nIn your app's manifest file, declare a\n[`\u003cmeta-data\u003e`](/guide/topics/manifest/meta-data-element)\nelement that points to the `res/xml/searchable.xml` file. Declare the\nelement in an `\u003cactivity\u003e` in which you want to display the\n`SearchView`. \n\n```xml\n\u003cactivity\nandroid:name=\".SearchResultsActivity\"\nandroid:exported=\"false\"\nandroid:label=\"@string/title_activity_search_results\"\nandroid:launchMode=\"singleTop\"\nandroid:theme=\"@style/Theme.AppCompat.Light\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.SEARCH\" /\u003e\n \u003c/intent-filter\u003e\n \u003cmeta-data\n android:name=\"android.app.searchable\"\n android:resource=\"@xml/searchable\" /\u003e\n\u003c/activity\u003e\n```\n\nIn the `onCreateOptionsMenu()` method that you create, associate\nthe search configuration with the `SearchView` by calling\n[setSearchableInfo(SearchableInfo)](/reference/android/widget/SearchView#setSearchableInfo(android.app.SearchableInfo)): \n\n### Kotlin\n\n```kotlin\noverride fun onCreateOptionsMenu(menu: Menu): Boolean {\n menuInflater.inflate(R.menu.options_menu, menu)\n\n val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager\n val searchView = menu.findItem(R.id.search).actionView as SearchView\n val component = ComponentName(this, SearchResultsActivity::class.java)\n val searchableInfo = searchManager.getSearchableInfo(component)\n searchView.setSearchableInfo(searchableInfo)\n return true\n}\n```\n\nThe call to\n[getSearchableInfo()](/reference/android/app/SearchManager#getSearchableInfo(android.content.ComponentName))\nobtains a\n[SearchableInfo](/reference/android/app/SearchableInfo)\nobject that is created from the search configuration XML file. When the search\nconfiguration is correctly associated with your `SearchView` and the\nuser submits a query, the `SearchView` starts an activity with the\n[ACTION_SEARCH](/reference/android/content/Intent#ACTION_SEARCH)\nintent. You then need an activity that can filter for this intent and handle the\nsearch query.\n\nCreate a searchable activity\n----------------------------\n\nA searchable activity filters for the `ACTION_SEARCH` intent and\nsearches for the query in a data set. To create a searchable activity, declare\nan activity of your choice to filter for the `ACTION_SEARCH`\nintent: \n\n```xml\n\u003cactivity android:name=\".SearchResultsActivity\" ... \u003e\n ...\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.SEARCH\" /\u003e\n \u003c/intent-filter\u003e\n ...\n\u003c/activity\u003e\n```\n\nIn your searchable activity, handle the `ACTION_SEARCH` intent by\nchecking for it in your\n[onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle))\nmethod.\n**Note:** If your searchable activity launches in single top mode---`android:launchMode=\"singleTop\"`---also handle the `ACTION_SEARCH` intent in the [onNewIntent()](/reference/android/app/Activity#onNewIntent(android.content.Intent)) method. In single top mode, only one instance of your activity is created. Subsequent calls to start your activity don't create a new activity on the stack. This launch helps users perform searches from the same activity without creating a new activity instance every time. \n\n### Kotlin\n\n```kotlin\nclass SearchResultsActivity : AppCompatActivity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_search_results)\n handleIntent(intent)\n }\n\n override fun onNewIntent(intent: Intent) {\n super.onNewIntent(intent)\n handleIntent(intent)\n }\n\n private fun handleIntent(intent: Intent) {\n if (Intent.ACTION_SEARCH == intent.action) {\n val query = intent.getStringExtra(SearchManager.QUERY)\n Log.d(\"SEARCH\", \"Search query was: $query\")\n }\n }\n}\n```\n\nNow, the `SearchView` can accept the user's query and start your\nsearchable activity with the `ACTION_SEARCH` intent.\n\nAfter you obtain the search query, you can pass it to the\n`ViewModel`, where you can use it in other layers of your\narchitecture to retrieve the search results to display."]]