14

For the views declared in XML we could use SpannableStringBuilder as mentioned here https://stackoverflow.com/a/4897412/9715339 to color that partial string.

But with JetPack compose Text I am not able to achieve the same with only single Text.

I want something like this.

Partial color text

As you can see only "Sign up" text has different color and Also I would like to make it clickable.

This is how my text code looks at the moment

Text(text = "Don't have an account? Sign Up",
                        modifier = Modifier.align(Alignment.BottomCenter),
                        style = MaterialTheme.typography.h6,
                        color = MaterialTheme.colors.secondary,
                    )

Is this possible in jetpack compose?

Edric
  • 24,639
  • 13
  • 81
  • 91
Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41

1 Answers1

24

So with the help of @CommonsWare's comment and the Compose documentation, I managed to create the same using AnnotatedString & ClickableText. Comments are added inline for anyone to understand.

@Composable
fun AnnotatedClickableText() {
    val annotatedText = buildAnnotatedString {
        //append your initial text
        withStyle(
            style = SpanStyle(
                color = Color.Gray,
            )
        ) {
            append("Don't have an account? ")

        }

        //Start of the pushing annotation which you want to color and make them clickable later
        pushStringAnnotation(
            tag = "SignUp",// provide tag which will then be provided when you click the text
            annotation = "SignUp"
        )
        //add text with your different color/style
        withStyle(
            style = SpanStyle(
                color = Color.Red,
            )
        ) {
            append("Sign Up")
        }
        // when pop is called it means the end of annotation with current tag
        pop()
    }

    ClickableText(
        text = annotatedText,
        onClick = { offset ->
            annotatedText.getStringAnnotations(
                tag = "SignUp",// tag which you used in the buildAnnotatedString
                start = offset,
                end = offset
            )[0].let { annotation ->
                //do your stuff when it gets clicked
                Log.d("Clicked", annotation.item)
            }
        }
    )
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
  • 10
    Thanks for providing this example. Currently clicking the text without a tag will throw `java.lang.IndexOutOfBoundsException: Index: 0, Size: 0` . To correct this, you would do the following as indicated by [these docs](https://developer.android.com/jetpack/compose/text#click-with-annotation) and use `.firstOrNull()?.let { annotation`. – kelvin Sep 04 '21 at 08:32
  • Thanks for the comment above `pop()` "current tag". I've faced an issue when using multiple pushes whatever i clicked it only trigger the first pushed item. – adwardwo1f Nov 30 '21 at 09:04
  • Instead of [0].let, use .forEach to handle more than one annotation – Sean Jul 07 '22 at 17:08