Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose basic textfield no cursor

Forgive if I ask something really stupid now but I'm quite new to compose. I'm trying to create a BasicTextField which looks like I want it to but for some reason there is no cursor blinking when I've selected the TextField and the keyboard is showing. I saw it with a normal TextField but I didn't like the looks of the default things. Am I missing a line or something? Thanks

fun CustomSearchField(state: MutableState<TextFieldValue>) {
    val focusManager = LocalFocusManager.current
    val keyboardController = LocalSoftwareKeyboardController.current

    BasicTextField(
        value = state.value,
        onValueChange = { value ->
            state.value = value
        },
        singleLine = true,
        cursorBrush = SolidColor(Color.White),
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = {
                keyboardController?.hide()
                focusManager.clearFocus()
            }
        ),
        decorationBox = {
            Row(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .fillMaxWidth()
                    .heightIn(48.dp)
                    .clip(shape = RoundedCornerShape(8.dp))
                    .border(
                        BorderStroke(
                            1.dp, colorResource(R.color.cardBorder)
                        ), shape = RoundedCornerShape(8.dp)
                    )
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_search_icon),
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
                    modifier = Modifier
                        .padding(start = 16.dp)
                        .size(24.dp)
                )

                if (state.value == TextFieldValue("")) {
                    /*Text(
                        text = "Search",
                        style = poppinsRegular(14),
                        color = colorResource(id = R.color.mainGray),
                        modifier = Modifier
                            .weight(1f)
                    )*/
                } else {
                    Text(
                        text = state.value.text,
                        style = poppinsRegular(14),
                        color = colorResource(id = R.color.primaryTextColor),
                        modifier = Modifier
                            .weight(1f)
                    )
                }

                if (state.value != TextFieldValue("")) {
                    IconButton(
                        onClick = {
                            state.value =
                                TextFieldValue("")
                        }
                    ) {
                        Image(
                            painter = painterResource(id = R.drawable.clear_icon),
                            contentDescription = null,
                            contentScale = ContentScale.Crop,
                            colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
                            modifier = Modifier
                                .size(24.dp)
                                .padding(2.dp)
                        )
                    }
                }
            }
        }
like image 227
Oscar Berggren Avatar asked Feb 12 '26 13:02

Oscar Berggren


1 Answers

There are two issues:

  1. The cursor color is white:
cursorBrush = SolidColor(Color.Black),
  1. decorationBox takes an argument with the text box and text controls. At the decorationBox function, you need to add innerTextField -> to make that the argument name. Then, inside the row, you need to call the composable.

decorationBox = {
    Row(
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .padding(horizontal = 16.dp)
            .fillMaxWidth()
            .heightIn(48.dp)
            .clip(shape = RoundedCornerShape(8.dp))
            .border(
                BorderStroke(
                    1.dp, colorResource(R.color.cardBorder)
                ), shape = RoundedCornerShape(8.dp)
            )
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_search_icon),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
            modifier = Modifier
                .padding(start = 16.dp)
                .size(24.dp)
        )
    
        if (state.value == TextFieldValue("")) {
            /*Text(
                text = "Search",
                style = poppinsRegular(14),
                color = colorResource(id = R.color.mainGray),
                modifier = Modifier
                    .weight(1f)
            )*/
        } else {
            innerTextField() // <--- HERE
        }
    
        if (state.value != TextFieldValue("")) {
            IconButton(
                onClick = {
                    state.value =
                        TextFieldValue("")
                }
            ) {
                Image(
                    painter = painterResource(id = R.drawable.clear_icon),
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
                    modifier = Modifier
                        .size(24.dp)
                        .padding(2.dp)
                )
            }
        }
    }
}
like image 135
eten Avatar answered Feb 15 '26 05:02

eten