Skip to content

Commit 6846c24

Browse files
authored
Add clickable text snippets (#297)
1 parent f3f38d4 commit 6846c24

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ import androidx.compose.ui.graphics.Brush
4242
import androidx.compose.ui.graphics.Color
4343
import androidx.compose.ui.graphics.Color.Companion.Cyan
4444
import androidx.compose.ui.graphics.Shadow
45+
import androidx.compose.ui.platform.LocalUriHandler
4546
import androidx.compose.ui.res.stringResource
4647
import androidx.compose.ui.text.AnnotatedString
48+
import androidx.compose.ui.text.LinkAnnotation
4749
import androidx.compose.ui.text.ParagraphStyle
4850
import androidx.compose.ui.text.PlatformTextStyle
4951
import androidx.compose.ui.text.SpanStyle
52+
import androidx.compose.ui.text.TextLinkStyles
5053
import androidx.compose.ui.text.TextStyle
5154
import androidx.compose.ui.text.buildAnnotatedString
5255
import androidx.compose.ui.text.font.Font
@@ -58,6 +61,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
5861
import androidx.compose.ui.text.style.LineHeightStyle
5962
import androidx.compose.ui.text.style.TextAlign
6063
import androidx.compose.ui.text.style.TextOverflow
64+
import androidx.compose.ui.text.withLink
6165
import androidx.compose.ui.text.withStyle
6266
import androidx.compose.ui.unit.dp
6367
import androidx.compose.ui.unit.em
@@ -577,6 +581,50 @@ private object TextEffectiveStateManagement2 {
577581
// [END android_compose_text_state_management]
578582
}
579583

584+
// [START android_compose_text_link_1]
585+
@Composable
586+
fun AnnotatedStringWithLinkSample() {
587+
// Display a link in the text
588+
Text(
589+
buildAnnotatedString {
590+
append("Build better apps faster with ")
591+
withLink(
592+
LinkAnnotation.Url(
593+
"https://developer.android.com/jetpack/compose",
594+
TextLinkStyles(style = SpanStyle(color = Color.Blue))
595+
)
596+
) {
597+
append("Jetpack Compose")
598+
}
599+
}
600+
)
601+
}
602+
// [END android_compose_text_link_1]
603+
604+
// [START android_compose_text_link_2]
605+
@Composable
606+
fun AnnotatedStringWithListenerSample() {
607+
// Display a link in the text and log metrics whenever user clicks on it. In that case we handle
608+
// the link using openUri method of the LocalUriHandler
609+
val uriHandler = LocalUriHandler.current
610+
Text(
611+
buildAnnotatedString {
612+
append("Build better apps faster with ")
613+
val link =
614+
LinkAnnotation.Url(
615+
"https://developer.android.com/jetpack/compose",
616+
TextLinkStyles(SpanStyle(color = Color.Blue))
617+
) {
618+
val url = (it as LinkAnnotation.Url).url
619+
// log some metrics
620+
uriHandler.openUri(url)
621+
}
622+
withLink(link) { append("Jetpack Compose") }
623+
}
624+
)
625+
}
626+
// [END android_compose_text_link_2]
627+
580628
private val firaSansFamily = FontFamily()
581629

582630
val LightBlue = Color(0xFF0066FF)

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ androidx-compose-material3-adaptive-navigation = { module = "androidx.compose.ma
7373
androidx-compose-material3-adaptive-navigation-suite = { module = "androidx.compose.material3:material3-adaptive-navigation-suite", version.ref = "material3-adaptive-navigation-suite" }
7474
androidx-compose-runtime = { module = "androidx.compose.runtime:runtime" }
7575
androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata" }
76-
androidx-compose-ui = { module = "androidx.compose.ui:ui" }
76+
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose-latest" }
7777
androidx-compose-ui-googlefonts = { module = "androidx.compose.ui:ui-text-google-fonts" }
7878
androidx-graphics-shapes = "androidx.graphics:graphics-shapes:1.0.0-beta01"
7979
androidx-compose-ui-graphics = { module = "androidx.compose.ui:ui-graphics" }

0 commit comments

Comments
 (0)