Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a keyboard language in Android Compose

I'm coding an app in Android Compose. I have a TextField with text in known language, say German. When a user taps the TextField, the keyboard pops up. I want the keyboard to pop up preset with German language (given it is present on the phone) to save the user few taps. How can I do it?

like image 852
Andrey.Kozyrev Avatar asked Sep 10 '25 12:09

Andrey.Kozyrev


2 Answers

As of Compose 1.7.0-beta06, a new parameter has been added to the Keyboard's ImeOptions called hintLocales

With this, you can specify the languages you want your ime to switch to based on the context in your app, directly addressing the OP's question.

From the docs:

List of the languages that the user is supposed to switch to no matter what input method subtype is currently used. This special "hint" can be used mainly for, but not limited to, multilingual users who want IMEs to switch language based on editor's context. Pass LocaleList.Empty to express the intention that a specific hint should not be set.

Sample usage:

OutlinedTextField(
   ...
   keyboardOptions = KeyboardOptions.Default.copy(
        hintLocales = LocaleList(Locale("ar"))
    )
)
like image 176
Mofe Ejegi Avatar answered Sep 13 '25 09:09

Mofe Ejegi


I don't think there is an API now for this feature. This is the signature for TextField,

@Composable
fun TextField(
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions(),
    singleLine: Boolean = false,
    maxLines: Int = Int.MAX_VALUE,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    shape: Shape =
        MaterialTheme.shapes.small.copy(bottomEnd = ZeroCornerSize, bottomStart = ZeroCornerSize),
    colors: TextFieldColors = TextFieldDefaults.textFieldColors()
)

The parameter related to Keyboard is keyboardOptions, and if you check the code for KeyboardOptions

class KeyboardOptions constructor(
    val capitalization: KeyboardCapitalization = KeyboardCapitalization.None,
    val autoCorrect: Boolean = true,
    val keyboardType: KeyboardType = KeyboardType.Text,
    val imeAction: ImeAction = ImeAction.Default
)

inline class KeyboardType internal constructor(@Suppress("unused") private val value: Int) {

    override fun toString(): String {
        return when (this) {
            Text -> "Text"
            Ascii -> "Ascii"
            Number -> "Number"
            Phone -> "Phone"
            Uri -> "Uri"
            Email -> "Email"
            Password -> "Password"
            NumberPassword -> "NumberPassword"
            else -> "Invalid"
        }
    }
...
inline class ImeAction internal constructor(@Suppress("unused") private val value: Int) {

    override fun toString(): String {
        return when (this) {
            None -> "None"
            Default -> "Default"
            Go -> "Go"
            Search -> "Search"
            Send -> "Send"
            Previous -> "Previous"
            Next -> "Next"
            Done -> "Done"
            else -> "Invalid"
        }
    }
...

You can only change the keyboard type and the action button, but there is no API to change the language at this time.

like image 38
Tink Avatar answered Sep 13 '25 07:09

Tink