Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jetpack Compose cannot change BasicTextField cursor thumb color

Currently I'm working with android jetpack compose BasicTextField. And when I change cursor color, I expect the cursor handle to be changed with the same color as well. But it turns out with a different color. Is this a bug or I did set something wrong?

Here is what I have set

colors = TextFieldDefaults.textFieldColors(
    backgroundColor = Color.Transparent,
    focusedIndicatorColor = colorResource(id = R.color.accent),
    unfocusedIndicatorColor = colorResource(id = R.color.lightest_grey),
    focusedLabelColor = colorResource(id = R.color.secondary_20),
    unfocusedLabelColor = colorResource(id = R.color.light_grey),
    textColor = colorResource(id = R.color.secondary),
    cursorColor = colorResource(id = R.color.secondary),
  )

enter image description here

like image 437
TRose Avatar asked Dec 21 '25 20:12

TRose


2 Answers

You have to provide a custom TextSelectionColors.

Something like:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Green,
    backgroundColor = Red
)

CompositionLocalProvider(LocalTextSelectionColors provides customTextSelectionColors) {
    BasicTextField(
        value = text,
        onValueChange = {text = it},
        cursorBrush = SolidColor(Black)
    )
}

enter image description here enter image description here

like image 157
Gabriele Mariotti Avatar answered Dec 23 '25 10:12

Gabriele Mariotti


CompositionLocalProvider(
    LocalTextSelectionColors provides TextSelectionColors(
        handleColor = /*your custom color*/,
        backgroundColor = /*your custom color*/
    )
) {
//your composable
}
like image 34
deved roy Avatar answered Dec 23 '25 12:12

deved roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!