Farhad7
Farhad7

Reputation: 163

Android: Issue with Samsung Keyboard Auto-Replace in Jetpack Compose TextField

I'm experiencing a strange issue with the Samsung keyboard in a Jetpack Compose TextField. When Auto replace and Suggest Text Correction are enabled, sometimes the suggested text from the keyboard gets entered on a new line when I type quickly and press the Enter key. This issue doesn't appear in the SMS app or other applications, so the problem seems to be with my application.

I am using the following version of Material3:

implementation "androidx.compose.material3:material3:1.1.2"
TextField(
    value = textField,
    modifier = Modifier
        .background(
            color = SampleTheme.customColors.background,
            shape = RoundedCornerShape(size = 32.dp)
        )
        .padding(start = 6.dp, end = 6.dp)
        .animateContentSize()
        .wrapContentHeight()
        .weight(0.7f)
        .focusRequester(focusRequester),
    colors = TextFieldDefaults.colors(
        focusedContainerColor = Color.Transparent,
        unfocusedContainerColor = Color.Transparent,
        disabledContainerColor = Color.Transparent,
        cursorColor = SampleTheme.customColors.colorAccent,
        focusedIndicatorColor = Color.Transparent,
        unfocusedIndicatorColor = Color.Transparent,
        selectionColors = TextSelectionColors(
            handleColor = SampleTheme.customColors.colorAccent,
            backgroundColor = SampleTheme.customColors.colorAccent.copy(
                alpha = 0.4f
            )
        )
    ),
    onValueChange = {
        textField = it
        viewModel.onTextChange(it)
    },
    shape = RoundedCornerShape(8.dp),
    placeholder = {
        Text(
            text = stringResource(R.string.message_input_placeholder),
            style = TextStyle(
                fontSize = 12.sp,
                fontFamily = TestFont().iransans,
                fontWeight = FontWeight.Light,
                color = SampleTheme.customColors.hintColor,
            )
        )
    },
    textStyle = TextStyle(
        fontSize = 14.sp,
        color = SampleTheme.customColors.textColor,
        fontWeight = FontWeight.Normal,
        fontFamily = TestFont().iransans,
        textDirection = TextDirection.ContentOrLtr
    ),
    trailingIcon = {
        // Keyboard
        AnimatedVisibility(
            visible = state.emojiPicker,
            enter = fadeIn(),
            exit = fadeOut(),
        ) {
            KeyboardIcon(onClick = {
                controller?.show()
                viewModel.switchEmojiPickerState(
                    HistoryUiIntent.SwitchEmojiPickerState
                )
            })
        }
        // Emoji
        AnimatedVisibility(
            visible = !state.emojiPicker,
            enter = fadeIn(),
            exit = fadeOut()
        ) {
            SmileyIcon(onClick = {
                controller?.hide()
                viewModel.switchEmojiPickerState(
                    HistoryUiIntent.SwitchEmojiPickerState
                )
            })
        }
    },
)

And the textField variable:

var textField by remember {
    mutableStateOf(
        TextFieldValue(
            DraftHelper.initialTextFieldValue(
                threadModel?.id ?: 0
            )
        )
    )
}

Does anyone know why this might be happening and how to fix it?

wrong auto correction gif

Upvotes: 1

Views: 71

Answers (0)

Related Questions