dgmltn
dgmltn

Reputation: 3903

Jetpack Compose - SelectionContainer - how to add missing functionality?

I'm trying to replicate some View-based selected text behavior with Jetpack Compose. In both of these cases, I've long-tapped on the middle of the URL.

View based:

<TextView ... android:textIsSelectable="true" />

view-based-screenshot

Compose:

    SelectionContainer {
        Text(...)
    }

compose-based-screenshot

As you can see in the screenshots, there are a few missing options in the Compose layout. Is there a way to add this? Yes, I know I can use AndroidView but is there a more composey way to do it? Here's what's in the View layout, but lacking from the Compose layout:

Edit: I'm not asking for a custom toolbar, I'm asking for the one Google provides in TextView.

Upvotes: 18

Views: 3557

Answers (2)

Alexandr Morozov
Alexandr Morozov

Reputation: 1

 val localTextToolbar = LocalTextToolbar.current

  localTextToolbar.showMenu(
                    rect = Rect.Zero,
                    onCopyRequested = {},
                    onPasteRequested =  {},
                    onCutRequested = {},
                    onSelectAllRequested = {})

Upvotes: 0

Eliza Camber
Eliza Camber

Reputation: 1642

While this is still not supported, if you use a TextField instead of a Text you at least get the "Select all" option too.

@Composable
private fun SelectableTextField() {
    TextField(
        value = "This is a selectable text",
        onValueChange = {},
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        readOnly = true,
        colors = TextFieldDefaults.colors(
            focusedContainerColor = Color.Transparent,
            unfocusedContainerColor = Color.Transparent,
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent
        )
    )
}

Upvotes: 1

Related Questions