Android Compose - Align center one composable only in Column? - android

In Compose, you can align center all composables in a Column widget by doing the following:
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "First item",
modifier = Modifier.padding(16.dp)
)
Text(
text = "Second item",
modifier = Modifier.padding(16.dp)
)
Text(
text = "Third item",
modifier = Modifier.padding(16.dp)
)
}
However, what if I only want to center the first composable?

I think this should work for you
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "First item",
modifier = Modifier.padding(16.dp)
)
Text(
text = "Second item",
modifier = Modifier.padding(16.dp).fillMaxWidth()
)
Text(
text = "Third item",
modifier = Modifier.padding(16.dp).fillMaxWidth()
)
}

Related

How do I separate text and icon in TextButton

How do I separate text and icon in TextButton to make it like whatsapp settings
Row(
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
) {
TextButton(
onClick = { /*TODO*/ },
modifier = Modifier.fillMaxSize(),
) {
Text(text = "$name")
Icon(
imageVector = Icons.Filled.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.size(40.dp)
)
}
}
It shows
But I want to like this
I try Spacer and Padding but it didn't work because I have
fun SettingsButtons(name: String)
#Composable
fun SettingsButtons(name: String) {
Row(
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
) {
TextButton(
onClick = { /*TODO*/ },
modifier = Modifier.fillMaxSize(),
) {
Text(text = "$name")
Icon(
imageVector = Icons.Filled.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.size(40.dp)
)
}
}
}
And based on the parameter that I passed {name} the text will be changed
SettingsButtons(name = "Account")
SettingsButtons(name = "Order History")
SettingsButtons(name = "Favorite")
so I think that why Spacer and Padding didn't work because the text size is different or I am not using it correctly
The content of a TextButton is a RowScope.
You can apply a weight(1f) modifier to the Text
TextButton(
onClick = { /*TODO*/ },
modifier = Modifier.fillMaxWidth(),
) {
Text(text = "Name", modifier = Modifier.weight(1f))
Icon(
imageVector = Icons.Filled.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.size(40.dp)
)
}
Try RaisedButton with a Row as Child and in Row add Text and Icon widget
RaisedButton(
onPressed: () {},
child: Row(
children: <Widget>[
Text(
'Button'),
Icon(
Icons.arrow_forward,
color: Colors.white,
))
I think what you are looking for is Arrangement. You could try to use Arrangement like this:
Row(modifier = Modifier.fillMaxWidth().height(50.dp)) {
TextButton(
onClick = { /*TODO*/ },
modifier = Modifier.fillMaxWidth(),
Arrangement.SpaceBetween
) {
Text(text = "$name")
Icon(
imageVector = Icons.Filled.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.size(40.dp)
)
}
}
I found this question which is quite similiar to your problem but in a vertical direction:
Filling remaining space with Spacer on Jetpack Compose
Further I will link you the documentation where you can find more info about alignment and arrangement in jetpack compose.
https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/Arrangement

GoogleMap composable taking all available screen space

I expected the following code to show The map on the top and two text fields at the bottom. Instead, the map is taking all available space.
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(10.dp),
modifier = Modifier.fillMaxWidth(),
) {
GoogleMap(
cameraPositionState = cameraPositionState
) {
Marker(
position = singapore,
title = "Singapore",
snippet = "Marker in Singapore"
)
}
TextField(
value = "",
onValueChange = {},
label = { Text(text = "Load Location") },
modifier = Modifier.fillMaxSize()
)
TextField(
value = "",
onValueChange = {},
label = { Text(text = "Unload Location") },
modifier = Modifier.fillMaxSize()
)
}
Ever heard of the weight modifier?

How to align two texts from different rows in Jetpack Compose

I have 3 texts in one row and then another three in another row inside a LazyColumn
I want to align the "text" in the center of the first row with "15.025" value in the second row
I've used
TextAlign.Center
on both of the Texts and
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
set this on the Row but it doesn't work. Any tips please?
I'm pretty new in Compose, sorry for the basic question.
You can use Modifier.weight to make the width of "left view" equals to the width of "right view". It will make the middle text on each row always center.
Row(Modifier.fillMaxWidth()) {
Text(
text = "Start",
modifier = Modifier.weight(1f)
)
Text(
text = "Center",
textAlign = TextAlign.Center, // you can also fixed the width of center text
)
Text(
text = "End",
modifier = Modifier.weight(1f),
textAlign = TextAlign.End
)
}
If your text content is always short, you can also use Box to make the middle text always center.
Box(Modifier.fillMaxWidth()) {
Text(text = "Start", modifier = Modifier.align(Alignment.CenterStart))
Text(text = "Center", modifier = Modifier.align(Alignment.Center))
Text(text = "End", modifier = Modifier.align(Alignment.CenterEnd))
}
This is what I wanted to achieve and with the help of #Linh I did
Here's how I did it.
Column(Modifier.fillMaxSize()) {
Row(Modifier.fillMaxWidth()) {
Text(
text = "Start",
modifier = Modifier.weight(1.5f),
textAlign = TextAlign.Start
)
Text(
text = "Center",
modifier = Modifier.weight(1f),
textAlign = TextAlign.Start
)
Text(
text = "End",
modifier = Modifier.weight(1f),
textAlign = TextAlign.End
)
}
}
Thanks again for the help.

Jetpack Compose: Center Icon or Trailing elements in a ListItem

I'm trying to center the icon and trailing elements vertically using a ListItem. I'm not sure if it is possible or I need to drop the ListItem and use a combination of column/row instead.
I'm using something like:
ListItem(
Modifier.clickable(onClick = onClick),
icon = {
Box(
modifier = Modifier.fillMaxHeight(), contentAlignment = ALignment.Center
) {
Text(
text = dateUtil.formatTime(item.startTime),
style = TextStyle(
color = SnyDarkBlue,
fontSize = 14.sp,
fontFamily = oswaldFamily,
fontWeight = FontWeight.Bold,
letterSpacing = (-0.25).sp,
),
)
}
},
thanks in advance!
You can center both the icon and text sections using a Column:
ListItem(
modifier = Modifier
.fillMaxWidth()
.requiredHeight(50.dp)
.background(color = Color.Gray),
icon = {
Column(
modifier = Modifier
.fillMaxHeight(), verticalArrangement = Arrangement.Center
) {
Text(
text = "06:00 PM"
)
}
},
text = {
Column(
modifier = Modifier
.fillMaxHeight(), verticalArrangement = Arrangement.Center
) {
Text("Your item text goes here")
}
},
)

Icons getting disappears if the text is too long in Jetpack Compose

#Composable
fun TopAppBar(
name: String,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.fillMaxWidth()
.padding(20.dp, 0.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Back",
tint = Color.Black,
modifier = Modifier.size(24.dp)
)
Text(
text = name,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 20.sp,
maxLines = 1
)
Row {
Icon(
painter = painterResource(id = R.drawable.ic_bell),
contentDescription = null,
tint = Color.Black,
modifier = Modifier
.padding(8.dp, 8.dp)
.size(24.dp)
)
Icon(
painter = painterResource(id = R.drawable.ic_dotmenu),
contentDescription = null,
tint = Color.Black,
modifier = Modifier
.padding(8.dp, 8.dp)
.size(24.dp)
)
}
}
}
You have to set the weight and the fill to false:
Text(
modifier = Modifier.weight(weight = 1f, fill = false),
text = name,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 20.sp,
maxLines = 1
)
You can add the weight modifier to the Text composable
Row(){
Icon()
Text(
text = ".....",
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 20.sp,
maxLines = 1,
modifier = Modifier.weight(1f)
)
//...
}
.

Categories

Resources