Search code examples
androidandroid-jetpack-composeandroid-compose-textfield

How to hide TextFieldCursorHandle in Jetpack compose for TextField/BasicTextField?


The solution I have so far is to use a Transparent Color for the cursor.
I am looking for a better way to hide it if there is any.

cursorBrush = SolidColor(Transparent)

TextField should be focused, the keyboard should be open and the user should be able to type input.

Screenshot

The problem with this is I can still see the TextFieldCursorHandle after entering text.

enter image description here


Solution

    • In the BasicTextField you can hide the cursor using cursorBrush = SolidColor(Unspecified).
    • In the TextFieldyou can use the attribute colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)

    The TextFieldCursorHandle and the selected text use the color provided by LocalTextSelectionColors.current You override this color defining a custom TextSelectionColors:

    val customTextSelectionColors = TextSelectionColors(
        handleColor = Color.Transparent,
        backgroundColor = Color.Transparent
    )
    
    CompositionLocalProvider(LocalTextSelectionColors provides customTextSelectionColors) {
       BasicTextField(
           value = text,
           onValueChange = {text = it},
           cursorBrush = SolidColor(Unspecified)
       )
    
       TextField(
           value = text,
           onValueChange = {text = it},
           colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)
       )
    
    }