androidx.compose.material3

PrimaryTabRow

Composable function

Tabs switch between related views on the same screen, like "Flights", "Hotels" and "Cars".

Use a PrimaryTabRow at the top of a screen, under the top app bar. Use a SecondaryTabRow for tabs inside a part of the screen. Inside, add one Tab per view. You keep the selected index in a variable.

Primary tabs
Primary tabs
var selected by remember { mutableIntStateOf(0) }
val titles = listOf("Flights", "Hotels", "Cars")
PrimaryTabRow(selectedTabIndex = selected, modifier = Modifier.width(360.dp)) {
    titles.forEachIndexed { index, title ->
        Tab(selected = selected == index, onClick = { selected = index }, text = { Text(title) })
    }
}
Secondary tabs
Secondary tabs
var selected by remember { mutableIntStateOf(1) }
val titles = listOf("Overview", "Specs", "Reviews")
SecondaryTabRow(selectedTabIndex = selected, modifier = Modifier.width(360.dp)) {
    titles.forEachIndexed { index, title ->
        Tab(selected = selected == index, onClick = { selected = index }, text = { Text(title) })
    }
}

Parameters

@Composable
fun PrimaryTabRow(
    selectedTabIndex: Int,
    modifier: Modifier = Modifier,
    containerColor: Color = TabRowDefaults.primaryContainerColor,
    contentColor: Color = TabRowDefaults.primaryContentColor,
    indicator: @Composable TabIndicatorScope.() -> Unit = { TabRowDefaults.PrimaryIndicator(modifier = Modifier.tabIndicatorOffset(selectedTabIndex, matchContentSize = true), width = Dp.Unspecified) },
    divider: @Composable () -> Unit = @Composable { HorizontalDivider() },
    tabs: @Composable () -> Unit,
)

selectedTabIndex: IntRequired

The index of the selected tab. The indicator moves to it.

Use a scrollable row when there are many tabs, or their labels are long.

Scrollable tabs
Scrollable tabs
var selected by remember { mutableIntStateOf(2) }
val titles = listOf("All", "Music", "Podcasts", "Audiobooks", "Radio", "Live")
PrimaryScrollableTabRow(selectedTabIndex = selected, modifier = Modifier.width(360.dp)) {
    titles.forEachIndexed { index, title ->
        Tab(selected = selected == index, onClick = { selected = index }, text = { Text(title) })
    }
}

modifier: ModifierOptional

Default: Modifier

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

containerColor: ColorOptional

Default: TabRowDefaults.primaryContainerColor

The background color of the row.

contentColor: ColorOptional

Default: TabRowDefaults.primaryContentColor

The color of the tabs. Default: the theme's primary for primary tabs.

indicator: @Composable TabIndicatorScope.() -> UnitOptional

Default: { TabRowDefaults.PrimaryIndicator(modifier = Modifier.tabIndicatorOffset(selectedTabIndex, matchContentSize = true), width = Dp.Unspecified) }

The line under the selected tab. Change it only for a custom look. The defaults are in TabRowDefaults.

divider: @Composable () -> UnitOptional

Default: @Composable { HorizontalDivider() }

The line under the whole row. Pass {} for none.

tabs: @Composable () -> UnitRequired

The tabs. Add one Tab for each view. They share the width equally. For many tabs that need to scroll, use PrimaryScrollableTabRow instead.

Tab types