{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

SoftwareKeyboardController

@ExperimentalComposeUiApi
public interface SoftwareKeyboardController


Provide software keyboard control.

Summary

Public methods

abstract void

Hide the software keyboard.

default void

This method is deprecated. Use hide instead.

abstract void

Request that the system show a software keyboard.

default void

This method is deprecated. Use show instead.

Public methods

hide

abstract void hide()

Hide the software keyboard.

This request is best effort, if the system cannot hide the software keyboard this call will silently be ignored.

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp

val keyboardController = LocalSoftwareKeyboardController.current

// used to ensure a TextField is focused when showing keyboard
val focusRequester = remember { FocusRequester() }
var (text, setText) = remember {
    mutableStateOf("Close keyboard on done ime action (blue ✔️)")
}
Column(Modifier.padding(16.dp)) {
    BasicTextField(
        text,
        setText,
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = { keyboardController?.hide() }
        ),
        modifier = Modifier
            .focusRequester(focusRequester)
            .fillMaxWidth()
    )
    Spacer(Modifier.height(16.dp))
    Button(
        onClick = {
            focusRequester.requestFocus()
            keyboardController?.show()
        },
        modifier = Modifier.fillMaxWidth()
    ) {
        Text("Show software keyboard.")
    }
}

Calling this function is considered a side-effect and should not be called directly from recomposition.

hideSoftwareKeyboard

default void hideSoftwareKeyboard()
See also
hide

show

abstract void show()

Request that the system show a software keyboard.

This request is best effort. If the system can currently show a software keyboard, it will be shown. However, there is no guarantee that the system will be able to show a software keyboard. If the system cannot show a software keyboard currently, this call will be silently ignored.

The software keyboard will never show if there is no composable that will accept text input, such as a TextField when it is focused. You may find it useful to ensure focus when calling this function.

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp

val keyboardController = LocalSoftwareKeyboardController.current

// used to ensure a TextField is focused when showing keyboard
val focusRequester = remember { FocusRequester() }
var (text, setText) = remember {
    mutableStateOf("Close keyboard on done ime action (blue ✔️)")
}
Column(Modifier.padding(16.dp)) {
    BasicTextField(
        text,
        setText,
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = { keyboardController?.hide() }
        ),
        modifier = Modifier
            .focusRequester(focusRequester)
            .fillMaxWidth()
    )
    Spacer(Modifier.height(16.dp))
    Button(
        onClick = {
            focusRequester.requestFocus()
            keyboardController?.show()
        },
        modifier = Modifier.fillMaxWidth()
    ) {
        Text("Show software keyboard.")
    }
}

You do not need to call this function unless you also call hide, as the keyboard is automatically shown and hidden by focus events in the BasicTextField.

Calling this function is considered a side-effect and should not be called directly from recomposition.

showSoftwareKeyboard

default void showSoftwareKeyboard()
See also
show