Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,30 @@ private object TextEffectiveStateManagement2 {
// [START android_compose_text_link_1]
@Composable
fun AnnotatedStringWithLinkSample() {
// Display a link in the text
// Display multiple links in the text
Text(
buildAnnotatedString {
append("Build better apps faster with ")
append("Go to the ")
withLink(
LinkAnnotation.Url(
"https://developer.android.com/jetpack/compose",
"https://developer.android.com/",
TextLinkStyles(style = SpanStyle(color = Color.Blue))
)
) {
append("Jetpack Compose")
append("Android Developers ")
pop()
append("website, and check out the")
}
withLink(
LinkAnnotation.Url(
"https://developer.android.com/jetpack/compose",
TextLinkStyles(style = SpanStyle(color = Color.Green))
)
) {
append("Compose guidance")
pop()
append(".")
pop()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need the pop operations here, just move the non-linked text out of the withLink() {} lambda.

Text(
        buildAnnotatedString {
            append("Go to the ")
            withLink(
                LinkAnnotation.Url(
                    "https://developer.android.com/",
                    TextLinkStyles(style = SpanStyle(color = Color.Blue))
                )
            ) {
                append("Android Developers ")

            }

            append("website, and check out the")
            withLink(
                LinkAnnotation.Url(
                    "https://developer.android.com/jetpack/compose",
                    TextLinkStyles(style = SpanStyle(color = Color.Green))
                )
            ) {
                append("Compose guidance")
            }
            append(".")
        }
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice, thanks.

}
}
)
Expand Down