androidx.compose.material3

SearchBarExperimental

Composable function

A search bar is a field for searching. When the user taps it, it opens a view for suggestions and results.

In the newer form, the work is split in two:

  1. A collapsed SearchBar (or AppBarWithSearch at the top of a screen) shows the field.
  2. An expanded bar shows results: ExpandedFullScreenSearchBar on phones, ExpandedDockedSearchBar on wider screens. Pass it the same state and field.

Both get a SearchBarState from rememberSearchBarState(). The text lives in a TextFieldState.

A collapsed search barExpressive theme
A collapsed search bar
val text = rememberTextFieldState()
val state = rememberSearchBarState()
SearchBar(
    state = state,
    inputField = {
        SearchBarDefaults.InputField(
            textFieldState = text,
            searchBarState = state,
            onSearch = { /* run the search */ },
            placeholder = { Text("Search photos") },
            leadingIcon = { Icon(Icons.Filled.Search, contentDescription = null) },
        )
    },
    modifier = Modifier.width(360.dp),
)

Make the field with SearchBarDefaults.InputField. Pass it the text state, the search bar state, and onSearch, which runs when the user presses the search key.

StateSince 1.9Experimental

The newer form. Takes a SearchBarState. Pair it with an expanded search bar.

@Composable
fun SearchBar(
    state: SearchBarState,
    inputField: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    shape: Shape = SearchBarDefaults.inputFieldShape,
    colors: SearchBarColors = SearchBarDefaults.colors(),
    tonalElevation: Dp = SearchBarDefaults.TonalElevation,
    shadowElevation: Dp = SearchBarDefaults.ShadowElevation,
)

state: SearchBarStateRequired

Whether the bar is collapsed or expanded. Create it with rememberSearchBarState(). Share it between the collapsed and the expanded bar.

inputField: @Composable () -> UnitRequired

The text field. Use SearchBarDefaults.InputField(...).

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

shape: ShapeOptional

Default: SearchBarDefaults.inputFieldShape

Sets the outline. Default: fully round.

colors: SearchBarColorsOptional

Default: SearchBarDefaults.colors()

Sets the container and field colors. Make one with SearchBarDefaults.colors.

tonalElevation: DpOptional

Default: SearchBarDefaults.TonalElevation

Tints the background a little toward the primary color.

shadowElevation: DpOptional

Default: SearchBarDefaults.ShadowElevation

The size of the shadow under the bar.

Expanded flagExperimental

The older form. Takes expanded and shows results inside itself.

@Composable
fun SearchBar(
    inputField: @Composable () -> Unit,
    expanded: Boolean,
    onExpandedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    shape: Shape = SearchBarDefaults.inputFieldShape,
    colors: SearchBarColors = SearchBarDefaults.colors(),
    tonalElevation: Dp = SearchBarDefaults.TonalElevation,
    shadowElevation: Dp = SearchBarDefaults.ShadowElevation,
    windowInsets: WindowInsets = SearchBarDefaults.windowInsets,
    content: @Composable ColumnScope.() -> Unit,
)

expanded: BooleanRequired

Whether the older search bar is open. Pass your state here.

onExpandedChange: (Boolean) -> UnitRequired

Called when the older search bar opens or closes. Update your state here.

windowInsets: WindowInsetsOptional

Default: SearchBarDefaults.windowInsets

Space kept free for system bars. The default is usually right.

content: @Composable ColumnScope.() -> UnitRequired

Suggestions or results, shown while the bar is expanded. Placed in a Column.

Same as above: inputField, modifier, shape, colors, tonalElevation, shadowElevation.

Search bar types

See also