KeyboardActions


Configures custom actions to run when the action button on the software keyboard (such as the "Done", "Search", or "Next" button in the bottom corner of the keyboard) is clicked.

Setting these actions overrides default behavior, allowing you to implement custom focus routing (e.g., moving focus to a specific field on Next) or trigger custom logic (e.g., hiding the keyboard or submitting data on Done).

Use KeyboardActionScope.defaultKeyboardAction to run the default action for the triggered ImeAction.

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextObfuscationMode
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

val nameState = rememberTextFieldState()
val emailState = rememberTextFieldState()
val passwordState = rememberTextFieldState()
var isPasswordVisible by remember { mutableStateOf(false) }
val urlState = rememberTextFieldState()
val addressState = rememberTextFieldState()
val biographyState = rememberTextFieldState()

Column(
    verticalArrangement = Arrangement.spacedBy(12.dp),
    modifier = Modifier.padding(16.dp).fillMaxWidth(),
) {
    // 1. PersonName Input (Auto-Capitalizes Words)
    OutlinedTextField(
        state = nameState,
        label = { Text("Full Name") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.PersonName,
                capitalization = KeyboardCapitalization.Words,
                imeAction = ImeAction.Next, // Soft keyboard automatically moves focus on Next
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 2. Email Input (Restricted layout showing '@' and '.', auto-capitalization disabled)
    OutlinedTextField(
        state = emailState,
        label = { Text("Email") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Email,
                capitalization = KeyboardCapitalization.None,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 3. Password Input with Eye-Icon Toggle (masked vs visible password layouts,
    // capitalizations disabled)
    Row(verticalAlignment = Alignment.CenterVertically) {
        OutlinedSecureTextField(
            state = passwordState,
            label = { Text("Password") },
            textObfuscationMode =
                if (isPasswordVisible) TextObfuscationMode.Visible
                else TextObfuscationMode.System,
            keyboardOptions =
                KeyboardOptions(
                    keyboardType =
                        if (isPasswordVisible) KeyboardType.PasswordVisible
                        else KeyboardType.Password,
                    capitalization = KeyboardCapitalization.None,
                    imeAction = ImeAction.Next,
                ),
            modifier = Modifier.weight(0.7f),
        )
        Spacer(Modifier.width(8.dp))
        TextButton(
            onClick = { isPasswordVisible = !isPasswordVisible },
            modifier = Modifier.weight(0.3f),
        ) {
            Text(if (isPasswordVisible) "Hide" else "Show")
        }
    }

    // 4. Uri Input (Prominent '/' and '.com' keys, auto-capitalization disabled)
    OutlinedTextField(
        state = urlState,
        label = { Text("Homepage URL") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Uri,
                capitalization = KeyboardCapitalization.None,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 5. PostalAddress Input (Auto-Capitalizes Words, prompts standard shipping keys)
    OutlinedTextField(
        state = addressState,
        label = { Text("Shipping Address") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.PostalAddress,
                capitalization = KeyboardCapitalization.Words,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 6. Free-Text Biography Input (Auto-Capitalizes Sentences, multi-line active)
    OutlinedTextField(
        state = biographyState,
        label = { Text("Biography") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Text,
                capitalization = KeyboardCapitalization.Sentences,
                imeAction =
                    ImeAction.Done, // Soft keyboard automatically dismisses keyboard on Done
            ),
        modifier = Modifier.fillMaxWidth(),
    )
}
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

val emailState = rememberTextFieldState()
val passwordState = rememberTextFieldState()

Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
    OutlinedTextField(
        state = emailState,
        label = { Text("Email Address") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Email,
                capitalization = KeyboardCapitalization.None,
                imeAction =
                    ImeAction
                        .Next, // Soft keyboard automatically shifts focus to password field on
                // Next
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    OutlinedSecureTextField(
        state = passwordState,
        label = { Text("Password") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Password,
                capitalization = KeyboardCapitalization.None,
                imeAction =
                    ImeAction.Done, // Soft keyboard automatically dismisses the keyboard on Done
            ),
        modifier = Modifier.fillMaxWidth(),
    )
}

Summary

Public companion properties

KeyboardActions

Use this default value if you don't want to specify any action but want to use the default action implementations.

Cmn

Public constructors

KeyboardActions(
    onDone: (KeyboardActionScope.() -> Unit)?,
    onGo: (KeyboardActionScope.() -> Unit)?,
    onNext: (KeyboardActionScope.() -> Unit)?,
    onPrevious: (KeyboardActionScope.() -> Unit)?,
    onSearch: (KeyboardActionScope.() -> Unit)?,
    onSend: (KeyboardActionScope.() -> Unit)?
)
Cmn

Public functions

open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn

Public properties

(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Done action.

Cmn
(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Go action.

Cmn
(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Next action.

Cmn
(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Previous action.

Cmn
(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Search action.

Cmn
(KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Send action.

Cmn

Public companion properties

Default

val DefaultKeyboardActions

Use this default value if you don't want to specify any action but want to use the default action implementations.

Public constructors

KeyboardActions

KeyboardActions(
    onDone: (KeyboardActionScope.() -> Unit)? = null,
    onGo: (KeyboardActionScope.() -> Unit)? = null,
    onNext: (KeyboardActionScope.() -> Unit)? = null,
    onPrevious: (KeyboardActionScope.() -> Unit)? = null,
    onSearch: (KeyboardActionScope.() -> Unit)? = null,
    onSend: (KeyboardActionScope.() -> Unit)? = null
)

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

Public properties

onDone

val onDone: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Done action. A null value indicates that the default implementation if any, should be executed.

onGo

val onGo: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Go action. A null value indicates that the default implementation if any, should be executed.

onNext

val onNext: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Next action. A null value indicates that the default implementation should be executed. The default implementation moves focus to the next item in the focus traversal order.

See Modifier.focusProperties() for more details on how to specify a custom focus order if needed.

onPrevious

val onPrevious: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Previous action. A null value indicates that the default implementation should be executed. The default implementation moves focus to the previous item in the focus traversal order.

See Modifier.focusProperties() for more details on how to specify a custom focus order if needed.

onSearch

val onSearch: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Search action. A null value indicates that the default implementation if any, should be executed.

onSend

val onSend: (KeyboardActionScope.() -> Unit)?

This is run when the user triggers the Send action. A null value indicates that the default implementation if any, should be executed.