Home > Net >  Change color of selection text in SelectionContainer in compose
Change color of selection text in SelectionContainer in compose

Time:10-11

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?

CodePudding user response:

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

  • Related