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?
Related
I have created a search view that takes input. If the input string is greater than or equal to 3 then a drop down is shown with some suggestion. That suggestion list comes from the server, but currently it shows for length 3, but I can't type more in textfield after 3 chars - the keyboard hides if try to write more.
Here is my code snippet.
val searchitems by viewmodel.stateFlowMessage.collectAsState()
var showdropdown by remember {
mutableStateOf(false)
}
Box(
Modifier
.fillMaxWidth()
.height(56.dp)
.background(Color.White), contentAlignment = Alignment.Center
) {
Column() {
TextField(
value = state.value,
onValueChange = {
state.value = it
if (state.value.text.length >= 3) {
searchquery()showdropdown = true
}
},
singleLine = true,
textStyle = TextStyle(
fontWeight = FontWeight(400),
color = Color.Black,
fontSize = 14.sp,
),
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth(1f),
placeholder = {
Text(
text = label,
modifier = Modifier
.wrapContentSize()
.padding(start = 15.dp),
style = TextStyle(
fontSize = 14.sp, color = colorResource(id = R.color.grey))
)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Search,
autoCorrect = true,
),
leadingIcon = {
Image(
imageVector = Icons.Default.Search,
contentDescription = "",
modifier = Modifier
.size(24.dp)
.padding(start = 4.dp),
colorFilter = ColorFilter.tint(color = colorResource(id = R.color.grey))
)
},
colors = TextFieldDefaults.textFieldColors(
cursorColor = colorResource(id = R.color.grey),
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
backgroundColor = colorResource(id = R.color.col2)
)
)
}
DropdownMenu(expanded = showdropdown,
onDismissRequest = { showdropdown = false }, properties = PopupProperties(focusable = false)) {
Log.d("checks", "${searchitems.size}")
for (it in searchitems) {
DropdownMenuItem(onClick = {
selectitem(it)
state.value = TextFieldValue(it)
showdropdown = false
}
) {
Text(text = it,
style = TextStyle(fontSize = 14.sp,
color = Color.Black,
fontWeight = FontWeight(400)))
}
}
}
}
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
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")
}
},
)
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()
)
}
I am working on an Android app in compose,
When writing text, I need to show a keaybord that has the "underline", "italic" and "bold" buttons present on top of the keyboard text.
Is there a way I can do that?
Currently, this is what I have:
val keyboardController = LocalSoftwareKeyboardController.current
var textValue by remember { mutableStateOf(TextFieldValue("Write your text...")) }
TextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
placeholder = { Text(text = "John Doe") },
textStyle = TextStyle(
color = Color.Gray,
fontSize = 20.sp,
// fontWeight = FontWeight.Bold,
// textDecoration = TextDecoration.Underline
),
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
},
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
// do something here
}
) )