Support multiple links in a single string of text
Stay organized with collections
Save and categorize content based on your preferences.
You can support multiple links in a single string of text to perform different actions when clicking a subsection of text.
Results
Version compatibility
This implementation requires that your project minSDK be set to API level 21 or higher.
Dependencies
Display multiple links in a single string
This snippet embeds multiple clickable links into a single string of text:
@Composable
funAnnotatedStringWithLinkSample(){
// Display multiple links in the text
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(".")
}
)
}
Key points about the code
- Uses the
buildAnnotatedStringfunction to create an annotated string of text. - Specifies the link and text styling by passing them as arguments of the
LinkAnnotation.Url()function (itself passed as an argument of thewithLink()function). A click listener is built intoLinkAnnotation.Url(). - Adds text using
append()in the body of thewithLinkfunction. - Repeats this process to add another linked text segment.
Collections that contain this guide
This guide is part of these curated Quick Guide collections that cover broader Android development goals: