Remaining Backward Compatible
Stay organized with collections
Save and categorize content based on your preferences.
Try the Compose way
Jetpack Compose is the recommended UI toolkit for Android. Learn how to add search functionality in Compose.
The SearchView
and action bar are only available on Android 3.0 and
later. To support older platforms, you can fall back to the search dialog. The search dialog is a
system provided UI that overlays on top of your application when invoked.
Set Minimum and Target API levels
To setup the search dialog, first declare in your manifest that you want to support older
devices, but want to target Android 3.0 or later versions. When you do this, your application
automatically uses the action bar on Android 3.0 or later and uses the traditional menu system on
older devices:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />
<application>
...
Provide the Search Dialog for Older Devices
To invoke the search dialog on older devices, call onSearchRequested()
whenever a user selects the search
menu item from the options menu. Because Android 3.0 and higher devices show the
SearchView
in the action bar (as demonstrated in the first lesson), only versions
older than 3.0 call onOptionsItemSelected()
when the
user selects the search menu item.
Kotlin
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.search -> {
onSearchRequested()
true
}
else -> false
}
}
Java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
Check the Android Build Version at Runtime
At runtime, check the device version to make sure an unsupported use of SearchView
does not occur on older devices. In our example code, this happens in
the onCreateOptionsMenu()
method:
Kotlin
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.options_menu, menu)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
(menu.findItem(R.id.search).actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
setIconifiedByDefault(false)
}
}
return true
}
Java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-03-24 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-24 UTC."],[],[],null,["# Remaining Backward Compatible\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\nThe [SearchView](/reference/android/widget/SearchView) and action bar are only available on Android 3.0 and\nlater. To support older platforms, you can fall back to the search dialog. The search dialog is a\nsystem provided UI that overlays on top of your application when invoked.\n\nSet Minimum and Target API levels\n---------------------------------\n\nTo setup the search dialog, first declare in your manifest that you want to support older\ndevices, but want to target Android 3.0 or later versions. When you do this, your application\nautomatically uses the action bar on Android 3.0 or later and uses the traditional menu system on\nolder devices: \n\n```xml\n\u003cuses-sdk android:minSdkVersion=\"7\" android:targetSdkVersion=\"15\" /\u003e\n\n\u003capplication\u003e\n...\n```\n\nProvide the Search Dialog for Older Devices\n-------------------------------------------\n\nTo invoke the search dialog on older devices, call [onSearchRequested()](/reference/android/app/Activity#onSearchRequested()) whenever a user selects the search\nmenu item from the options menu. Because Android 3.0 and higher devices show the\n[SearchView](/reference/android/widget/SearchView) in the action bar (as demonstrated in the first lesson), only versions\nolder than 3.0 call [onOptionsItemSelected()](/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem)) when the\nuser selects the search menu item. \n\n### Kotlin\n\n```kotlin\noverride fun onOptionsItemSelected(item: MenuItem): Boolean {\n return when (item.itemId) {\n R.id.search -\u003e {\n onSearchRequested()\n true\n }\n else -\u003e false\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n case R.id.search:\n onSearchRequested();\n return true;\n default:\n return false;\n }\n}\n```\n\nCheck the Android Build Version at Runtime\n------------------------------------------\n\nAt runtime, check the device version to make sure an unsupported use of [SearchView](/reference/android/widget/SearchView) does not occur on older devices. In our example code, this happens in\nthe [onCreateOptionsMenu()](/reference/android/app/Activity#onCreateOptionsMenu(android.view.Menu)) method: \n\n### Kotlin\n\n```kotlin\noverride fun onCreateOptionsMenu(menu: Menu): Boolean {\n\n menuInflater.inflate(R.menu.options_menu, menu)\n\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.HONEYCOMB) {\n val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager\n (menu.findItem(R.id.search).actionView as SearchView).apply {\n setSearchableInfo(searchManager.getSearchableInfo(componentName))\n setIconifiedByDefault(false)\n }\n }\n return true\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onCreateOptionsMenu(Menu menu) {\n\n MenuInflater inflater = getMenuInflater();\n inflater.inflate(R.menu.options_menu, menu);\n\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.HONEYCOMB) {\n SearchManager searchManager =\n (SearchManager) getSystemService(Context.SEARCH_SERVICE);\n SearchView searchView =\n (SearchView) menu.findItem(R.id.search).getActionView();\n searchView.setSearchableInfo(\n searchManager.getSearchableInfo(getComponentName()));\n searchView.setIconifiedByDefault(false);\n }\n return true;\n}\n```"]]