Add sample of links styling to material text
Also updated ui-text samples to include link styling since there's no default styling
Fixes: 339847447
Test: demo
Change-Id: Id926083381c269d88f5a004d5bc512a611e2dfc4
diff --git a/compose/material/material/samples/src/main/java/androidx/compose/material/samples/TextSamples.kt b/compose/material/material/samples/src/main/java/androidx/compose/material/samples/TextSamples.kt
new file mode 100644
index 0000000..6ed29c7
--- /dev/null
+++ b/compose/material/material/samples/src/main/java/androidx/compose/material/samples/TextSamples.kt
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.compose.material.samples
+
+import androidx.annotation.Sampled
+import androidx.compose.material.MaterialTheme
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.text.LinkAnnotation
+import androidx.compose.ui.text.SpanStyle
+import androidx.compose.ui.text.TextLinkStyles
+import androidx.compose.ui.text.buildAnnotatedString
+import androidx.compose.ui.text.style.TextDecoration
+import androidx.compose.ui.text.withLink
+
+@Sampled
+@Composable
+fun TextWithLinks() {
+ val url = "https://developer.android.com/jetpack/compose"
+
+ val linkColor = MaterialTheme.colors.primary
+ val linkStyle = SpanStyle(color = linkColor, textDecoration = TextDecoration.Underline)
+
+ val annotatedString = buildAnnotatedString {
+ append("Build better apps faster with ")
+ withLink(
+ LinkAnnotation.Url(url = url, styles = TextLinkStyles(style = linkStyle))
+ ) {
+ append("Jetpack Compose")
+ }
+ }
+ // Note that if your string is defined in resources, you can pass the same link style object
+ // when constructing the AnnotatedString using the AnnotatedString.fromHtml method.
+ Text(annotatedString)
+}
diff --git a/compose/material/material/src/commonMain/kotlin/androidx/compose/material/Text.kt b/compose/material/material/src/commonMain/kotlin/androidx/compose/material/Text.kt
index 5fc4f0d..b527f7b 100644
--- a/compose/material/material/src/commonMain/kotlin/androidx/compose/material/Text.kt
+++ b/compose/material/material/src/commonMain/kotlin/androidx/compose/material/Text.kt
@@ -222,6 +222,9 @@
* [Text] or element containing this [Text] to adapt to different background colors and still
* maintain contrast and accessibility.
*
+ * See an example of displaying text with links where links apply the styling from the theme:
+ * @sample androidx.compose.material.samples.TextWithLinks
+ *
* @param text The text to be displayed.
* @param modifier [Modifier] to apply to this layout node.
* @param color [Color] to apply to the text. If [Color.Unspecified], and [style] has no color set,
diff --git a/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/TextSamples.kt b/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/TextSamples.kt
new file mode 100644
index 0000000..39f454ee
--- /dev/null
+++ b/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/TextSamples.kt
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.compose.material3.samples
+
+import androidx.annotation.Sampled
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.text.LinkAnnotation
+import androidx.compose.ui.text.SpanStyle
+import androidx.compose.ui.text.TextLinkStyles
+import androidx.compose.ui.text.buildAnnotatedString
+import androidx.compose.ui.text.style.TextDecoration
+import androidx.compose.ui.text.withLink
+import androidx.compose.ui.tooling.preview.Preview
+
+@Preview(showBackground = true)
+@Sampled
+@Composable
+fun TextWithLinks() {
+ val url = "https://developer.android.com/jetpack/compose"
+
+ val linkColor = MaterialTheme.colorScheme.primary
+ val linkStyle = SpanStyle(color = linkColor, textDecoration = TextDecoration.Underline)
+
+ val annotatedString = buildAnnotatedString {
+ append("Build better apps faster with ")
+ withLink(
+ LinkAnnotation.Url(url = url, styles = TextLinkStyles(style = linkStyle))
+ ) {
+ append("Jetpack Compose")
+ }
+ }
+ // Note that if your string is defined in resources, you can pass the same link style object
+ // when constructing the AnnotatedString using the AnnotatedString.fromHtml method.
+ Text(annotatedString)
+}
diff --git a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Text.kt b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Text.kt
index 0930a98..a26609f 100644
--- a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Text.kt
+++ b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Text.kt
@@ -199,6 +199,9 @@
* Additionally, for [color], if [color] is not set, and [style] does not have a color, then
* [LocalContentColor] will be used.
*
+ * See an example of displaying text with links where links apply the styling from the theme:
+ * @sample androidx.compose.material3.samples.TextWithLinks
+ *
* @param text the text to be displayed
* @param modifier the [Modifier] to be applied to this layout node
* @param color [Color] to apply to the text. If [Color.Unspecified], and [style] has no color set,
diff --git a/compose/ui/ui-text/samples/src/main/java/androidx/compose/ui/text/samples/AnnotatedStringBuilderSamples.kt b/compose/ui/ui-text/samples/src/main/java/androidx/compose/ui/text/samples/AnnotatedStringBuilderSamples.kt
index b864ae0..3088ab3 100644
--- a/compose/ui/ui-text/samples/src/main/java/androidx/compose/ui/text/samples/AnnotatedStringBuilderSamples.kt
+++ b/compose/ui/ui-text/samples/src/main/java/androidx/compose/ui/text/samples/AnnotatedStringBuilderSamples.kt
@@ -177,7 +177,12 @@
BasicText(
buildAnnotatedString {
append("Build better apps faster with ")
- withLink(LinkAnnotation.Url("https://developer.android.com/jetpack/compose")) {
+ withLink(
+ LinkAnnotation.Url(
+ "https://developer.android.com/jetpack/compose",
+ TextLinkStyles(style = SpanStyle(color = Color.Blue))
+ )
+ ) {
append("Jetpack Compose")
}
}
@@ -194,6 +199,7 @@
val link = LinkAnnotation.Url(
"https://developer.android.com/jetpack/compose",
TextLinkStyles(
+ style = SpanStyle(color = Color.Blue),
hoveredStyle = SpanStyle(textDecoration = TextDecoration.Underline)
)
)
@@ -212,7 +218,8 @@
buildAnnotatedString {
append("Build better apps faster with ")
val link = LinkAnnotation.Url(
- "https://developer.android.com/jetpack/compose"
+ "https://developer.android.com/jetpack/compose",
+ TextLinkStyles(SpanStyle(color = Color.Blue))
) {
val url = (it as LinkAnnotation.Url).url
// log some metrics