I am trying to achieve something like shown in the image below. I tried TabRow with default Tab and its center aligned. I cannot achieve left alignment here. I tried the TapRow with custom tab approach as per Google's website but the tabs were still center aligned ( I guess its TapRow issue and not Tab).
In Tabrow, With fillMaxWidth modifier, its entire row filled and center aligned and with .width=xx.dp approach, its reduced sized tabrow but still goes center aligned. I want left aligned. How can I achieve that?
Here is my code:
@Composable
fun HomeCategoryTabIndicator(
modifier: Modifier = Modifier,
color: Color = MaterialTheme.colors.onSurface
) {
Spacer(
modifier
.padding(horizontal = 10.dp)
.height(4.dp)
.background(color, RoundedCornerShape(topStartPercent = 100, topEndPercent = 100))
)
}
@Composable
fun MyTab(
onClick: (String) -> Unit,
title: String
) {
Column(
Modifier
.padding(10.dp)
.wrapContentHeight()
.wrapContentWidth()
.clickable {onClick(title)},
verticalArrangement = Arrangement.SpaceBetween
) {
Text(
text = title,
style = MaterialTheme.typography.body1,
modifier = Modifier.align(Alignment.CenterHorizontally)
)
}
}
@Composable
private fun HomeCategoryTabs(
categories: List<HomeCategory>,
selectedCategory: HomeCategory,
onCategorySelected: (HomeCategory) -> Unit
) {
val selectedIndex = categories.indexOfFirst { it == selectedCategory }
val indicator = @Composable { tabPositions: List<TabPosition> ->
HomeCategoryTabIndicator(
Modifier.tabIndicatorOffset(tabPositions[selectedIndex]),
color = MaterialTheme.ts.colors.primary,
)
}
TabRow(
modifier = Modifier.width(330.dp),
selectedTabIndex = selectedIndex,
indicator = indicator,
) {
categories.forEach { category ->
MyTab(
onClick = {
onCategorySelected(category)
},
title = when (category) {
HomeCategory.Market -> "Market"
HomeCategory.Sector -> "Sector"
HomeCategory.News -> "News"
HomeCategory.Ideas -> "Ideas"
HomeCategory.Events -> "Events"
}
)
}
}
}
This is whats expected (left aligned):

This is how its showing right now (center aligned):

@Anand Chaudhari hi!
For left alignment you need to use ScrollableTabRow and set edges padding to 0.
Example for your code:
ScrollableTabRow(
edgePadding = 0.dp
selectedTabIndex = selectedIndex,
indicator = indicator,
) {
categories.forEach { category ->
MyTab(
onClick = {
onCategorySelected(category)
},
title = when (category) {
HomeCategory.Market -> "Market"
HomeCategory.Sector -> "Sector"
HomeCategory.News -> "News"
HomeCategory.Ideas -> "Ideas"
HomeCategory.Events -> "Events"
}
)
}
}
Thanks!
You can use ScrollableTabRow with edgePadding = 0.dp instead of TabRow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With