https://kotlinlang.org logo
#compose
Title
# compose
d

Divyansh Kushwaha

03/13/2024, 5:38 PM
I have an expandable text in LazyColumn with reverseLayout, when I click expand, the text expands upwards. I want it to expand downwards. Any ideas how can I achieve this?
z

Zach Klippenstein (he/him) [MOD]

03/13/2024, 5:41 PM
Please keep long code snippets to the thread to keep the main channel readable. Thank you.
d

Divyansh Kushwaha

03/13/2024, 5:43 PM
Copy code
@Composable
fun Content() {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(
            space = 16.dp,
            alignment = <http://Alignment.Top|Alignment.Top>
        ),
        contentPadding = PaddingValues(8.dp),
        reverseLayout = true
    ) {
        items(50) {
            TextMessage(text = text)
        }
    }
}

@Composable
fun TextMessage(
    modifier: Modifier = Modifier,
    text: String,
    minimizedMaxLines: Int = 3
) {
    if (text.isNotEmpty()) {
        var expanded by remember { mutableStateOf(false) }
        Box(
            modifier = modifier
                .shadow(2.dp, RoundedCornerShape(8.dp))
                .clip(RoundedCornerShape(8.dp))
                .background(Color(0xFFCDFFF8))
                .padding(8.dp),
        ) {
            Row(
                modifier = Modifier.fillMaxSize(),
                verticalAlignment = Alignment.Bottom
            ) {
                SelectionContainer {
                    Column {
                        Text(
                            text = text,
                            maxLines = if (expanded) Int.MAX_VALUE else minimizedMaxLines,
                            overflow = TextOverflow.Ellipsis,
                            onTextLayout = { layoutResult ->
                                expanded = !layoutResult.hasVisualOverflow
                            }
                        )
                        if (!expanded) {
                            Text(
                                text = "See more",
                                modifier = Modifier.clickable {
                                    expanded = true
                                },
                                style = MaterialTheme.typography.labelSmall,
                                color = Color.Green
                            )
                        }
                    }
                }
            }
        }
    }
}
@Zach Klippenstein (he/him) [MOD] Done! Thanks.