SearchBar
A search bar represents a floating search field that allows users to enter a keyword or phrase and get relevant information. It can be used as a way to navigate through an app via search queries.
A search bar expands into a search "view" and can be used to display dynamic suggestions or search results.
A SearchBar tries to occupy the entirety of its allowed size in the expanded state. For full-screen behavior as specified by Material guidelines, parent layouts of the SearchBar must not pass any Constraints that limit its size, and the host activity should set WindowCompat.setDecorFitsSystemWindows(window, false)
.
If this expansion behavior is undesirable, for example on large tablet screens, DockedSearchBar can be used instead.
An example looks like:
Parameters
the input field of this search bar that allows entering a query, typically a SearchBarDefaults.InputField.
whether this search bar is expanded and showing search results.
the callback to be invoked when this search bar's expanded state is changed.
the Modifier to be applied to this search bar.
the shape of this search bar when it is not expanded. When expanded, the shape will always be SearchBarDefaults.fullScreenShape.
SearchBarColors that will be used to resolve the colors used for this search bar in different states. See SearchBarDefaults.colors.
when SearchBarColors.containerColor is ColorScheme.surface, a translucent primary color overlay is applied on top of the container. A higher tonal elevation value will result in a darker color in light theme and lighter color in dark theme. See also: Surface.
the elevation for the shadow below this search bar
the window insets that this search bar will respect
the content of this search bar to display search results below the inputField.
Samples
androidx.compose.material3.samples.SearchBarSampleDeprecated
Use overload which takes inputField as a parameter
Replace with
SearchBar(
inputField = {
SearchBarDefaults.InputField(
query = query,
onQueryChange = onQueryChange,
onSearch = onSearch,
expanded = active,
onExpandedChange = onActiveChange,
enabled = enabled,
placeholder = placeholder,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
colors = colors.inputFieldColors,
interactionSource = interactionSource,
)
},
expanded = active,
onExpandedChange = onActiveChange,
modifier = modifier,
shape = shape,
colors = colors,
tonalElevation = tonalElevation,
shadowElevation = shadowElevation,
windowInsets = windowInsets,
content = content,
)