androidandroid-jetpack-composeandroid-jetpack-compose-material3

How to make the background of TextField transparent?


I am using Jetpack Compose with material3. My screen looks like this. I want the text field to have a transparent background instead of the white background. I tried out the below code (see the line background(.. but it doesn't work. What am I missing here?

TextField(          
        value = userNameValue.value,
        onValueChange = { userNameValue.value = it },
        modifier = Modifier
            .fillMaxWidth()
            .background(
                Color.Transparent
            ),
        label = {
            Text(
                "Names" //stringResource(id = R.string.username_label)
            )
        }
}

enter image description here


Solution

  • The TextField Composable offers a colors parameter. It takes a value of type TextFieldColors that bundles all sorts of colors contained in a TextField.

    In your case, please try to override all the colors that contain the word "Container", similar to this:

    TextField(
        value = userNameValue.value,
        onValueChange = { userNameValue.value = it },
        modifier = Modifier.fillMaxWidth(),
        label = {
            Text(
                text = "Names" //stringResource(id = R.string.username_label)
            ),
        },
        colors = TextFieldDefaults.colors(
            focusedContainerColor = Color.Transparent,
            unfocusedContainerColor = Color.Transparent,
            disabledContainerColor = Color.Transparent,
            errorContainerColor = Color.Transparent
        )
    }