David Corrado
11/10/2022, 8:04 PMDavid Corrado
11/10/2022, 8:04 PMHStack(spacing: 0){
Text("Left Text").background(Color.red)
Text("Right Text").background(Color.blue)
}
HStack(spacing: 0){
Text("Left Text is longer that to fit screen width for a single line so right text will not be visible").background(Color.red)
Text("Right Text").background(Color.blue)
}
HStack(spacing: 0){
Text("Left Text").background(Color.red)
Text("Right Text is longer that to fit screen width so left text will not be visible").background(Color.blue)
}
HStack(spacing: 0){
Text("Left Text is as long as Right Text so right one will not be visible lorem ipsum lorem ipsum").background(Color.red)
Text("Right Text is as long as Left Text so right one will not be visible lorem ipsum lorem ipsum").background(Color.blue)
}
David Corrado
11/10/2022, 8:05 PMKirill Grouchnikov
11/10/2022, 8:44 PMDavid Corrado
11/10/2022, 9:27 PMDavid Corrado
11/10/2022, 9:27 PMDavid Corrado
11/10/2022, 9:28 PMRow {
Text("Left Text", modifier = Modifier.background(color = Color.Red))
Text("Right Text", modifier = Modifier.background(color = Color.Blue))
}
//Broken
Row {
Text(
"Left Text is longer that to fit screen width for a single line so right Text will not be visible",
modifier = Modifier.background(color = Color.Red)
)
Text("Right Text", modifier = Modifier.background(color = Color.Blue))
}
Row {
Text("Left Text", modifier = Modifier.background(color = Color.Red))
Text(
"Right Text is longer that to fit screen width so left text will not be visible",
modifier = Modifier.background(color = Color.Blue)
)
}
//Broken
Row {
Text(
"Left Text is as long as Right Text so right one will not be visible lorem ipsum lorem ipsum",
modifier = Modifier.background(color = Color.Red)
)
Text(
"Right Text is as long as Left Text so right one will not be visible lorem ipsum lorem ipsum",
modifier = Modifier.background(color = Color.Blue)
)
}
David Corrado
11/10/2022, 9:28 PMDavid Corrado
11/10/2022, 9:29 PMdorche
11/10/2022, 9:41 PMrobercoding
11/10/2022, 10:49 PM@Composable
fun MyCoolRowText(leftText: String, rightText: String) {
SubcomposeLayout { constraints ->
// Measure left with text content
val leftTextWidth = subcompose(TextSlot.Left) {
Text(text = leftText)
}.first().measure(constraints).width
// Measure right with text content
val rightTextWidth = subcompose(TextSlot.Right) {
Text(text = rightText)
}.first().measure(constraints).width
// get screen width
val maxWidth = constraints.maxWidth
val maxHalfWidth = maxWidth / 2
// Prepare boolean to check if any of them is higher or lower than width
val bothExceedHalfWidth = leftTextWidth > maxHalfWidth && rightTextWidth > maxHalfWidth
val bothAreLowerThanHalfWidth = leftTextWidth < maxHalfWidth && rightTextWidth < maxHalfWidth
val anyIsHigherThanHalfWidth = leftTextWidth > maxHalfWidth || rightTextWidth > maxHalfWidth
val (updatedLeftWidth, updatedRightWidth) = when {
// Return half of the width for both texts
bothExceedHalfWidth -> maxHalfWidth to maxHalfWidth
// Return as it is
bothAreLowerThanHalfWidth -> leftTextWidth to rightTextWidth
// Get the width for the higher one and return the lower one as it is
anyIsHigherThanHalfWidth -> {
if(leftTextWidth > maxHalfWidth) {
val newLeftWidth = maxWidth - rightTextWidth
newLeftWidth to rightTextWidth
} else {
val newRightWidth = maxWidth - leftTextWidth
leftTextWidth to newRightWidth
}
}
// as it is
else -> leftTextWidth to rightTextWidth
}
// Set left and right text widths and get the whole placeable
val placeable = subcompose(TextSlot.Content) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = leftText,
Modifier
.width(updatedLeftWidth.toDp())
.background(Color.Red)
)
Text(
text = rightText,
Modifier
.width(updatedRightWidth.toDp())
.background(Color.Blue)
)
}
}.first().measure(constraints)
// Set width and height for the whole component
layout(placeable.width, placeable.height) {
// Place the content
placeable.place(0, 0)
}
}
}
enum class TextSlot {
Left, Right, Content
}
robercoding
11/10/2022, 10:50 PMZach Klippenstein (he/him) [MOD]
11/11/2022, 1:48 AMrobercoding
11/11/2022, 6:14 AMDavid Corrado
11/11/2022, 4:40 PMZach Klippenstein (he/him) [MOD]
11/11/2022, 4:41 PMRow
.David Corrado
11/11/2022, 4:43 PMDavid Corrado
11/11/2022, 4:50 PMDavid Corrado
11/11/2022, 8:51 PMDavid Corrado
11/11/2022, 8:53 PMZach Klippenstein (he/him) [MOD]
11/11/2022, 9:49 PMDavid Corrado
11/16/2022, 6:53 PM