Skip to content

CodeTextField

A verification-code input that splits the code into individual cells, mirroring ColorOS's COUICodeInputView: 42x46dp card cells with an 8dp corner radius, a 1.6dp primary-colored stroke on the active cell, digits popping in with a 0.6 -> 1.0 scale + fade, and an optional security mode that shows dots instead of digits. Tapping anywhere focuses the hidden field; typing fills the cells from left to right, pasting distributes the pasted code into the cells, and backspace clears the last cell. A blinking cursor is shown in the active empty cell while focused.

Import

kotlin
import io.github.suqi8.coui.kmp.basic.CodeTextField
import io.github.suqi8.coui.kmp.basic.CodeTextFieldDefaults

Basic Usage

kotlin
var code by remember { mutableStateOf("") }

CodeTextField(
    value = code,
    onValueChange = { code = it.filter(Char::isDigit) },
    onComplete = { submit(it) },
    modifier = Modifier.fillMaxWidth(),
)

Security Mode

kotlin
var pin by remember { mutableStateOf("") }

CodeTextField(
    value = pin,
    onValueChange = { pin = it.filter(Char::isDigit) },
    cellCount = 4,
    security = true,
    modifier = Modifier.fillMaxWidth(),
)

Component States

Disabled State

A disabled field keeps its current code visible but cannot be focused or edited, and the cursor and active-cell stroke are hidden:

kotlin
CodeTextField(
    value = code,
    onValueChange = { code = it },
    enabled = false,
    modifier = Modifier.fillMaxWidth(),
)

Properties

CodeTextField

PropertyTypeDescriptionDefault ValueRequired
valueStringThe code shown in the cells-Yes
onValueChange(String) -> UnitCallback with the sanitized code on change-Yes
modifierModifierModifier applied to the componentModifierNo
cellCountIntNumber of code cellsCodeTextFieldDefaults.CellCount (6)No
enabledBooleanWhether the component is enabledtrueNo
securityBooleanShow dots instead of digitsfalseNo
cellSizeDpSizeSize of one cell before adaptive scalingCodeTextFieldDefaults.CellSizeNo
cellCornerRadiusDpCorner radius of one cellCodeTextFieldDefaults.CellCornerRadiusNo
colorsCodeTextFieldColorsColor configurationCodeTextFieldDefaults.codeTextFieldColors()No
textStyleTextStyleText style of the cell digitsCodeTextFieldDefaults.textStyle()No
keyboardOptionsKeyboardOptionsKeyboard options of the hidden fieldKeyboardOptions(keyboardType = KeyboardType.Number)No
onComplete((String) -> Unit)?Called with the full code once all cells are fillednullNo
interactionSourceMutableInteractionSource?Interaction sourcenullNo

CodeTextFieldDefaults

ConstantTypeDescriptionDefault Value
CellCountIntDefault number of cells6
CellSizeDpSizeSize of one cellDpSize(42.dp, 46.dp)
CellCornerRadiusDpCorner radius of one cell8.dp
MinCellSpacingDpMinimum gap between adjacent cells4.dp
MaxCellSpacingDpMaximum gap between adjacent cells16.dp
CellStrokeWidthDpStroke width of the active-cell highlight1.6.dp
SecurityCircleRadiusDpRadius of the dot shown in security mode5.dp
CursorWidthDpWidth of the blinking cursor2.dp
ReferenceWidthDpReference width below which cells scale down360.dp

Methods

Method NameTypeDescription
textStyle()TextStyleDefault digit style (COUITheme.textStyles.title2 at 30sp, coui_code_input_cell_text_size)
codeTextFieldColors()CodeTextFieldColorsCreates the color configuration for the code text field

codeTextFieldColors() factory

ParameterTypeDefault
cellBackgroundColorColorCOUITheme.colorScheme.surfaceContainer
textColorColorCOUITheme.colorScheme.onSurface
focusedStrokeColorColorCOUITheme.colorScheme.primary
securityCircleColorColorCOUITheme.colorScheme.onSurface (alpha 0.847)
cursorColorColorCOUITheme.colorScheme.primary

Behavior

  • onValueChange always receives the sanitized code: whitespace stripped and truncated to cellCount.
  • A multi-character insertion (paste) replaces the whole code and is distributed into the cells, like COUICodeInputView's TextWatcher.
  • The active cell (first empty one, or the last cell when full) shows a 1.6dp stroke while focused; the stroke fades in over 100ms with a 33ms delay and fades out over 100ms (COUI move ease 0.3, 0, 0.1, 1).
  • Digits appear with a 0.6 -> 1.0 scale + fade over 100ms and fade out with a 33ms delay; security mode skips digit animations, mirroring COUI.
  • Below the 360dp reference width the cells shrink proportionally and the gap between cells adapts within 4dp and 16dp.

Advanced Usage

Custom Cell Style

kotlin
CodeTextField(
    value = code,
    onValueChange = { code = it },
    cellCount = 4,
    cellSize = DpSize(48.dp, 52.dp),
    cellCornerRadius = 12.dp,
    colors = CodeTextFieldDefaults.codeTextFieldColors(
        focusedStrokeColor = COUITheme.colorScheme.secondary,
    ),
    modifier = Modifier.fillMaxWidth(),
)

Changelog

Released under the Apache-2.0 License