0

I am new to jetpack compose and the app Im working on has to have the end of the text to be in the middle of the 9th line. Is there a way to do that? So far its only been able to get the ellipsis to the end of the 9th line. I tried to do 8.5 but that would not work.

This is what I want: This is what I want

This is what I have:

This is what I have

My code:

@Composable
fun ExpandableText(text: String) {
    var expanded by rememberSaveable { mutableStateOf(false) }
    val maxLines = if (expanded) Int.MAX_VALUE else 9
    Text(
        modifier = Modifier.padding(top = 16.dp),
        text = text,
        maxLines = maxLines,
        overflow = TextOverflow.Ellipsis,
        style = MaterialTheme.typography.body1,
        color = MaterialTheme.ts.colors.onBackgroundSecondary,
        lineHeight = 20.sp
    )
    Text(
        modifier = Modifier
            .padding(top = 16.dp, bottom = 40.dp)
            .clickable { expanded = !expanded },
        text = stringResource(id = R.string.see_more),
        style = MaterialTheme.typography.body2,
        color = MaterialTheme.ts.colors.actionColor
    )
}
Android Dev
  • 305
  • 3
  • 18
  • Have you tried wrapping both inside a `Row` with the second `Text` without top padding? – Darshan Jun 24 '22 at 18:36
  • @DarShan that is a really really good suggestion! let me try this! – Android Dev Jun 24 '22 at 18:37
  • @DarShan Doesn't work... the See More text disappears and the first text still looks the same – Android Dev Jun 24 '22 at 18:50
  • You can check this: https://proandroiddev.com/expandabletext-in-jetpack-compose-b924ea424774 – Darshan Jun 24 '22 at 18:52
  • @DarShan Oh thats fantastic, thank you! I will try that right now – Android Dev Jun 24 '22 at 21:31
  • @DarShan doesn't work :( Because I need that Show More to be blue and a different style, so it has to be a separate Text(), that example just has 1 long text. plus that code does not expand properly. When I click Show More, it just makes the text smaller, not expanded. – Android Dev Jun 24 '22 at 23:54
  • Actually nevermind I take that back, I think theres a hacky way I can do this – Android Dev Jun 25 '22 at 00:03
  • 1
    this is probably what you want : https://stackoverflow.com/a/67244131/4252352 - basically you need to build an `AnnotatedString` .. if you want to do this properly. – Mark Jun 25 '22 at 02:07
  • @Mark holy crap!!!!! this is great!!!! thank you so much! Im gonna try this – Android Dev Jun 25 '22 at 20:21

0 Answers0