ClickableText

Functions summary

Unit
@Composable
ClickableText(
    text: AnnotatedString,
    modifier: Modifier,
    style: TextStyle,
    softWrap: Boolean,
    overflow: TextOverflow,
    maxLines: Int,
    onTextLayout: (TextLayoutResult) -> Unit,
    onClick: (Int) -> Unit
)

This function is deprecated. Use Text or BasicText and pass an AnnotatedString that contains a LinkAnnotation.

Cmn

Functions

@Composable
fun ClickableText(
    text: AnnotatedString,
    modifier: Modifier = Modifier,
    style: TextStyle = TextStyle.Default,
    softWrap: Boolean = true,
    overflow: TextOverflow = TextOverflow.Clip,
    maxLines: Int = Int.MAX_VALUE,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    onClick: (Int) -> Unit
): Unit

A continent version of BasicText component to be able to handle click event on the text.

This is a shorthand of BasicText with pointerInput to be able to handle click event easily.

import androidx.compose.foundation.text.ClickableText
import androidx.compose.material.Text
import androidx.compose.ui.text.AnnotatedString

ClickableText(
    text = AnnotatedString("Click Me"),
    onClick = { offset -> Log.d("ClickableText", "$offset -th character is clicked.") },
)

For other gestures, e.g. long press, dragging, follow sample code.

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.TextLayoutResult

val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }
val gesture =
    Modifier.pointerInput(onLongClick) {
        detectTapGestures(
            onLongPress = { pos ->
                layoutResult.value?.let { layout ->
                    onLongClick(layout.getOffsetForPosition(pos))
                }
            }
        )
    }

Text(
    text = text,
    modifier = modifier.then(gesture),
    style = style,
    softWrap = softWrap,
    overflow = overflow,
    maxLines = maxLines,
    onTextLayout = {
        onTextLayout(it)
        layoutResult.value = it
    },
)
Parameters
text: AnnotatedString

The text to be displayed.

modifier: Modifier = Modifier

Modifier to apply to this layout node.

style: TextStyle = TextStyle.Default

Style configuration for the text such as color, font, line height etc.

softWrap: Boolean = true

Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects.

overflow: TextOverflow = TextOverflow.Clip

How visual overflow should be handled.

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero.

onTextLayout: (TextLayoutResult) -> Unit = {}

Callback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw selection around the text.

onClick: (Int) -> Unit

Callback that is executed when users click the text. This callback is called with clicked character's offset.

Note: this composable is now deprecated. Instead, use regular androidx.compose.material3.Text or androidx.compose.foundation.text.BasicText and pass an AnnotatedString that contains a androidx.compose.ui.text.LinkAnnotation.