1

I want to change the color of the SelectionContainer but I don't see any option in the parameter section to make a change here. I've also took a look at the internal code and I couldn't see anything related with colors. Docs doesn't mention nothing about this.

Question: Is there any way to change the selection color or isn't that possible?

Barrufet
  • 495
  • 1
  • 11
  • 33
  • Check https://stackoverflow.com/questions/68779305/how-to-change-textfields-highlighted-text-color-on-jetpack-compose/68780835#68780835 – Gabriele Mariotti Oct 11 '21 at 11:19

1 Answers1

4

You can use TextSelectionColors to set the color of the selection.

To do this you need to wrap your Text in a CompositionLocalProvider. The colors used for text selection by text and text field components are provided by LocalTextSelectionColors.current.

You can provide a custom TextSelectionColors using:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Red,
    backgroundColor = Red.copy(alpha = 0.4f)
)

CompositionLocalProvider(LocalTextSelectionColors provides customTextSelectionColors) {
    SelectionContainer {
        Text(
            text = coin.description,
            style = MaterialTheme.typography.body2
        )
    }
}

P.S. Part of the question was copied from this SO answer - link

clamentjohn
  • 3,417
  • 2
  • 18
  • 42