androidandroid-jetpack-composeandroid-jetpack-compose-material3

How can I change the background color of TextField in Compose?


I want to change the background color of a TextField in Jetpack Compose, but nothing seems to work.

TextField

I have tried setting Modifier.background(color = Color.Transparent), but it does not work (color does not change). Also, I have tried using both colors = TextFieldDefaults.textFieldColors(...) and colors = TextFieldDefaults.colors(...), as suggested in this question, but none of them have backgroundColor or any other parameter related to background (in Material3). I have also tried this approach, but it does not work either.


Solution

  • The TextField's colors are defined by the colors parameter. There are a lot of different colors that can be customized, but the ones you need are the container colors:

    TextField(
        colors = TextFieldDefaults.colors(
            focusedContainerColor = Color.Green,
            unfocusedContainerColor = Color.Blue,
            disabledContainerColor = Color.Gray,
            errorContainerColor = Color.Red,
        ),
    )
    

    Set them as you like and omit those for that you want to use the default colors.