kotlinkeyboardandroid-jetpack-compose

Jetpack Compose KeyboardCapitalization option gets ignored?


From all I can tell, the minimal example below should create a TextField that automatically sets the keyboard to all-caps.

Not sure if I'm doing anything wrong, but on my phone at least, the capitalization option gets ignored and the keyboard stays on lowercase unless I press shift.

Any ideas what could be the cause?

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // A surface container using the 'background' color from the theme
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                //IngredientOverview(listOfFoods)
                var text by remember { mutableStateOf(TextFieldValue("")) }
                TextField(
                    value = text,
                    onValueChange = {
                        text = it
                    },
                    keyboardOptions = KeyboardOptions(
                        capitalization = KeyboardCapitalization.Characters)
                )
            }
        }
    }
}

Solution

  • I had this exact same problem, the issue I encountered was that I was passing in the wrong types to TextField. Making sure I passed in type TextFieldValue solved the issue. Example below of your code.

      TextField(
            value = text, //This should be type TextFieldValue, not a string
            onValueChange = { value: TextFieldValue ->
                text = value , //This should be type TextFieldValuue, not a string
            },
            keyboardOptions = KeyboardOptions(
                capitalization = KeyboardCapitalization.Characters)
            )